Skip to content

Commit

Permalink
feat(type): make $type expression return extended type list
Browse files Browse the repository at this point in the history
  • Loading branch information
simonfan committed Feb 12, 2021
1 parent b29e154 commit 6d11dff
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
35 changes: 35 additions & 0 deletions src/expressions/type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,39 @@ describe('$type', () => {
scope: { $$VALUE: true }
}, ['$type'])).toEqual('boolean')
})

test('array', () => {
expect(evaluate({
interpreters,
scope: { $$VALUE: [] }
}, ['$type'])).toEqual('array')
})

test('object', () => {
expect(evaluate({
interpreters,
scope: { $$VALUE: {} }
}, ['$type'])).toEqual('object')
})

test('map', () => {
expect(evaluate({
interpreters,
scope: { $$VALUE: new Map() }
}, ['$type'])).toEqual('map')
})

test('set', () => {
expect(evaluate({
interpreters,
scope: { $$VALUE: new Set() }
}, ['$type'])).toEqual('set')
})

test('symbol', () => {
expect(evaluate({
interpreters,
scope: { $$VALUE: Symbol() }
}, ['$type'])).toEqual('symbol')
})
})
26 changes: 21 additions & 5 deletions src/expressions/type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
evaluate
} from '../expression'
import { evaluate } from '../expression'
import { getType } from '@orioro/validate-type'

import {
EvaluationContext,
Expand All @@ -14,12 +13,29 @@ import { $$VALUE } from './value'
*
* @function $type
* @param {*} valueExp
* @returns {string} type
* @returns {string} type Possible values:
* - string
* - regexp
* - number
* - bigint
* - nan
* - null
* - undefined
* - boolean
* - function
* - object
* - array
* - date
* - symbol
* - map
* - set
* - weakmap
* - weakset
*/
export const $type = (
context:EvaluationContext,
valueExp:Expression = $$VALUE
) => typeof (evaluate(context, valueExp))
) => getType(evaluate(context, valueExp))

export const TYPE_EXPRESSIONS = {
$type
Expand Down

0 comments on commit 6d11dff

Please sign in to comment.