Skip to content

Commit

Permalink
Fixed #1418: missing whitespace when stringifying an expression conta…
Browse files Browse the repository at this point in the history
…ining "not"
  • Loading branch information
josdejong committed Feb 27, 2019
1 parent 6fe5c46 commit d9deed9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Not yet published, version 5.6.0

- Upgrade decimal.js to v10.1.0 (#1421).
- Fixed #1418: missing whitespace when stringifying an expression
containing "not".


# 2019-02-20, version 5.5.0
Expand Down
7 changes: 5 additions & 2 deletions src/expression/node/OperatorNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,13 @@ function factory (type, config, load, typed) {
operand = '(' + operand + ')'
}

// for example for "not", we want a space between operand and argument
const opIsNamed = /[a-zA-Z]+/.test(this.op)

if (assoc === 'right') { // prefix operator
return this.op + operand
return this.op + (opIsNamed ? ' ' : '') + operand
} else if (assoc === 'left') { // postfix
return operand + this.op
return operand + (opIsNamed ? ' ' : '') + this.op
}

// fall back to postfix
Expand Down
11 changes: 11 additions & 0 deletions test/expression/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1949,6 +1949,17 @@ describe('parse', function () {
assert.strictEqual(parse('1/2a').toString(), '1 / 2 a')
})

it('should correctly stringify named operators', function () {
assert.strictEqual(parse('7 mod 3').toString(), '7 mod 3')
assert.strictEqual(parse('5 inch to cm').toString(), '5 inch to cm')
assert.strictEqual(parse('5 inch in cm').toString(), '5 inch in cm')
assert.strictEqual(parse('false and true').toString(), 'false and true')
assert.strictEqual(parse('false xor true').toString(), 'false xor true')
assert.strictEqual(parse('false or true').toString(), 'false or true')
assert.strictEqual(parse('not true').toString(), 'not true')
assert.strictEqual(parse('5!').toString(), '5!')
})

it('should correctly stringify an index with dot notation', function () {
assert.strictEqual(parse('A[2]').toString(), 'A[2]')
assert.strictEqual(parse('a["b"]').toString(), 'a["b"]')
Expand Down

0 comments on commit d9deed9

Please sign in to comment.