Skip to content

Commit

Permalink
JS: Define expression.add(…) to build new expression
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed Jul 19, 2023
1 parent cfecaf5 commit e3b32c4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/expressions/lib/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,12 @@ export class Expression {
matches (schema) {
return this.validate(schema).valid
}

add (expression) {
if (this.schema.maxItems) {
return Expression.build({ All: [this, expression] })
} else {
return this.clone({ args: [...this.args, expression] })
}
}
}
17 changes: 17 additions & 0 deletions packages/expressions/test/expression.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,21 @@ describe('Expression', () => {
expect(expression.validate().valid).toBe(false)
})
})

describe('add', () => {
test('Any returns new expression with added arg', () => {
const expression = Expression.build({ Any: [] }).add(true)
expect(expression.value).toEqual({ Any: [true] })
})

test('Max returns new expression with added arg', () => {
const expression = Expression.build({ Max: [1] }).add(2)
expect(expression.value).toEqual({ Max: [1, 2] })
})

test('Equal returns new expression wrapped in All', () => {
const expression = Expression.build({ Equal: [1, 1] }).add(false)
expect(expression.value).toEqual({ All: [{ Equal: [1, 1] }, false] })
})
})
})

0 comments on commit e3b32c4

Please sign in to comment.