Skip to content

Commit 989007f

Browse files
committed
Passing tests
1 parent 8cd1b8d commit 989007f

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

logic.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ http://ricostacruz.com/cheatsheets/umdjs.html
114114
"/": function (a, b) {
115115
return a / b;
116116
},
117-
min: function () {
118-
return Math.min.apply(this, arguments);
117+
min: function (...args) {
118+
return Math.min(...args);
119119
},
120-
max: function () {
121-
return Math.max.apply(this, arguments);
120+
max: function (...args) {
121+
return Math.max(...args);
122122
},
123123
merge: function () {
124124
return Array.prototype.reduce.call(
@@ -129,9 +129,8 @@ http://ricostacruz.com/cheatsheets/umdjs.html
129129
[]
130130
);
131131
},
132-
var: function (a, b) {
132+
var: function (data, a, b) {
133133
var not_found = b === undefined ? null : b;
134-
var data = this;
135134
if (typeof a === "undefined" || a === "" || a === null) {
136135
return data;
137136
}
@@ -148,7 +147,7 @@ http://ricostacruz.com/cheatsheets/umdjs.html
148147
}
149148
return data;
150149
},
151-
missing: function () {
150+
missing: function (data, ...args) {
152151
/*
153152
Missing can receive many keys as many arguments, like {"missing:[1,2]}
154153
Missing can also receive *one* argument that is an array of keys,
@@ -157,21 +156,21 @@ http://ricostacruz.com/cheatsheets/umdjs.html
157156
*/
158157

159158
var missing = [];
160-
var keys = Array.isArray(arguments[0]) ? arguments[0] : arguments;
159+
var keys = Array.isArray(args[0]) ? args[0] : args;
161160

162161
for (var i = 0; i < keys.length; i++) {
163162
var key = keys[i];
164-
var value = jsonLogic.apply({ var: key }, this);
163+
var value = jsonLogic.apply({ var: key }, data);
165164
if (value === null || value === "") {
166165
missing.push(key);
167166
}
168167
}
169168

170169
return missing;
171170
},
172-
missing_some: function (need_count, options) {
171+
missing_some: function (data, need_count, options) {
173172
// missing_some takes two arguments, how many (minimum) items must be present, and an array of keys (just like 'missing') to check for presence.
174-
var are_missing = jsonLogic.apply({ missing: options }, this);
173+
var are_missing = jsonLogic.apply({ missing: options }, data);
175174

176175
if (options.length - are_missing.length >= need_count) {
177176
return [];
@@ -355,6 +354,15 @@ http://ricostacruz.com/cheatsheets/umdjs.html
355354
}
356355
}
357356
return false; // None were truthy
357+
} else if (op === "var") {
358+
return operations.var(
359+
data,
360+
...values.map((val) => jsonLogic.apply(val, data))
361+
);
362+
} else if (op === "missing") {
363+
return operations.missing(data, ...values.map((val) => jsonLogic.apply(val, data)));
364+
} else if (op === "missing_some") {
365+
return operations.missing_some(data, ...values.map((val) => jsonLogic.apply(val, data)));
358366
}
359367

360368
// Everyone else gets immediate depth-first recursion
@@ -366,7 +374,7 @@ http://ricostacruz.com/cheatsheets/umdjs.html
366374
// Structured commands like % or > can name formal arguments while flexible commands (like missing or merge) can operate on the pseudo-array arguments
367375
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
368376
if (operations.hasOwnProperty(op) && typeof operations[op] === "function") {
369-
return operations[op].apply(data, values);
377+
return operations[op](...values);
370378
} else if (op.indexOf(".") > 0) {
371379
// Contains a dot, and not in the 0th position
372380
var sub_ops = String(op).split(".");

0 commit comments

Comments
 (0)