diff --git a/package.json b/package.json index 45227df..e3c4652 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "typescript": "^4.0.2" }, "dependencies": { - "@orioro/validate-type": "^1.3.1", + "@orioro/validate-type": "^1.4.0", "lodash": "^4.17.20" }, "config": { diff --git a/src/expression.ts b/src/expression.ts index 8dd3f25..68c1a34 100644 --- a/src/expression.ts +++ b/src/expression.ts @@ -1,4 +1,6 @@ -import { validateType } from '@orioro/validate-type' +import { validateType, ExpectedType } from '@orioro/validate-type' + +import { isPlainObject } from 'lodash' import { Expression, @@ -116,32 +118,18 @@ type ParamResolverFunction = (context: EvaluationContext, arg: any) => any * * @typedef {Function | null | string | string[]} ParamResolver */ -type ParamType = - | 'string' - | 'regexp' - | 'number' - | 'bigint' - | 'nan' - | 'null' - | 'undefined' - | 'boolean' - | 'function' - | 'object' - | 'array' - | 'date' - | 'symbol' - | 'map' - | 'set' - | 'weakmap' - | 'weakset' - | 'any' export type ParamResolver = | ParamResolverFunction | null - | ParamType - | ParamType[] + | ExpectedType + | ExpectedType[] + +const _paramResolverNoop = (context: EvaluationContext, arg: any): any => arg -const paramResolverNoop = (context: EvaluationContext, arg: any): any => arg +const _isExpectedType = (resolver: ParamResolver): boolean => + Array.isArray(resolver) || + isPlainObject(resolver) || + typeof resolver === 'string' /** * @function interpreter @@ -178,12 +166,12 @@ export const interpreter = ( const _paramResolvers = paramResolvers.map((resolver) => { if (typeof resolver === 'function') { return resolver - } else if (Array.isArray(resolver) || typeof resolver === 'string') { + } else if (_isExpectedType(resolver)) { return resolver === 'any' ? evaluate : evaluateTyped.bind(null, resolver) } else if (resolver === null) { - return paramResolverNoop + return _paramResolverNoop } else { throw new TypeError( `Expected resolver to be either Function | ExpectedType | 'any' | null, but got ${typeof resolver}: ${resolver}` diff --git a/yarn.lock b/yarn.lock index fad1122..ebdeb97 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1264,10 +1264,10 @@ vinyl-fs "^3.0.3" yargs "^16.2.0" -"@orioro/validate-type@^1.3.1": - version "1.3.1" - resolved "https://registry.yarnpkg.com/@orioro/validate-type/-/validate-type-1.3.1.tgz#9f667bc189782e30daf306da7aa9d2e272504ff8" - integrity sha512-pNnpsEIPms9COh2I4dMqfqrIhtL7b7lv0tFtkV3kYjxRE22G35M3GqgYXnYxPPcOt+nXcCv02rCwtuAJmoQ2Pw== +"@orioro/validate-type@^1.4.0": + version "1.4.0" + resolved "https://registry.yarnpkg.com/@orioro/validate-type/-/validate-type-1.4.0.tgz#2a60a7cbfcb540d81654417a10c8404e3e98f89b" + integrity sha512-z9JdQxLUFbrIMZCBy2Xwzn82VDiR5wonNBDgAtCI4vHi2wJMODNv6P7oIk77K/fmq60RKJ2IElduV9AP4dm+4Q== "@rollup/plugin-babel@^5.2.0": version "5.2.3"