UNPKG

tiny-types

Version:

A tiny library that brings Tiny Types to JavaScript and TypeScript

44 lines 1.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isEqualTo = isEqualTo; const TinyType_1 = require("../TinyType"); const Predicate_1 = require("./Predicate"); /** * @desc Ensures that the `value` is equal to `expectedValue`. * This {@link Predicate} is typically used in combination with other {@link Predicate}s. * * @example <caption>Comparing Tiny Types</caption> * import { ensure, isEqualTo, TinyType, TinyTypeOf } from 'tiny-types'; * * class AccountId extends TinyTypeOf<number>() {} * class Command extends TinyTypeOf<AccountId>() {} * class UpgradeAccount extends Command {} * * class AccountsService { * constructor(public readonly loggedInUser: AccountId) {} * handle(command: Command) { * ensure('AccountId', command.value, isEqualTo(this.loggedInUser)); * } * } * * @example <caption>Comparing primitives</caption> * import { ensure, isEqualTo, TinyType } from 'tiny-types'; * * class Admin extends TinyType { * constructor(public readonly id: number) { * ensure('Admin::id', id, isEqualTo(1)); * } * } * * @param {string | number | symbol | TinyType | object} expectedValue * @returns {Predicate<any>} */ function isEqualTo(expectedValue) { return Predicate_1.Predicate.to(`be equal to ${String(expectedValue)}`, (value) => (hasEquals(value) && hasEquals(expectedValue)) ? value.equals(expectedValue) : value === expectedValue); } function hasEquals(value) { return !!value && (value instanceof TinyType_1.TinyType || value.equals); } //# sourceMappingURL=isEqualTo.js.map