Skip to content

Commit 5d5c38d

Browse files
committed
Add another check, and throws
1 parent 3c6d8fa commit 5d5c38d

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

defaultMethods.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ const defaultMethods = {
9494
if (type === 'NaN') return Number.NaN
9595
return { error: type }
9696
},
97+
throws: (item) => {
98+
if (Array.isArray(item)) item = item[0]
99+
if (Number.isNaN(item)) throw new Error('NaN was returned from expression')
100+
if (item && item.error) throw item.error
101+
return item
102+
},
97103
max: (data) => Math.max(...data),
98104
min: (data) => Math.min(...data),
99105
in: ([item, array]) => (array || []).includes(item),
@@ -962,7 +968,7 @@ defaultMethods['!!'].compile = function (data, buildState) {
962968
defaultMethods.none.deterministic = defaultMethods.some.deterministic
963969

964970
// @ts-ignore Allowing a optimizeUnary attribute that can be used for performance optimizations
965-
defaultMethods['+'].optimizeUnary = defaultMethods['-'].optimizeUnary = defaultMethods['!'].optimizeUnary = defaultMethods['!!'].optimizeUnary = defaultMethods.cat.optimizeUnary = defaultMethods.error.optimizeUnary = true
971+
defaultMethods['+'].optimizeUnary = defaultMethods['-'].optimizeUnary = defaultMethods['!'].optimizeUnary = defaultMethods['!!'].optimizeUnary = defaultMethods.cat.optimizeUnary = defaultMethods.error.optimizeUnary = defaultMethods.throws.optimizeUnary = true
966972

967973
export default {
968974
...defaultMethods,

suites/plus.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,11 @@
158158
"rule": { "+": [[1], 1] },
159159
"result": { "error": "NaN" },
160160
"data": null
161+
},
162+
{
163+
"description": "Addition with Object produces NaN",
164+
"rule": { "+": [{ "val": "x" }, 1] },
165+
"result": { "error": "NaN" },
166+
"data": { "x": {} }
161167
}
162168
]

0 commit comments

Comments
 (0)