expression-evaluation
Version:
Expression Evaluation
93 lines (92 loc) • 5.36 kB
JavaScript
import { FunctionDefinition, FUNCTION_ARG_MAX } from '../FunctionDefinition.js';
import { typeBoolean, typeNumber, typeBuffer, typeString, typeArray, typeObject, typeFunction, typeOptionalNumber, typeOptionalArray, typeNumberOrString, typeOptionalArrayOrObject, typeEnumerable, typeIterable, typeUnknown } from '../Type.js';
export const funcAppend = new FunctionDefinition((...values) => values[0] instanceof ArrayBuffer
? values.reduce((acc, val) => concatBuffers(acc, val), new ArrayBuffer(0))
: typeof values[0] === 'string'
? values.reduce((acc, val) => acc + val, '')
: values.reduce((acc, val) => [...acc, ...val], []), typeEnumerable, [typeEnumerable], 2, FUNCTION_ARG_MAX, 0);
export const funcLength = new FunctionDefinition((value) => value == null
? undefined
: value instanceof ArrayBuffer
? value.byteLength
: typeof value === 'string' || Array.isArray(value)
? value.length
: Object.keys(value).length, typeNumber, [typeIterable]);
export const funcSlice = new FunctionDefinition((value, start = 0, end) => value == null
? undefined
: value.slice(start, end), typeEnumerable, [typeEnumerable, typeOptionalNumber, typeOptionalNumber], 1, 3);
export const funcByte = new FunctionDefinition((value, pos) => value == null
? undefined
: value.slice(pos, pos + 1), typeBuffer, [typeBuffer, typeNumber]);
export const funcChar = new FunctionDefinition((value, pos) => value == null
? undefined
: value.charAt(pos < 0 ? value.length + pos : pos), typeString, [typeString, typeNumber]);
export const funcCharCode = new FunctionDefinition((value, pos) => value == null
? undefined
: value.charCodeAt(pos < 0 ? value.length + pos : pos), typeNumber, [typeString, typeNumber]);
export const funcEntries = new FunctionDefinition((value) => value == null
? undefined
: Array.isArray(value)
? Object.entries(value).map((e) => [Number(e[0]), e[1]])
: Object.entries(value), typeOptionalArray, [typeOptionalArrayOrObject]);
export const funcKeys = new FunctionDefinition((value) => value == null
? undefined
: Array.isArray(value)
? Object.keys(value).map((k) => Number(k))
: Object.keys(value), typeOptionalArray, [typeOptionalArrayOrObject]);
export const funcValues = new FunctionDefinition((value) => value == null
? undefined
: Object.values(value), typeOptionalArray, [typeOptionalArrayOrObject]);
export const funcAt = new FunctionDefinition((value, index) => {
if (value == null) {
return undefined;
}
else if (Array.isArray(value)) {
const ix = Number(index);
return value[ix < 0 ? value.length + ix : ix];
}
else {
return value[String(index)];
}
}, typeUnknown, [typeOptionalArrayOrObject, typeNumberOrString]);
export const funcFirst = new FunctionDefinition((value, predicate) => value?.find((v, i, a) => predicate(v, i, a)), typeUnknown, [typeArray, typeFunction]);
export const funcLast = new FunctionDefinition((value, predicate) => value?.reverse().find((v, i, a) => predicate(v, i, a)), typeUnknown, [typeArray, typeFunction]);
export const funcFirstIndex = new FunctionDefinition((value, predicate) => {
if (value == null) {
return undefined;
}
const ix = value.findIndex((v, i, a) => predicate(v, i, a));
return ix < 0 ? Number.NaN : ix;
}, typeNumber, [typeArray, typeFunction]);
export const funcLastIndex = new FunctionDefinition((value, predicate) => {
if (value == null) {
return undefined;
}
const ix = [...value].reverse().findIndex((v, i, a) => predicate(v, i, a));
return ix < 0 ? Number.NaN : ix;
}, typeNumber, [typeArray, typeFunction]);
export const funcEvery = new FunctionDefinition((value, predicate) => value?.every((v, i, a) => predicate(v, i, a)), typeBoolean, [typeArray, typeFunction]);
export const funcAny = new FunctionDefinition((value, predicate) => value?.some((v, i, a) => predicate(v, i, a)), typeBoolean, [typeArray, typeFunction]);
export const funcFlatten = new FunctionDefinition((values, depth) => values?.flat(depth), typeArray, [typeArray, typeOptionalNumber], 1, 2);
export const funcReverse = new FunctionDefinition((value) => [...value].reverse(), typeArray, [typeArray]);
export const funcTransform = new FunctionDefinition((value, callback) => value?.map(callback), typeArray, [typeArray, typeFunction]);
export const funcFilter = new FunctionDefinition((value, predicate) => value?.filter(predicate), typeArray, [typeArray, typeFunction]);
export const funcReduce = new FunctionDefinition((value, callback, initial) => initial != null ? value?.reduce(callback, initial) : value?.reduce(callback), typeUnknown, [typeArray, typeFunction, typeUnknown], 2, 3);
export const funcCompose = new FunctionDefinition((value, callback) => {
if (value == null) {
return undefined;
}
const obj = {};
for (let i = 0; i < value.length; ++i) {
const key = value[i];
obj[key] = callback(obj, key, i);
}
return obj;
}, typeObject, [typeArray, typeFunction]);
function concatBuffers(value1, value2) {
const bytes = new Uint8Array(value1.byteLength + value2.byteLength);
bytes.set(new Uint8Array(value1), 0);
bytes.set(new Uint8Array(value2), value1.byteLength);
return bytes.buffer;
}
;