tiny-types
Version:
A tiny library that brings Tiny Types to JavaScript and TypeScript
39 lines • 1.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasLengthOf = hasLengthOf;
const isDefined_1 = require("./isDefined");
const isEqualTo_1 = require("./isEqualTo");
const property_1 = require("./property");
/**
* @desc Ensures that the `value` is of `expectedLength`.
* Applies to {@link String}s, {@link Array}s and anything that has a `.length` property.
*
* This function is an alias for to `property('length', isDefined(), isEqualTo(expectedLength))`
*
* @example <caption>Array</caption>
* import { ensure, hasLengthOf, TinyType } from 'tiny-types';
*
* class Tuple extends TinyType {
* constructor(public readonly values: any[]) {
* super();
* ensure('Tuple', values, hasLengthOf(2));
* }
* }
*
* @example <caption>String</caption>
* import { ensure, hasLengthOf, TinyType } from 'tiny-types';
*
* class Username extends TinyType {
* constructor(public readonly value: string) {
* super();
* ensure('Username', value, hasLengthOf(8));
* }
* }
*
* @param {number} expectedLength
* @returns {Predicate}
*/
function hasLengthOf(expectedLength) {
return (0, property_1.property)('length', (0, isDefined_1.isDefined)(), (0, isEqualTo_1.isEqualTo)(expectedLength));
}
//# sourceMappingURL=hasLengthOf.js.map