Skip to content

Commit afdeb79

Browse files
Merge pull request #32 from TotalTechGeek/bugfix/resolve-issue-31
Resolve an issue with the unary operators.
2 parents 4fcbe2e + 262ab5b commit afdeb79

File tree

5 files changed

+77
-49
lines changed

5 files changed

+77
-49
lines changed

bench/compatible.json

+61
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,15 @@
375375
{},
376376
false
377377
],
378+
[
379+
{
380+
"!": [
381+
false
382+
]
383+
},
384+
{},
385+
true
386+
],
378387
[
379388
{
380389
"!": false
@@ -1455,6 +1464,24 @@
14551464
null,
14561465
"apple"
14571466
],
1467+
[
1468+
{
1469+
"!": [
1470+
0
1471+
]
1472+
},
1473+
{},
1474+
true
1475+
],
1476+
[
1477+
{
1478+
"!!": [
1479+
0
1480+
]
1481+
},
1482+
{},
1483+
false
1484+
],
14581485
[
14591486
{
14601487
"and": [
@@ -1475,6 +1502,24 @@
14751502
{},
14761503
true
14771504
],
1505+
[
1506+
{
1507+
"!": [
1508+
""
1509+
]
1510+
},
1511+
{},
1512+
true
1513+
],
1514+
[
1515+
{
1516+
"!!": [
1517+
""
1518+
]
1519+
},
1520+
{},
1521+
false
1522+
],
14781523
[
14791524
{
14801525
"and": [
@@ -3503,5 +3548,21 @@
35033548
"items": []
35043549
},
35053550
false
3551+
],
3552+
[
3553+
{
3554+
"!": [
3555+
false
3556+
]
3557+
},
3558+
{},
3559+
true
3560+
],
3561+
[
3562+
{
3563+
"!!": false
3564+
},
3565+
{},
3566+
false
35063567
]
35073568
]

bench/incompatible.json

-45
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
[
2-
[
3-
{
4-
"!": [
5-
false
6-
]
7-
},
8-
{},
9-
true
10-
],
112
[
123
{
134
"if": []
@@ -91,42 +82,6 @@
9182
{},
9283
true
9384
],
94-
[
95-
{
96-
"!": [
97-
0
98-
]
99-
},
100-
{},
101-
true
102-
],
103-
[
104-
{
105-
"!!": [
106-
0
107-
]
108-
},
109-
{},
110-
false
111-
],
112-
[
113-
{
114-
"!": [
115-
""
116-
]
117-
},
118-
{},
119-
true
120-
],
121-
[
122-
{
123-
"!!": [
124-
""
125-
]
126-
},
127-
{},
128-
false
129-
],
13085
[
13186
{
13287
"if": [

bench/tests.json

+10
Original file line numberDiff line numberDiff line change
@@ -4033,5 +4033,15 @@
40334033
},
40344034
false
40354035
],
4036+
[
4037+
{ "!": [false] },
4038+
{},
4039+
true
4040+
],
4041+
[
4042+
{ "!!": false },
4043+
{},
4044+
false
4045+
],
40364046
"EOF"
40374047
]

defaultMethods.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,9 @@ const defaultMethods = {
365365
},
366366
traverse: false
367367
},
368-
not: (value) => !value,
369-
'!': (value) => !value,
370-
'!!': (value) => Boolean(value),
368+
not: (value) => Array.isArray(value) ? !value[0] : !value,
369+
'!': (value) => Array.isArray(value) ? !value[0] : !value,
370+
'!!': (value) => Boolean(Array.isArray(value) ? value[0] : value),
371371
cat: (arr) => (typeof arr === 'string' ? arr : arr.join('')),
372372
keys: (obj) => Object.keys(obj),
373373
eachKey: {
@@ -793,10 +793,12 @@ defaultMethods.not.compile = defaultMethods['!'].compile = function (
793793
data,
794794
buildState
795795
) {
796+
if (Array.isArray(data)) return `(!(${buildString(data[0], buildState)}))`
796797
return `(!(${buildString(data, buildState)}))`
797798
}
798799
// @ts-ignore Allow custom attribute
799800
defaultMethods['!!'].compile = function (data, buildState) {
801+
if (Array.isArray(data)) return `(!!(${buildString(data[0], buildState)}))`
800802
return `(!!(${buildString(data, buildState)}))`
801803
}
802804
defaultMethods.none.deterministic = defaultMethods.some.deterministic

package.json

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

0 commit comments

Comments
 (0)