onnxruntime/js/common/test/unit-tests/common.ts
Yulong Wang abdc31de40
[js] change default formatter for JavaScript/TypeScript from clang-format to Prettier (#21728)
### Description

See
454996d496
for manual changes (excluded auto-generated formatting changes)

### Why

Because the toolsets for old clang-format is out-of-date. This reduces
the development efficiency.

- The NPM package `clang-format` is already in maintenance mode. not
updated since 2 years ago.
- The VSCode extension for clang-format is not maintained for a while,
and a recent Node.js security update made it not working at all in
Windows.

No one in community seems interested in fixing those.

Choose Prettier as it is the most popular TS/JS formatter.

### How to merge

It's easy to break the build:
- Be careful of any new commits on main not included in this PR.
- Be careful that after this PR is merged, other PRs that already passed
CI can merge.

So, make sure there is no new commits before merging this one, and
invalidate js PRs that already passed CI, force them to merge to latest.
2024-08-14 16:51:22 -07:00

58 lines
2 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import assert from 'assert/strict';
import { Tensor } from 'onnxruntime-common';
/**
* A list of numerical types that are compatible with JavaScript 'number' value.
*
* 3 elements in each list are:
* - type: a string representing the type name,
* - typedArrayConstructor: the built-in typed array constructor for the type,
* - canBeInferredFromType: whether the type can be inferred from the type name.
*/
export const NUMBER_COMPATIBLE_NUMERICAL_TYPES = [
['int8', Int8Array, true] as const,
['uint8', Uint8Array, true] as const,
['int16', Int16Array, true] as const,
['uint16', Uint16Array, true] as const,
['int32', Int32Array, true] as const,
['uint32', Uint32Array, true] as const,
['float32', Float32Array, true] as const,
['float64', Float64Array, true] as const,
];
/**
* Big integer types
*/
export const BIGINT_TYPES = [['int64', BigInt64Array, true] as const, ['uint64', BigUint64Array, true] as const];
/**
* float16 type, data represented by Uint16Array
*/
export const FLOAT16_TYPE = ['float16', Uint16Array, false] as const;
/**
* A list of all numerical types.
*
* not including string and bool.
*/
export const ALL_NUMERICAL_TYPES = [...NUMBER_COMPATIBLE_NUMERICAL_TYPES, ...BIGINT_TYPES, FLOAT16_TYPE];
/**
* a helper function to assert that a value is an array of a certain type
*/
export const assertIsArrayOf = (value: unknown, type: 'string' | 'number' | 'boolean'): void => {
assert(Array.isArray(value), 'array should be an array');
for (let i = 0; i < value.length; i++) {
assert.equal(typeof value[i], type, `array should be an array of ${type}s`);
}
};
/**
* the 'TensorAny' is a type allows skip typescript type checking for Tensor.
*
* This allows to write test code to pass invalid parameters to Tensor constructor and check the behavior.
*/
export const TensorAny = Tensor as unknown as { new (...args: unknown[]): Tensor };