Skip to content

Commit

Permalink
feat: add $isType experssion and typeExpressions(types) utility function
Browse files Browse the repository at this point in the history
Add functionality to allow generating custom type expressions
  • Loading branch information
simonfan committed Feb 27, 2021
1 parent 727c5eb commit f71ff16
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 121 deletions.
2 changes: 2 additions & 0 deletions .README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
npm install @orioro/expression
```

Framework of expression interpreters.

# Use cases

## Data querying
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
npm install @orioro/expression
```

Framework of expression interpreters.

# Use cases

## Data querying
Expand Down Expand Up @@ -691,6 +693,7 @@ through Catastrophic backtracking, for future study and reference:
## Type

- [`$type(valueExp)`](#typevalueexp)
- [`$isType(type, value)`](#istypetype-value)


##### `$type(valueExp)`
Expand All @@ -715,6 +718,12 @@ through Catastrophic backtracking, for future study and reference:
- weakmap
- weakset

##### `$isType(type, value)`

- `type` {ExpectedType}
- `value` {*}
- Returns: {Boolean}


## Value

Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"name": "@orioro/expression",
"version": "0.0.0-semantic-release",
"description": "Framework of expression interpreters",
"keywords": ["expression", "eval", "conditional"],
"keywords": [
"expression",
"eval",
"conditional"
],
"homepage": "https://github.com/orioro/node-expression",
"bugs": "https://github.com/orioro/node-expression/issues",
"repository": {
Expand Down Expand Up @@ -38,6 +42,7 @@
"@babel/core": "^7.11.5",
"@babel/preset-env": "^7.11.5",
"@babel/preset-typescript": "^7.10.4",
"@orioro/jest-util": "^1.2.0",
"@orioro/readme": "^1.0.1",
"@rollup/plugin-babel": "^5.2.0",
"@rollup/plugin-commonjs": "^15.0.0",
Expand All @@ -60,7 +65,7 @@
"typescript": "^4.0.2"
},
"dependencies": {
"@orioro/validate-type": "^2.0.0",
"@orioro/typing": "^3.0.0",
"lodash": "^4.17.20"
},
"config": {
Expand Down
4 changes: 4 additions & 0 deletions src/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,17 @@ Array [
"$stringToLowerCase",
"$stringInterpolate",
"STRING_EXPRESSIONS",
"typeExpressions",
"$type",
"$isType",
"TYPE_EXPRESSIONS",
"$$VALUE",
"$value",
"$literal",
"$evaluate",
"VALUE_EXPRESSIONS",
"TypeAlternatives",
"TypeMap",
"isExpression",
"evaluate",
"evaluateTyped",
Expand Down
2 changes: 1 addition & 1 deletion src/expression.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { validateType, ExpectedType } from '@orioro/validate-type'
import { validateType, ExpectedType } from '@orioro/typing'

import { isPlainObject } from 'lodash'

Expand Down
2 changes: 1 addition & 1 deletion src/expressions/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
isExpression,
} from '../expression'
import { EvaluationContext, Expression } from '../types'
import { validateType } from '@orioro/validate-type'
import { validateType } from '@orioro/typing'

export const $$INDEX = ['$value', '$$INDEX']
export const $$ARRAY = ['$value', '$$ARRAY']
Expand Down
2 changes: 1 addition & 1 deletion src/expressions/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { interpreter } from '../expression'
import { get } from 'lodash'
import { PlainObject } from '../types'

import { getType } from '@orioro/validate-type'
import { getType } from '@orioro/typing'

const stringifyValue = (value) => {
switch (getType(value)) {
Expand Down
171 changes: 87 additions & 84 deletions src/expressions/type.spec.ts
Original file line number Diff line number Diff line change
@@ -1,106 +1,109 @@
import { evaluate } from '../expression'
import { $value } from './value'
import { TYPE_EXPRESSIONS } from './type'
import { TYPE_EXPRESSIONS, typeExpressions } from './type'
import { testCases } from '@orioro/jest-util'

const interpreters = {
$value,
...TYPE_EXPRESSIONS,
}

describe('$type', () => {
test('string', () => {
expect(
testCases(
[
['some string', 'string'],
[10, 'number'],
[true, 'boolean'],
[[], 'array'],
[{}, 'object'],
[new Map(), 'map'],
[new Set(), 'set'],
[Symbol(), 'symbol'],
],
(value) =>
evaluate(
{
interpreters,
scope: { $$VALUE: 'some string' },
scope: { $$VALUE: value },
},
['$type']
)
).toEqual('string')
})

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

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

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

test('object', () => {
expect(
evaluate(
{
interpreters,
scope: { $$VALUE: {} },
},
['$type']
)
).toEqual('object')
})
describe('$isType', () => {
testCases(
[
['string', 'Some str', true],
['string', 9, false],
['number', 9, true],
],
(type, value) =>
evaluate({ interpreters, scope: { $$VALUE: value } }, ['$isType', type]),
'$isType'
)
})

test('map', () => {
expect(
evaluate(
{
interpreters,
scope: { $$VALUE: new Map() },
},
['$type']
)
).toEqual('map')
describe('typeExpressions(types)', () => {
const [$customType, $customIsType] = typeExpressions({
numericOnlyString: (value) =>
typeof value === 'string' && /^[0-9]+$/.test(value),
alphaOnlyString: (value) =>
typeof value === 'string' && /^[a-zA-Z]+$/.test(value),
alphaNumericString: (value) =>
typeof value === 'string' && /^[a-zA-Z0-9]+$/.test(value),
normalString: (value) => typeof value === 'string',
})

test('set', () => {
expect(
evaluate(
{
interpreters,
scope: { $$VALUE: new Set() },
},
['$type']
)
).toEqual('set')
describe('$customType', () => {
testCases(
[
['abc123', 'alphaNumericString'],
['abc123-', 'normalString'],
['abc', 'alphaOnlyString'],
['abc123', 'alphaNumericString'],
['123', 'numericOnlyString'],
],
(value) =>
evaluate(
{
interpreters: {
...interpreters,
$customType,
$customIsType,
},
scope: { $$VALUE: value },
},
['$customType']
),
'$customType'
)
})

test('symbol', () => {
expect(
evaluate(
{
interpreters,
scope: { $$VALUE: Symbol() },
},
['$type']
)
).toEqual('symbol')
describe('$customIsType', () => {
testCases(
[
['alphaNumericString', 'abc123', true],
['alphaNumericString', 'abc123-', false],
['alphaOnlyString', 'abc', true],
['alphaOnlyString', 'abc123', false],
['numericOnlyString', '123', true],
['numericOnlyString', 'abc123', false],
],
(type, value) =>
evaluate(
{
interpreters: {
...interpreters,
$customType,
$customIsType,
},
scope: { $$VALUE: value },
},
['$customIsType', type]
),
'$customIsType'
)
})
})
Loading

0 comments on commit f71ff16

Please sign in to comment.