Skip to content

Commit

Permalink
fix: update @orioro/validate-type and interpreter(...) typing to matc…
Browse files Browse the repository at this point in the history
…h ExpectedType
  • Loading branch information
simonfan committed Feb 24, 2021
1 parent 4c0fe4d commit cfb3c8c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
38 changes: 13 additions & 25 deletions src/expression.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { validateType } from '@orioro/validate-type'
import { validateType, ExpectedType } from '@orioro/validate-type'

import { isPlainObject } from 'lodash'

import {
Expression,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}`
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit cfb3c8c

Please sign in to comment.