refactor: start files migration nightmare

This commit is contained in:
Azgaar 2022-06-25 00:47:48 +03:00
parent c4736cc640
commit bc65e0e207
64 changed files with 1990 additions and 816 deletions

33
src/utils/arrayUtils.ts Normal file
View file

@ -0,0 +1,33 @@
import {UINT16_MAX, UINT32_MAX, UINT8_MAX} from "../constants";
export function last<T>(array: T[]) {
return array[array.length - 1];
}
export function unique<T>(array: T[]) {
return [...new Set(array)];
}
function getTypedArray(maxValue: number) {
console.assert(
Number.isInteger(maxValue) && maxValue >= 0 && maxValue <= UINT32_MAX,
`Array maxValue must be an integer between 0 and ${UINT32_MAX}, got ${maxValue}`
);
if (maxValue <= UINT8_MAX) return Uint8Array;
if (maxValue <= UINT16_MAX) return Uint16Array;
if (maxValue <= UINT32_MAX) return Uint32Array;
return Uint32Array;
}
interface ICreateTypedArray {
maxValue: number;
length: number;
from: ArrayLike<number>;
}
export function createTypedArray({maxValue, length, from}: ICreateTypedArray) {
const typedArray = getTypedArray(maxValue);
if (!from) return new typedArray(length);
return typedArray.from(from);
}