Skip to content

Commit ac0c30e

Browse files
committed
Export build function separately
1 parent d95f2ea commit ac0c30e

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

lib/expression.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
import { v4 as uuidv4 } from 'uuid'
22
import { Schema } from './schemas'
33

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+
}
98

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) {
1913
throw new TypeError(`Invalid expression: ${JSON.stringify(expression)}`)
2014
}
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)}`)
2119
}
20+
}
21+
22+
export class Expression {
23+
static build = build
2224

2325
constructor ({ id = uuidv4(), parent = undefined }) {
2426
this.id = id

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export { Schema, schemas, BaseURI } from './schemas'
2-
export { Expression, Function, Constant } from './expression'
2+
export { build, Expression, Function, Constant } from './expression'
33
export { default as examples } from '../examples'

0 commit comments

Comments
 (0)