|
1 | 1 | import { v4 as uuidv4 } from 'uuid' |
2 | 2 | import { Schema } from './schemas' |
3 | 3 |
|
4 | | -export class Expression { |
5 | | - static build (expression, attrs = {}) { |
6 | | - if (expression instanceof Function || expression instanceof Constant) { |
7 | | - return expression.clone(attrs) |
8 | | - } |
| 4 | +export function build (expression, attrs = {}) { |
| 5 | + if (expression instanceof Function || expression instanceof Constant) { |
| 6 | + return expression.clone(attrs) |
| 7 | + } |
9 | 8 |
|
10 | | - if (['number', 'string', 'boolean'].includes(typeof expression) || expression === null) { |
11 | | - return new Constant({ value: expression, ...attrs }) |
12 | | - } else if (typeof expression === 'object') { |
13 | | - if (Object.keys(expression).length !== 1) { |
14 | | - throw new TypeError(`Invalid expression: ${JSON.stringify(expression)}`) |
15 | | - } |
16 | | - const name = Object.keys(expression)[0] |
17 | | - return new Function({ name, args: expression[name] }) |
18 | | - } else { |
| 9 | + if (['number', 'string', 'boolean'].includes(typeof expression) || expression === null) { |
| 10 | + return new Constant({ value: expression, ...attrs }) |
| 11 | + } else if (typeof expression === 'object') { |
| 12 | + if (Object.keys(expression).length !== 1) { |
19 | 13 | throw new TypeError(`Invalid expression: ${JSON.stringify(expression)}`) |
20 | 14 | } |
| 15 | + const name = Object.keys(expression)[0] |
| 16 | + return new Function({ name, args: expression[name] }) |
| 17 | + } else { |
| 18 | + throw new TypeError(`Invalid expression: ${JSON.stringify(expression)}`) |
21 | 19 | } |
| 20 | +} |
| 21 | + |
| 22 | +export class Expression { |
| 23 | + static build = build |
22 | 24 |
|
23 | 25 | constructor ({ id = uuidv4(), parent = undefined }) { |
24 | 26 | this.id = id |
|
0 commit comments