Skip to content

Resolve an issue with the unary operators. #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading