Skip to content

Commit edbe00e

Browse files
committed
Switch minus operator to error on zero arguments
1 parent 02dc0aa commit edbe00e

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

defaultMethods.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const defaultMethods = {
103103
if (typeof data === 'boolean') return precoerceNumber(-data)
104104
if (typeof data === 'object' && !Array.isArray(data)) throw NaN
105105
if (data[0] && typeof data[0] === 'object') throw NaN
106-
if (data.length === 0) return 0
106+
if (data.length === 0) throw INVALID_ARGUMENTS
107107
if (data.length === 1) return -data[0]
108108
let res = data[0]
109109
for (let i = 1; i < data.length; i++) {
@@ -1019,11 +1019,11 @@ defaultMethods.in.compile = function (data, buildState) {
10191019
// @ts-ignore Allow custom attribute
10201020
defaultMethods['-'].compile = function (data, buildState) {
10211021
if (Array.isArray(data)) {
1022-
if (data.length === 0) return '(-0)'
1022+
if (data.length === 0) throw INVALID_ARGUMENTS
10231023
return `${data.length === 1 ? '-' : ''}precoerceNumber(${data.map(i => numberCoercion(i, buildState)).join(' - ')})`
10241024
}
10251025
if (typeof data === 'string' || typeof data === 'number') return `(-${buildString(data, buildState)})`
1026-
return buildState.compile`(Array.isArray(prev = ${data}) ? prev.length === 0 ? 0 : prev.length === 1 ? -precoerceNumber(prev[0]) : prev.reduce((a,b) => (+precoerceNumber(a))-(+precoerceNumber(b))) : -precoerceNumber(prev))`
1026+
return buildState.compile`(Array.isArray(prev = ${data}) ? prev.length === 1 ? -precoerceNumber(prev[0]) : assertSize(prev, 1).reduce((a,b) => (+precoerceNumber(a))-(+precoerceNumber(b))) : -precoerceNumber(prev))`
10271027
}
10281028
// @ts-ignore Allow custom attribute
10291029
defaultMethods['/'].compile = function (data, buildState) {

suites/minus.extra.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
"description": "Minus with dynamic empty array",
44
"rule": { "-": { "preserve": [] }},
5-
"result": 0,
5+
"error": { "type": "Invalid Arguments" },
66
"data": null
77
},
88
{

suites/minus.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@
5555
"data": null
5656
},
5757
{
58-
"description": "Minus with zero operands is zero",
58+
"description": "Minus with zero operands is an error",
5959
"rule": { "-": [] },
60-
"result": 0,
60+
"error": { "type": "Invalid Arguments" },
6161
"data": null
6262
},
6363
{

0 commit comments

Comments
 (0)