UNPKG

alwz

Version:

Extendable library for typecasting

157 lines 6.26 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.projection = exports.dictionary = exports.object = exports.variant = exports.range = exports.tuple = exports.array = void 0; const ErrorValue_js_1 = __importDefault(require("./models/ErrorValue.cjs")); const Converter_js_1 = __importStar(require("./models/Converter.cjs")); const presets = __importStar(require("./presets.cjs")); const InvalidArgument = class extends ErrorValue_js_1.default { }; const array = (conversion, initiator = presets.array.convert) => { (0, Converter_js_1.assertConversion)(conversion); if (!(0, Converter_js_1.isConversion)(initiator)) { throw new Converter_js_1.default.InvalidConversionFunction('initiator must be a function', initiator); } return (input) => { return initiator(input).map(conversion); }; }; exports.array = array; const tuple = (conversions, initiator = presets.array.convert) => { if (!Array.isArray(conversions)) { throw new Converter_js_1.default.InvalidConversionFunction('first argument must be an array', conversions); } conversions.forEach((conversion, index) => { if (!(0, Converter_js_1.isConversion)(conversion)) { throw new Converter_js_1.default.InvalidConversionFunction('conversion ' + index + ' must be a function', [index, conversion]); } }); if (!(0, Converter_js_1.isConversion)(initiator)) { throw new Converter_js_1.default.InvalidConversionFunction('initiator must be a function', initiator); } return (input) => { const array = initiator(input); return conversions.map((fn, index) => fn(array[index])); }; }; exports.tuple = tuple; const range = (lower = -Number.MAX_VALUE, upper = Number.MAX_VALUE, fallback, conversion = presets.double.convert) => { (0, Converter_js_1.assertConversion)(conversion); lower = conversion(lower); upper = conversion(upper); const fallbackActual = fallback === undefined ? (input) => { const converted = conversion(input); return upper <= converted ? upper : lower; } : fallback; (0, Converter_js_1.assertFallback)(fallbackActual); return (input) => { const converted = conversion(input); if ((lower <= converted) && (converted <= upper)) { return converted; } return fallbackActual(input); }; }; exports.range = range; const variant = (values, fallback = () => values[0], conversion = presets.double.convert) => { (0, Converter_js_1.assertConversion)(conversion); (0, Converter_js_1.assertFallback)(fallback); if (!Array.isArray(values)) { throw new InvalidArgument('variant values must be an array of allowed values', values); } values = values.map((value) => conversion(value)); return (input) => { const converted = conversion(input); if (values.includes(converted)) { return converted; } return fallback(input); }; }; exports.variant = variant; const object = (schema, conversion = presets.object.convert) => { if (typeof schema !== 'object' || schema === null) { throw new InvalidArgument('schema must must be an object', schema); } for (const key in schema) { const conversion = schema[key]; (0, Converter_js_1.assertConversion)(conversion); } (0, Converter_js_1.assertConversion)(conversion); return (input) => { const result = {}; const source = conversion(input); for (const key in schema) { const type = schema[key]; result[key] = type(source === null || source === void 0 ? void 0 : source[key]); } return result; }; }; exports.object = object; const dictionary = (conversion, initiator = presets.object.convert) => { if (typeof conversion !== 'function') { throw new InvalidArgument('conversion must be a function', conversion); } (0, Converter_js_1.assertConversion)(initiator); return (input) => { const result = {}; const source = initiator(input); for (const key in source) { result[key] = conversion(source === null || source === void 0 ? void 0 : source[key], key); } return result; }; }; exports.dictionary = dictionary; const projection = (schema) => { const builders = Object .entries(schema) .map(([key, value]) => { if (typeof value === 'function') { return [key, value]; } else if (value && typeof value === 'object') { return [key, (0, exports.projection)(value)]; } else { throw new ErrorValue_js_1.default('invalid schema item', { key, value }); } }); return function (source, options) { return builders .reduce((target, [key, build]) => { target[key] = build.call(this, source, options, target); return target; }, {}); }; }; exports.projection = projection; //# sourceMappingURL=utils.cjs.map