Skip to content

Commit dddef34

Browse files
committed
Add additional coercion rules
1 parent 688d16f commit dddef34

File tree

7 files changed

+71
-3
lines changed

7 files changed

+71
-3
lines changed

compiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function isDeepSync (method, engine) {
112112

113113
if (Array.isArray(method)) return method.every(i => isDeepSync(i, engine))
114114

115-
if (typeof method === 'object') {
115+
if (method && typeof method === 'object') {
116116
const func = Object.keys(method)[0]
117117

118118
const lower = method[func]

defaultMethods.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function isSyncDeep (method, engine, buildState) {
5252

5353
const defaultMethods = {
5454
'+': (data) => {
55+
if (!data) return 0
5556
if (typeof data === 'string') return +data
5657
if (typeof data === 'number') return +data
5758
if (typeof data === 'boolean') return +data
@@ -70,6 +71,7 @@ const defaultMethods = {
7071
return res
7172
},
7273
'-': (data) => {
74+
if (!data) return 0
7375
if (typeof data === 'string') return -data
7476
if (typeof data === 'number') return -data
7577
if (typeof data === 'boolean') return -data

suites/divide.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@
129129
"result": 0,
130130
"data": null
131131
},
132+
{
133+
"description": "Divide Operator with Single Operand, Direct (Empty String)",
134+
"rule": { "/": "" },
135+
"result": 0,
136+
"data": null
137+
},
138+
{
139+
"description": "Divide Operator with a Single Operand, Direct (null)",
140+
"rule": { "/": null },
141+
"result": 0,
142+
"data": null
143+
},
132144
{
133145
"description": "Divide with val",
134146
"rule": { "/": [{ "val": "x" }, { "val": "y" }] },

suites/minus.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
},
3939
{
4040
"description": "Subtraction with Multiple Value Types",
41-
"rule": { "-": [1, "2", 3, "4", true, false] },
41+
"rule": { "-": [1, "2", 3, "4", "", true, false, null] },
4242
"result": -9,
4343
"data": null
4444
},
@@ -97,6 +97,18 @@
9797
"result": 0,
9898
"data": null
9999
},
100+
{
101+
"description": "Minus Operator with Single Operand, Direct (Empty String)",
102+
"rule": { "-": "" },
103+
"result": 0,
104+
"data": null
105+
},
106+
{
107+
"description": "Minus Operator with a Single Operand, Direct (null)",
108+
"rule": { "-": null },
109+
"result": 0,
110+
"data": null
111+
},
100112
{
101113
"description": "Subtraction with val",
102114
"rule": { "-": [{ "val": "x" }, { "val": "y" }] },

suites/modulo.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@
140140
"result": 0,
141141
"data": null
142142
},
143+
{
144+
"description": "Modulo Operator with Single Operand, Direct (Empty String)",
145+
"rule": { "%": "" },
146+
"result": 0,
147+
"data": null
148+
},
149+
{
150+
"description": "Modulo Operator with a Single Operand, Direct (null)",
151+
"rule": { "%": null },
152+
"result": 0,
153+
"data": null
154+
},
143155
{
144156
"description": "Modulo with val",
145157
"rule": { "%": [{ "val": "x" }, { "val": "y" }] },

suites/multiply.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848
"result": 1,
4949
"data": null
5050
},
51+
{
52+
"description": "Multiply with Multiple Value Types (3)",
53+
"rule": { "*": ["1", null] },
54+
"result": 0,
55+
"data": null
56+
},
5157
{
5258
"description": "Multiply with Single Operand (Number)",
5359
"rule": { "*": [1] },
@@ -121,6 +127,18 @@
121127
"result": 0,
122128
"data": null
123129
},
130+
{
131+
"description": "Multiply Operator with Single Operand, Direct (Empty String)",
132+
"rule": { "*": "" },
133+
"result": 0,
134+
"data": null
135+
},
136+
{
137+
"description": "Multiply Operator with a Single Operand, Direct (null)",
138+
"rule": { "*": null },
139+
"result": 0,
140+
"data": null
141+
},
124142
{
125143
"description": "Multiply with val",
126144
"rule": { "*": [{ "val": "x" }, { "val": "y" }] },

suites/plus.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
},
3939
{
4040
"description": "Addition with Multiple Value Types",
41-
"rule": { "+": [1, "2", 3, "4", true, false] },
41+
"rule": { "+": [1, "2", 3, "4", "", true, false, null] },
4242
"result": 11,
4343
"data": null
4444
},
@@ -117,6 +117,18 @@
117117
"result": 0,
118118
"data": null
119119
},
120+
{
121+
"description": "Plus Operator with Single Operand, Direct (Empty String)",
122+
"rule": { "+": "" },
123+
"result": 0,
124+
"data": null
125+
},
126+
{
127+
"description": "Plus Operator with a Single Operand, Direct (null)",
128+
"rule": { "+": null },
129+
"result": 0,
130+
"data": null
131+
},
120132
{
121133
"description": "Addition with val",
122134
"rule": { "+": [{ "val": "x" }, { "val": "y" }] },

0 commit comments

Comments
 (0)