UNPKG

castage

Version:

A type-safe library for dynamic object casting and ensuring type consistency in JavaScript/TypeScript.

32 lines (31 loc) 1.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.allOf = void 0; const resultage_1 = require("resultage"); const engine_js_1 = require("./engine.js"); const predicates_js_1 = require("./predicates.js"); /** * Combines multiple casters into a single caster that applies all of them. * The resulting caster will validate and transform a value using each of the provided casters, * and merge the results into a single object. * * @template T - A tuple of object types that the casters will handle. * @param {...{ [K in keyof T]: Caster<T[K]> }} casters - An array of casters to be combined. * @returns {Caster<UnionToIntersection<T[number]>>} A caster that applies all provided casters and merges their results. */ const allOf = (...casters) => { const name = `(${casters.map((c) => c.name).join(' & ')})`; return (0, engine_js_1.fromGuardAndTransform)(predicates_js_1.isObject, (value, path) => { const resultedValue = {}; for (let i = 0; i < casters.length; i += 1) { const caster = casters[i]; const result = caster(value, path); if (result.isErr) return result; const v = (0, resultage_1.unwrap)(result); Object.assign(resultedValue, v); } return (0, resultage_1.ok)(resultedValue); }, name); }; exports.allOf = allOf;