diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2023-12-30 15:12:11 -0400 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2023-12-30 15:49:09 -0400 |
commit | 042dff667e5c8f496c71a38a4e246fdf40dc437f (patch) | |
tree | baafa64eca5d32f17914203eb5fd90a1866f6d22 /src/data | |
parent | c3e8199a6ab1305bcc528861bf71d37098dee947 (diff) |
validators: set creator meta on typeof, instanceof validators
Diffstat (limited to 'src/data')
-rw-r--r-- | src/data/composite/wiki-data/inputThingClass.js | 4 | ||||
-rw-r--r-- | src/data/things/validators.js | 52 |
2 files changed, 37 insertions, 19 deletions
diff --git a/src/data/composite/wiki-data/inputThingClass.js b/src/data/composite/wiki-data/inputThingClass.js index d70480e6..5f2ca5a6 100644 --- a/src/data/composite/wiki-data/inputThingClass.js +++ b/src/data/composite/wiki-data/inputThingClass.js @@ -3,7 +3,7 @@ // referencing Thing class values defined outside of the #composite folder. import {input} from '#composite'; -import {isType} from '#validators'; +import {isFunction} from '#validators'; // TODO: Kludge. import Thing from '../../things/thing.js'; @@ -11,7 +11,7 @@ import Thing from '../../things/thing.js'; export default function inputThingClass() { return input.staticValue({ validate(thingClass) { - isType(thingClass, 'function'); + isFunction(thingClass); if (!Object.hasOwn(thingClass, Thing.referenceType)) { throw new TypeError(`Expected a Thing constructor, missing Thing.referenceType`); 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) |