ts-prims
Version:
Typescript Primitives
27 lines • 1.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.charsConstraint = exports.lengthOf = void 0;
const length_js_1 = require("./length.js");
const prim_js_1 = require("./prim.js");
/** Utility function to convert a low length `l` in chars back to a `LowLength`. */
const lengthOf = (l) => l == 0 ? 0 :
l <= length_js_1.shortLengthChars[1] ? 1 :
l == length_js_1.shortLengthChars[2] ? 2 :
l == length_js_1.shortLengthChars[3] ? 3 :
l == length_js_1.shortLengthChars[4] ? 4 :
l == length_js_1.shortLengthChars[5] ? 5 :
l == length_js_1.shortLengthChars[6] ? 6 :
7;
exports.lengthOf = lengthOf;
/** Constrains a type to the given length in chars `L`. */
const charsConstraint = (l) => {
console.info(`charsConstraint(${l})`);
return (pc, v) => {
console.info(`charsConstraint(${l}):`, v);
return (typeof v == 'string') && (v.length <= l) ? undefined :
`${(0, prim_js_1.display)(v)} is not assignable to '${pc.name}'.\n` +
` Length exceeds ${l}.`;
};
};
exports.charsConstraint = charsConstraint;
//# sourceMappingURL=chars.js.map