expression-evaluation
Version:
Expression Evaluation
96 lines (95 loc) • 6.75 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.funcCompose = exports.funcReduce = exports.funcFilter = exports.funcTransform = exports.funcReverse = exports.funcFlatten = exports.funcAny = exports.funcEvery = exports.funcLastIndex = exports.funcFirstIndex = exports.funcLast = exports.funcFirst = exports.funcAt = exports.funcValues = exports.funcKeys = exports.funcEntries = exports.funcCharCode = exports.funcChar = exports.funcByte = exports.funcSlice = exports.funcLength = exports.funcAppend = void 0;
const FunctionDefinition_js_1 = require("../FunctionDefinition.js");
const Type_js_1 = require("../Type.js");
exports.funcAppend = new FunctionDefinition_js_1.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], []), Type_js_1.typeEnumerable, [Type_js_1.typeEnumerable], 2, FunctionDefinition_js_1.FUNCTION_ARG_MAX, 0);
exports.funcLength = new FunctionDefinition_js_1.FunctionDefinition((value) => value == null
? undefined
: value instanceof ArrayBuffer
? value.byteLength
: typeof value === 'string' || Array.isArray(value)
? value.length
: Object.keys(value).length, Type_js_1.typeNumber, [Type_js_1.typeIterable]);
exports.funcSlice = new FunctionDefinition_js_1.FunctionDefinition((value, start = 0, end) => value == null
? undefined
: value.slice(start, end), Type_js_1.typeEnumerable, [Type_js_1.typeEnumerable, Type_js_1.typeOptionalNumber, Type_js_1.typeOptionalNumber], 1, 3);
exports.funcByte = new FunctionDefinition_js_1.FunctionDefinition((value, pos) => value == null
? undefined
: value.slice(pos, pos + 1), Type_js_1.typeBuffer, [Type_js_1.typeBuffer, Type_js_1.typeNumber]);
exports.funcChar = new FunctionDefinition_js_1.FunctionDefinition((value, pos) => value == null
? undefined
: value.charAt(pos < 0 ? value.length + pos : pos), Type_js_1.typeString, [Type_js_1.typeString, Type_js_1.typeNumber]);
exports.funcCharCode = new FunctionDefinition_js_1.FunctionDefinition((value, pos) => value == null
? undefined
: value.charCodeAt(pos < 0 ? value.length + pos : pos), Type_js_1.typeNumber, [Type_js_1.typeString, Type_js_1.typeNumber]);
exports.funcEntries = new FunctionDefinition_js_1.FunctionDefinition((value) => value == null
? undefined
: Array.isArray(value)
? Object.entries(value).map((e) => [Number(e[0]), e[1]])
: Object.entries(value), Type_js_1.typeOptionalArray, [Type_js_1.typeOptionalArrayOrObject]);
exports.funcKeys = new FunctionDefinition_js_1.FunctionDefinition((value) => value == null
? undefined
: Array.isArray(value)
? Object.keys(value).map((k) => Number(k))
: Object.keys(value), Type_js_1.typeOptionalArray, [Type_js_1.typeOptionalArrayOrObject]);
exports.funcValues = new FunctionDefinition_js_1.FunctionDefinition((value) => value == null
? undefined
: Object.values(value), Type_js_1.typeOptionalArray, [Type_js_1.typeOptionalArrayOrObject]);
exports.funcAt = new FunctionDefinition_js_1.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)];
}
}, Type_js_1.typeUnknown, [Type_js_1.typeOptionalArrayOrObject, Type_js_1.typeNumberOrString]);
exports.funcFirst = new FunctionDefinition_js_1.FunctionDefinition((value, predicate) => value?.find((v, i, a) => predicate(v, i, a)), Type_js_1.typeUnknown, [Type_js_1.typeArray, Type_js_1.typeFunction]);
exports.funcLast = new FunctionDefinition_js_1.FunctionDefinition((value, predicate) => value?.reverse().find((v, i, a) => predicate(v, i, a)), Type_js_1.typeUnknown, [Type_js_1.typeArray, Type_js_1.typeFunction]);
exports.funcFirstIndex = new FunctionDefinition_js_1.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;
}, Type_js_1.typeNumber, [Type_js_1.typeArray, Type_js_1.typeFunction]);
exports.funcLastIndex = new FunctionDefinition_js_1.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;
}, Type_js_1.typeNumber, [Type_js_1.typeArray, Type_js_1.typeFunction]);
exports.funcEvery = new FunctionDefinition_js_1.FunctionDefinition((value, predicate) => value?.every((v, i, a) => predicate(v, i, a)), Type_js_1.typeBoolean, [Type_js_1.typeArray, Type_js_1.typeFunction]);
exports.funcAny = new FunctionDefinition_js_1.FunctionDefinition((value, predicate) => value?.some((v, i, a) => predicate(v, i, a)), Type_js_1.typeBoolean, [Type_js_1.typeArray, Type_js_1.typeFunction]);
exports.funcFlatten = new FunctionDefinition_js_1.FunctionDefinition((values, depth) => values?.flat(depth), Type_js_1.typeArray, [Type_js_1.typeArray, Type_js_1.typeOptionalNumber], 1, 2);
exports.funcReverse = new FunctionDefinition_js_1.FunctionDefinition((value) => [...value].reverse(), Type_js_1.typeArray, [Type_js_1.typeArray]);
exports.funcTransform = new FunctionDefinition_js_1.FunctionDefinition((value, callback) => value?.map(callback), Type_js_1.typeArray, [Type_js_1.typeArray, Type_js_1.typeFunction]);
exports.funcFilter = new FunctionDefinition_js_1.FunctionDefinition((value, predicate) => value?.filter(predicate), Type_js_1.typeArray, [Type_js_1.typeArray, Type_js_1.typeFunction]);
exports.funcReduce = new FunctionDefinition_js_1.FunctionDefinition((value, callback, initial) => initial != null ? value?.reduce(callback, initial) : value?.reduce(callback), Type_js_1.typeUnknown, [Type_js_1.typeArray, Type_js_1.typeFunction, Type_js_1.typeUnknown], 2, 3);
exports.funcCompose = new FunctionDefinition_js_1.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;
}, Type_js_1.typeObject, [Type_js_1.typeArray, Type_js_1.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;
}
;
;