Skip to content

Commit 596e330

Browse files
committed
Resolve issue with negative Infinity
1 parent e547630 commit 596e330

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

async.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,30 @@ modes.forEach((logic) => {
4040
})
4141
expect(answer).toBe(-4)
4242
})
43+
44+
test('it should be able to negate a single number', async () => {
45+
const answer = await logic.run({
46+
'-': [1]
47+
})
48+
49+
expect(answer).toBe(-1)
50+
})
51+
52+
test('it should be able to negate a single number in an array', async () => {
53+
const answer = await logic.run({
54+
'-': 1
55+
})
56+
57+
expect(answer).toBe(-1)
58+
})
59+
60+
test('it should be able to negate Infinity', async () => {
61+
const answer = await logic.run({
62+
'-': Infinity
63+
})
64+
65+
expect(answer).toBe(-Infinity)
66+
})
4367
})
4468

4569
describe('*', () => {

build.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,24 @@ function timeout (n, x) {
4040
expect(await f()).toEqual(-5)
4141
})
4242

43+
test('Minus operator w/ Infinity', async () => {
44+
const f = await logic.build({ '-': Infinity })
45+
46+
expect(await f()).toEqual(-Infinity)
47+
})
48+
4349
test('Minus operator w/ array w/ variable input', async () => {
4450
const f = await logic.build({ '-': [{ preserve: 5 }] })
4551

4652
expect(await f()).toEqual(-5)
4753
})
4854

55+
test('Minus operator w/ array w/ variable input (Infinity)', async () => {
56+
const f = await logic.build({ '-': [{ preserve: Infinity }] })
57+
58+
expect(await f()).toEqual(-Infinity)
59+
})
60+
4961
test('Minus operator w/ variable input', async () => {
5062
const f = await logic.build({ '-': { preserve: '5' } })
5163

compiler.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import asyncIterators from './async_iterators.js'
3737
* @returns
3838
*/
3939
function isPrimitive (x) {
40+
if (typeof x === 'number' && (x === Infinity || x === -Infinity || Number.isNaN(x))) return false
4041
return (
4142
x === null ||
4243
x === undefined ||

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-logic-engine",
3-
"version": "1.1.21",
3+
"version": "1.1.22",
44
"description": "Construct complex rules with JSON & process them.",
55
"main": "./dist/cjs/index.js",
66
"module": "./dist/esm/index.js",

test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,30 @@ modes.forEach((logic) => {
4444

4545
expect(answer).toBe(-4)
4646
})
47+
48+
test('it should be able to negate a single number', () => {
49+
const answer = logic.run({
50+
'-': [1]
51+
})
52+
53+
expect(answer).toBe(-1)
54+
})
55+
56+
test('it should be able to negate a single number in an array', () => {
57+
const answer = logic.run({
58+
'-': 1
59+
})
60+
61+
expect(answer).toBe(-1)
62+
})
63+
64+
test('it should be able to negate Infinity', () => {
65+
const answer = logic.run({
66+
'-': Infinity
67+
})
68+
69+
expect(answer).toBe(-Infinity)
70+
})
4771
})
4872

4973
describe('*', () => {

0 commit comments

Comments
 (0)