Skip to content

Commit f9f565c

Browse files
committed
Add min, max, and chain tests and touch up defaultOperators for it
1 parent 8620018 commit f9f565c

File tree

4 files changed

+671
-17
lines changed

4 files changed

+671
-17
lines changed

defaultMethods.js

+18-17
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,24 @@ const defaultMethods = {
149149
// eslint-disable-next-line no-throw-literal
150150
throw { type }
151151
},
152-
max: (data) => Math.max(...data),
153-
min: (data) => Math.min(...data),
152+
max: (data) => {
153+
if (!data.length || typeof data[0] !== 'number') throw INVALID_ARGUMENTS
154+
let max = data[0]
155+
for (let i = 1; i < data.length; i++) {
156+
if (typeof data[i] !== 'number') throw INVALID_ARGUMENTS
157+
if (data[i] > max) max = data[i]
158+
}
159+
return max
160+
},
161+
min: (data) => {
162+
if (!data.length || typeof data[0] !== 'number') throw INVALID_ARGUMENTS
163+
let min = data[0]
164+
for (let i = 1; i < data.length; i++) {
165+
if (typeof data[i] !== 'number') throw INVALID_ARGUMENTS
166+
if (data[i] < min) min = data[i]
167+
}
168+
return min
169+
},
154170
in: ([item, array]) => (array || []).includes(item),
155171
preserve: {
156172
lazy: true,
@@ -937,21 +953,6 @@ Object.keys(defaultMethods).forEach((item) => {
937953
: defaultMethods[item].deterministic
938954
})
939955

940-
// @ts-ignore Allow custom attribute
941-
defaultMethods.min.compile = function (data, buildState) {
942-
if (!Array.isArray(data)) return false
943-
return `Math.min(${data
944-
.map((i) => buildString(i, buildState))
945-
.join(', ')})`
946-
}
947-
// @ts-ignore Allow custom attribute
948-
defaultMethods.max.compile = function (data, buildState) {
949-
if (!Array.isArray(data)) return false
950-
return `Math.max(${data
951-
.map((i) => buildString(i, buildState))
952-
.join(', ')})`
953-
}
954-
955956
// @ts-ignore Allow custom attribute
956957
defaultMethods.if.compile = function (data, buildState) {
957958
if (!Array.isArray(data)) return false

suites/arithmetic/chain.json

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
[
2+
"Collection of Chained Arithmetic Tests",
3+
"Plus",
4+
{
5+
"description": "Plus can be chained with other operators",
6+
"rule": { "+": { "val": "arr" } },
7+
"data": { "arr": [1, 2, 3] },
8+
"result": 6
9+
},
10+
{
11+
"description": "Plus can be chained with other operators (2)",
12+
"rule": { "+": { "merge": [[1, 2], 3, [4, 5]] } },
13+
"data": null,
14+
"result": 15
15+
},
16+
{
17+
"description": "Plus can be chained with other operators (3)",
18+
"rule": { "+": { "map": [{ "val": "people" }, { "val": "age" }] } },
19+
"data": {
20+
"people": [
21+
{ "name": "John", "age": 30 },
22+
{ "name": "Jane", "age": 25 },
23+
{ "name": "Bob", "age": 35 },
24+
{ "name": "Alice", "age": 28 }
25+
]
26+
},
27+
"result": 118
28+
},
29+
"Multiply",
30+
{
31+
"description": "Multiply can be chained with other operators",
32+
"rule": { "*": { "val": "arr" } },
33+
"data": { "arr": [1, 2, 3] },
34+
"result": 6
35+
},
36+
{
37+
"description": "Multiply can be chained with other operators (2)",
38+
"rule": { "*": { "merge": [[1, 2], 3, [4, 5]] } },
39+
"data": null,
40+
"result": 120
41+
},
42+
{
43+
"description": "Multiply can be chained with other operators (3)",
44+
"rule": { "*": { "map": [{ "val": "arr" }, { "val": "x" }] } },
45+
"data": {
46+
"arr": [
47+
{ "x": 30 },
48+
{ "x": 25 },
49+
{ "x": 35 },
50+
{ "x": 28 }
51+
]
52+
},
53+
"result": 735000
54+
},
55+
"Minus",
56+
{
57+
"description": "Minus can be chained with other operators",
58+
"rule": { "-": { "val": "arr" } },
59+
"data": { "arr": [10, 7] },
60+
"result": 3
61+
},
62+
{
63+
"description": "Minus can be chained with other operators (2)",
64+
"rule": { "-": { "merge": [[1], 5] } },
65+
"data": null,
66+
"result": -4
67+
},
68+
{
69+
"description": "Minus can be chained with other operators (3)",
70+
"rule": { "-": { "map": [{ "val": "people" }, { "val": "age" }] } },
71+
"data": {
72+
"people": [
73+
{ "name": "John", "age": 30 },
74+
{ "name": "Jane", "age": 25 },
75+
{ "name": "Bob", "age": 35 },
76+
{ "name": "Alice", "age": 28 }
77+
]
78+
},
79+
"result": -58
80+
},
81+
"Divide",
82+
{
83+
"description": "Divide can be chained with other operators",
84+
"rule": { "/": { "val": "arr" } },
85+
"data": { "arr": [10, 5] },
86+
"result": 2
87+
},
88+
{
89+
"description": "Divide can be chained with other operators (2)",
90+
"rule": { "/": { "merge": [[20], 5] } },
91+
"data": null,
92+
"result": 4
93+
},
94+
{
95+
"description": "Divide can be chained with other operators (3)",
96+
"rule": { "/": { "map": [{ "val": "arr" }, { "val": "x" }] } },
97+
"data": {
98+
"arr": [
99+
{ "x": 16 },
100+
{ "x": 2 },
101+
{ "x": 2 },
102+
{ "x": 2 }
103+
]
104+
},
105+
"result": 2
106+
},
107+
"Modulo",
108+
{
109+
"description": "Modulo can be chained with other operators",
110+
"rule": { "%": { "val": "arr" } },
111+
"data": { "arr": [10, 3] },
112+
"result": 1
113+
},
114+
{
115+
"description": "Modulo can be chained with other operators (2)",
116+
"rule": { "%": { "merge": [[20], 3] } },
117+
"data": null,
118+
"result": 2
119+
},
120+
{
121+
"description": "Modulo can be chained with other operators (3)",
122+
"rule": { "%": { "map": [{ "val": "arr" }, { "val": "x" }] } },
123+
"data": {
124+
"arr": [
125+
{ "x": 17 },
126+
{ "x": 4 }
127+
]
128+
},
129+
"result": 1
130+
}
131+
]

0 commit comments

Comments
 (0)