import { $NAME, $CONSTRUCTOR, MetaObject, MetaContext } from '../../MetaContext'; import { $ASSIGN } from '../../operations/base/$assign'; import { $CLONE } from '../../operations/base/$clone'; import { $COMPARE } from '../../operations/base/$compare'; import { $CONSTRUCT, genericConstruct } from '../../operations/base/$construct'; import { $DUMP } from '../../operations/base/$dump'; import { $EQUAL } from '../../operations/base/$equal'; import { $HASH, $Hash } from '../../operations/base/$hash'; import { $RESTORE, $CREATE, genericCreate } from '../../operations/base/$restore'; import { $SERIALIZE, genericSerialize } from '../../operations/base/$serialize'; import { $STRINGIFY, deepStringify, stringifyArrayLikeWrapper } from '../../operations/base/$stringify'; export function $assign(lhs: any, rhs: any) { if (lhs.constructor !== rhs.constructor) return new lhs.constructor(rhs); if (lhs.length !== rhs.length) return rhs.slice(); lhs.set(rhs); return lhs; } export function $clone(arr: any) { return arr.slice(); } export function $compare(lhs: any, rhs: any) { const lhsLength = lhs.length; const rhsLength = rhs.length; const minLength = Math.min(lhsLength, rhsLength); for (let i = 0; i < minLength; i++) { const result = lhs[i] - rhs[i]; if (result !== 0) return result; } return lhsLength - rhsLength; } export const $construct = genericConstruct; export const $create = genericCreate; export function $dump(value: any) { return Array.from(value); } export function $equal(lhs: any, rhs: any) { if (lhs.length !== rhs.length) return false; for (let i = 0; i < lhs.length; i++) if (lhs[i] !== rhs[i]) return false; return true; } export function $hash(this: MetaObject, arr: any, seed = 0, context: MetaContext & $Hash) { for (let i = 0; i < arr.length; i++) seed = context[$HASH](arr[i], seed); return seed; } export function $restore(this: MetaObject, arr: any, state: any) { if (arr.length !== state.length) return new this[$CONSTRUCTOR]!(state); for (let i = 0; i < arr.length; i++) arr[i] = state[i]; return arr; } export const $serialize = genericSerialize; export const $stringify = deepStringify((arr: any) => arr.map((val: any) => String(val)).join(', '), stringifyArrayLikeWrapper); export const $tags = ([ [Int8Array, 'Int8Array'], [Uint8Array, 'Uint8Array'], [Uint8ClampedArray, 'Uint8ClampedArray'], [Int16Array, 'Int16Array'], [Uint16Array, 'Uint16Array'], [Int32Array, 'Int32Array'], [Uint32Array, 'Uint32Array'], [Float32Array, 'Float32Array'], [Float64Array, 'Float64Array'] ] as [ [Int8ArrayConstructor, 'Int8Array'], [Uint8ArrayConstructor, 'Uint8Array'], [Uint8ClampedArrayConstructor, 'Uint8ClampedArray'], [Int16ArrayConstructor, 'Int16Array'], [Uint16ArrayConstructor, 'Uint16Array'], [Int32ArrayConstructor, 'Int32Array'], [Uint32ArrayConstructor, 'Uint32Array'], [Float32ArrayConstructor, 'Float32Array'], [Float64ArrayConstructor, 'Float64Array'] ]).map(([$constructor, $name]) => ({ [$CONSTRUCTOR]: $constructor, [$NAME]: $name })); export default $tags.map(({ $constructor, $name }) => ({ [$CONSTRUCTOR]: $constructor, [$NAME]: $name, [$ASSIGN]: $assign, [$CLONE]: $clone, [$COMPARE]: $compare, [$CONSTRUCT]: $construct, [$CREATE]: $create, [$DUMP]: $dump, [$EQUAL]: $equal, [$HASH]: $hash, [$RESTORE]: $restore, [$SERIALIZE]: $serialize, [$STRINGIFY]: $stringify }));