Skip to content

Commit

Permalink
Merge pull request #32 from TotalTechGeek/bugfix/resolve-issue-31
Browse files Browse the repository at this point in the history
Resolve an issue with the unary operators.
  • Loading branch information
TotalTechGeek committed May 23, 2024
2 parents 4fcbe2e + 262ab5b commit afdeb79
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 49 deletions.
61 changes: 61 additions & 0 deletions bench/compatible.json
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,15 @@
{},
false
],
[
{
"!": [
false
]
},
{},
true
],
[
{
"!": false
Expand Down Expand Up @@ -1455,6 +1464,24 @@
null,
"apple"
],
[
{
"!": [
0
]
},
{},
true
],
[
{
"!!": [
0
]
},
{},
false
],
[
{
"and": [
Expand All @@ -1475,6 +1502,24 @@
{},
true
],
[
{
"!": [
""
]
},
{},
true
],
[
{
"!!": [
""
]
},
{},
false
],
[
{
"and": [
Expand Down Expand Up @@ -3503,5 +3548,21 @@
"items": []
},
false
],
[
{
"!": [
false
]
},
{},
true
],
[
{
"!!": false
},
{},
false
]
]
45 changes: 0 additions & 45 deletions bench/incompatible.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
[
[
{
"!": [
false
]
},
{},
true
],
[
{
"if": []
Expand Down Expand Up @@ -91,42 +82,6 @@
{},
true
],
[
{
"!": [
0
]
},
{},
true
],
[
{
"!!": [
0
]
},
{},
false
],
[
{
"!": [
""
]
},
{},
true
],
[
{
"!!": [
""
]
},
{},
false
],
[
{
"if": [
Expand Down
10 changes: 10 additions & 0 deletions bench/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -4033,5 +4033,15 @@
},
false
],
[
{ "!": [false] },
{},
true
],
[
{ "!!": false },
{},
false
],
"EOF"
]
8 changes: 5 additions & 3 deletions defaultMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,9 @@ const defaultMethods = {
},
traverse: false
},
not: (value) => !value,
'!': (value) => !value,
'!!': (value) => Boolean(value),
not: (value) => Array.isArray(value) ? !value[0] : !value,
'!': (value) => Array.isArray(value) ? !value[0] : !value,
'!!': (value) => Boolean(Array.isArray(value) ? value[0] : value),
cat: (arr) => (typeof arr === 'string' ? arr : arr.join('')),
keys: (obj) => Object.keys(obj),
eachKey: {
Expand Down Expand Up @@ -793,10 +793,12 @@ defaultMethods.not.compile = defaultMethods['!'].compile = function (
data,
buildState
) {
if (Array.isArray(data)) return `(!(${buildString(data[0], buildState)}))`
return `(!(${buildString(data, buildState)}))`
}
// @ts-ignore Allow custom attribute
defaultMethods['!!'].compile = function (data, buildState) {
if (Array.isArray(data)) return `(!!(${buildString(data[0], buildState)}))`
return `(!!(${buildString(data, buildState)}))`
}
defaultMethods.none.deterministic = defaultMethods.some.deterministic
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-logic-engine",
"version": "1.3.2",
"version": "1.3.3",
"description": "Construct complex rules with JSON & process them.",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand Down

0 comments on commit afdeb79

Please sign in to comment.