From 042dff667e5c8f496c71a38a4e246fdf40dc437f Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 30 Dec 2023 15:12:11 -0400 Subject: validators: set creator meta on typeof, instanceof validators --- src/data/things/validators.js | 52 +++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 17 deletions(-) (limited to 'src/data/things/validators.js') diff --git a/src/data/things/validators.js b/src/data/things/validators.js index 94c73b2a..027aa195 100644 --- a/src/data/things/validators.js +++ b/src/data/things/validators.js @@ -32,21 +32,38 @@ export function a(noun) { return /[aeiou]/.test(noun[0]) ? `an ${noun}` : `a ${noun}`; } -export function isType(value, type) { - if (typeof value !== type) - throw new TypeError(`Expected ${a(type)}, got ${typeAppearance(value)}`); +export function validateType(type) { + const fn = value => { + if (typeof value !== type) + throw new TypeError(`Expected ${a(type)}, got ${typeAppearance(value)}`); - return true; -} + return true; + }; -export function isBoolean(value) { - return isType(value, 'boolean'); -} + setValidatorCreatorMeta(fn, validateType, {type}); -export function isNumber(value) { - return isType(value, 'number'); + return fn; } +export const isBoolean = + validateType('boolean'); + +export const isFunction = + validateType('function'); + +export const isNumber = + validateType('number'); + +export const isString = + validateType('string'); + +export const isSymbol = + validateType('symbol'); + +// Use isObject instead, which disallows null. +export const isTypeofObject = + validateType('object'); + export function isPositive(number) { isNumber(number); @@ -101,10 +118,6 @@ export function isWholeNumber(number) { return true; } -export function isString(value) { - return isType(value, 'string'); -} - export function isStringNonEmpty(value) { isString(value); @@ -142,12 +155,13 @@ export function isDate(value) { } export function isObject(value) { - isType(value, 'object'); + isTypeofObject(value); // Note: Please remember that null is always a valid value for properties // held by a CacheableObject. This assertion is exclusively for use in other // contexts. - if (value === null) throw new TypeError(`Expected an object, got null`); + if (value === null) + throw new TypeError(`Expected an object, got null`); return true; } @@ -245,7 +259,11 @@ export function sparseArrayOf(itemValidator) { } export function validateInstanceOf(constructor) { - return (object) => isInstance(object, constructor); + const fn = (object) => isInstance(object, constructor); + + setValidatorCreatorMeta(fn, validateInstanceOf, {constructor}); + + return fn; } // Wiki data (primitives & non-primitives) -- cgit 1.3.0-6-gf8a5