UNPKG

variant

Version:

Variant types (a.k.a. Discriminated Unions) in TypeScript

57 lines 2.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.matchElse = exports.partialMatch = exports.augment = void 0; /** * THIS FILE is for the content I intent to remove on Variant 3.0 that * I'm tired of seeing or considering in the code, so it gets shuffled * off to here. */ const match_1 = require("./match"); /** * @deprecated * Expand the functionality of a variant as a whole by tacking on properties * generated by a thunk. * @param variantDef * @param f */ function augment(variantDef, f) { return Object.keys(variantDef).reduce((acc, key) => { const augmentedFuncWrapper = (...args) => (Object.assign({}, f(), variantDef[key](...args))); return Object.assign(Object.assign({}, acc), { [key]: Object.assign(augmentedFuncWrapper, { key: variantDef[key].key, type: variantDef[key].type }) }); }, {}); } exports.augment = augment; /** * Match a variant against some of its possible options and do some * processing based on the type of variant received. May return undefined * if the variant is not accounted for by the handler. * @deprecated * @param obj * @param handler * @param typeKey override the property to inspect. By default, 'type'. */ function partialMatch(obj, handler, typeKey) { return match_1.match(obj, handler, typeKey); } exports.partialMatch = partialMatch; ; /** * Match a variant against it's some of its possible options and do some * processing based on the type of variant received. Finally, take the remaining * possibilities and handle them in a function. * * The input to the 'or' clause is well-typed. * * @deprecated * @param obj the variant in question * @param handler an object whose keys are the type names of the variant's type and values are handler functions for each option. * @param {string?} typeKey override the property to inspect. By default, 'type'. * @returns {The union of the return types of the various branches of the handler object} */ function matchElse(obj, handler, _else, typeKey) { var _a, _b; const typeString = obj[typeKey !== null && typeKey !== void 0 ? typeKey : 'type']; return (_b = (_a = handler[typeString]) === null || _a === void 0 ? void 0 : _a.call(handler, obj)) !== null && _b !== void 0 ? _b : _else(obj); } exports.matchElse = matchElse; //# sourceMappingURL=deprecated.js.map