UNPKG

expressionparser

Version:

Parse simple expressions, in a language of your own description

108 lines 3.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.char = exports.string = exports.iterable = exports.obj = exports.evalArray = exports.evalString = exports.evalBool = exports.bool = exports.array = exports.num = exports.unpackArgs = void 0; const ExpressionParser_1 = require("./ExpressionParser"); exports.unpackArgs = (f) => (expr) => { const result = expr(); if (!ExpressionParser_1.isArgumentsArray(result)) { if (f.length > 1) { throw new Error(`Too few arguments. Expected ${f.length}, found 1 (${JSON.stringify(result)})`); } return f(() => result); } else if (result.length === f.length || f.length === 0) { return f.apply(null, result); } else { throw new Error(`Incorrect number of arguments. Expected ${f.length}`); } }; exports.num = (result) => { if (typeof result !== "number") { throw new Error(`Expected number, found: ${typeof result} ${JSON.stringify(result)}`); } return result; }; exports.array = (result) => { if (!Array.isArray(result)) { throw new Error(`Expected array, found: ${typeof result} ${JSON.stringify(result)}`); } if (ExpressionParser_1.isArgumentsArray(result)) { throw new Error(`Expected array, found: arguments`); } return result; }; exports.bool = (value) => { if (typeof value !== "boolean") { throw new Error(`Expected boolean, found: ${typeof value} ${JSON.stringify(value)}`); } return value; }; exports.evalBool = (value) => { let result; while (typeof value === "function" && value.length === 0) { result = value(); } if (!result) { result = value; } return exports.bool(result); }; exports.evalString = (value) => { let result; if (typeof value === "function" && value.length === 0) { result = value(); } else { result = value; } return exports.string(result); }; exports.evalArray = (arr, typeCheck) => { return exports.array(arr).map((value) => { let result; if (typeof value === "function" && value.length === 0) { result = value(); } else { result = value; } if (typeCheck) { try { result = typeCheck(result); } catch (err) { throw new Error(`In array; ${err.message}`); } } return result; }); }; exports.obj = (obj) => { if (typeof obj !== "object" || obj === null) { throw new Error(`Expected object, found: ${typeof obj} ${JSON.stringify(obj)}`); } else if (Array.isArray(obj)) { throw new Error(`Expected object, found array`); } return obj; }; exports.iterable = (result) => { if (!Array.isArray(result) && typeof result !== "string") { throw new Error(`Expected array or string, found: ${typeof result} ${JSON.stringify(result)}`); } return result; }; exports.string = (result) => { if (typeof result !== "string") { throw new Error(`Expected string, found: ${typeof result} ${JSON.stringify(result)}`); } return result; }; exports.char = (result) => { if (typeof result !== "string" || result.length !== 1) { throw new Error(`Expected char, found: ${typeof result} ${JSON.stringify(result)}`); } return result; }; //# sourceMappingURL=helpers.js.map