mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-18 10:01:23 +01:00
refactor: start files migration nightmare
This commit is contained in:
parent
c4736cc640
commit
bc65e0e207
64 changed files with 1990 additions and 816 deletions
33
src/utils/arrayUtils.ts
Normal file
33
src/utils/arrayUtils.ts
Normal 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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue