Skip to content

Commit 02dc0aa

Browse files
committed
Modified multiply with no arguments to be 1
1 parent 5dca1ae commit 02dc0aa

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

defaultMethods.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ const defaultMethods = {
7171
return res
7272
},
7373
'*': (data) => {
74-
// eslint-disable-next-line no-throw-literal
75-
if (data.length === 0) throw INVALID_ARGUMENTS
74+
if (data.length === 0) return 1
7675
let res = 1
7776
for (let i = 0; i < data.length; i++) {
7877
if (data[i] && typeof data[i] === 'object') throw NaN
@@ -83,7 +82,7 @@ const defaultMethods = {
8382
},
8483
'/': (data) => {
8584
if (data[0] && typeof data[0] === 'object') throw NaN
86-
// eslint-disable-next-line no-throw-literal
85+
8786
if (data.length === 0) throw INVALID_ARGUMENTS
8887
if (data.length === 1) {
8988
if (!+data[0] || (data[0] && typeof data[0] === 'object')) throw NaN
@@ -116,7 +115,7 @@ const defaultMethods = {
116115
},
117116
'%': (data) => {
118117
if (data[0] && typeof data[0] === 'object') throw NaN
119-
// eslint-disable-next-line no-throw-literal
118+
120119
if (data.length < 2) throw INVALID_ARGUMENTS
121120
let res = +data[0]
122121
for (let i = 1; i < data.length; i++) {
@@ -129,6 +128,7 @@ const defaultMethods = {
129128
throw: (type) => {
130129
if (Array.isArray(type)) type = type[0]
131130
if (typeof type === 'object') throw type
131+
132132
// eslint-disable-next-line no-throw-literal
133133
throw { type }
134134
},
@@ -1042,11 +1042,10 @@ defaultMethods['/'].compile = function (data, buildState) {
10421042
// @ts-ignore Allow custom attribute
10431043
defaultMethods['*'].compile = function (data, buildState) {
10441044
if (Array.isArray(data)) {
1045-
// eslint-disable-next-line no-throw-literal
1046-
if (data.length === 0) throw INVALID_ARGUMENTS
1045+
if (data.length === 0) return '1'
10471046
return `precoerceNumber(${data.map(i => numberCoercion(i, buildState)).join(' * ')})`
10481047
}
1049-
return `assertSize(${buildString(data, buildState)}, 1).reduce((a,b) => (+precoerceNumber(a))*(+precoerceNumber(b)))`
1048+
return `(${buildString(data, buildState)}).reduce((a,b) => (+precoerceNumber(a))*(+precoerceNumber(b)), 1)`
10501049
}
10511050

10521051
// @ts-ignore Allow custom attribute

suites/multiply.extra.json

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

suites/multiply.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@
8585
"data": null
8686
},
8787
{
88-
"description": "Multiply with zero operands is an error",
88+
"description": "Multiply with zero operands is 1 (Product)",
8989
"rule": { "*": [] },
90-
"error": { "type": "Invalid Arguments" },
90+
"result": 1,
9191
"data": null
9292
},
9393
{

0 commit comments

Comments
 (0)