Skip to content

Commit 17e9fed

Browse files
authored
Merge pull request #90 from jwadhams/modern-eslint
Update to modern eslint, then actually listen to all the errors
2 parents fadfa5d + 1e1d80b commit 17e9fed

File tree

4 files changed

+109
-118
lines changed

4 files changed

+109
-118
lines changed

logic.js

Lines changed: 83 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ http://ricostacruz.com/cheatsheets/umdjs.html
7575
console.log(a); return a;
7676
},
7777
"in": function(a, b) {
78-
if(!b || typeof b.indexOf === "undefined") return false;
78+
if (!b || typeof b.indexOf === "undefined") return false;
7979
return (b.indexOf(a) !== -1);
8080
},
8181
"cat": function() {
8282
return Array.prototype.join.call(arguments, "");
8383
},
84-
"substr":function(source, start, end) {
85-
if(end < 0){
84+
"substr": function(source, start, end) {
85+
if (end < 0) {
8686
// JavaScript doesn't support negative end, this emulates PHP behavior
8787
var temp = String(source).substr(start);
8888
return temp.substr(0, temp.length + end);
@@ -100,9 +100,9 @@ http://ricostacruz.com/cheatsheets/umdjs.html
100100
});
101101
},
102102
"-": function(a, b) {
103-
if(b === undefined) {
103+
if (b === undefined) {
104104
return -a;
105-
}else{
105+
} else {
106106
return a - b;
107107
}
108108
},
@@ -123,17 +123,17 @@ http://ricostacruz.com/cheatsheets/umdjs.html
123123
"var": function(a, b) {
124124
var not_found = (b === undefined) ? null : b;
125125
var data = this;
126-
if(typeof a === "undefined" || a==="" || a===null) {
126+
if (typeof a === "undefined" || a==="" || a===null) {
127127
return data;
128128
}
129129
var sub_props = String(a).split(".");
130-
for(var i = 0; i < sub_props.length; i++) {
131-
if(data === null || data === undefined) {
130+
for (var i = 0; i < sub_props.length; i++) {
131+
if (data === null || data === undefined) {
132132
return not_found;
133133
}
134134
// Descending into data
135135
data = data[sub_props[i]];
136-
if(data === undefined) {
136+
if (data === undefined) {
137137
return not_found;
138138
}
139139
}
@@ -150,10 +150,10 @@ http://ricostacruz.com/cheatsheets/umdjs.html
150150
var missing = [];
151151
var keys = Array.isArray(arguments[0]) ? arguments[0] : arguments;
152152

153-
for(var i = 0; i < keys.length; i++) {
153+
for (var i = 0; i < keys.length; i++) {
154154
var key = keys[i];
155155
var value = jsonLogic.apply({"var": key}, this);
156-
if(value === null || value === "") {
156+
if (value === null || value === "") {
157157
missing.push(key);
158158
}
159159
}
@@ -164,9 +164,9 @@ http://ricostacruz.com/cheatsheets/umdjs.html
164164
// missing_some takes two arguments, how many (minimum) items must be present, and an array of keys (just like 'missing') to check for presence.
165165
var are_missing = jsonLogic.apply({"missing": options}, this);
166166

167-
if(options.length - are_missing.length >= need_count) {
167+
if (options.length - are_missing.length >= need_count) {
168168
return [];
169-
}else{
169+
} else {
170170
return are_missing;
171171
}
172172
},
@@ -187,7 +187,7 @@ http://ricostacruz.com/cheatsheets/umdjs.html
187187
Spec and rationale here: http://jsonlogic.com/truthy
188188
*/
189189
jsonLogic.truthy = function(value) {
190-
if(Array.isArray(value) && value.length === 0) {
190+
if (Array.isArray(value) && value.length === 0) {
191191
return false;
192192
}
193193
return !! value;
@@ -204,29 +204,32 @@ http://ricostacruz.com/cheatsheets/umdjs.html
204204

205205
jsonLogic.apply = function(logic, data) {
206206
// Does this array contain logic? Only one way to find out.
207-
if(Array.isArray(logic)) {
207+
if (Array.isArray(logic)) {
208208
return logic.map(function(l) {
209209
return jsonLogic.apply(l, data);
210210
});
211211
}
212212
// You've recursed to a primitive, stop!
213-
if( ! jsonLogic.is_logic(logic) ) {
213+
if ( ! jsonLogic.is_logic(logic) ) {
214214
return logic;
215215
}
216216

217217
var op = jsonLogic.get_operator(logic);
218218
var values = logic[op];
219219
var i;
220220
var current;
221-
var scopedLogic, scopedData, filtered, initial;
221+
var scopedLogic;
222+
var scopedData;
223+
var filtered;
224+
var initial;
222225

223226
// easy syntax for unary operators, like {"var" : "x"} instead of strict {"var" : ["x"]}
224-
if( ! Array.isArray(values)) {
227+
if ( ! Array.isArray(values)) {
225228
values = [values];
226229
}
227230

228231
// 'if', 'and', and 'or' violate the normal rule of depth-first calculating consequents, let each manage recursion as needed.
229-
if(op === "if" || op == "?:") {
232+
if (op === "if" || op == "?:") {
230233
/* 'if' should be called with a odd number of parameters, 3 or greater
231234
This works on the pattern:
232235
if( 0 ){ 1 }else{ 2 };
@@ -240,96 +243,91 @@ http://ricostacruz.com/cheatsheets/umdjs.html
240243
given one parameter, evaluate and return it. (it's an Else and all the If/ElseIf were false)
241244
given 0 parameters, return NULL (not great practice, but there was no Else)
242245
*/
243-
for(i = 0; i < values.length - 1; i += 2) {
244-
if( jsonLogic.truthy( jsonLogic.apply(values[i], data) ) ) {
246+
for (i = 0; i < values.length - 1; i += 2) {
247+
if ( jsonLogic.truthy( jsonLogic.apply(values[i], data) ) ) {
245248
return jsonLogic.apply(values[i+1], data);
246249
}
247250
}
248-
if(values.length === i+1) return jsonLogic.apply(values[i], data);
251+
if (values.length === i+1) {
252+
return jsonLogic.apply(values[i], data);
253+
}
249254
return null;
250-
}else if(op === "and") { // Return first falsy, or last
251-
for(i=0; i < values.length; i+=1) {
255+
} else if (op === "and") { // Return first falsy, or last
256+
for (i=0; i < values.length; i+=1) {
252257
current = jsonLogic.apply(values[i], data);
253-
if( ! jsonLogic.truthy(current)) {
258+
if ( ! jsonLogic.truthy(current)) {
254259
return current;
255260
}
256261
}
257262
return current; // Last
258-
}else if(op === "or") {// Return first truthy, or last
259-
for(i=0; i < values.length; i+=1) {
263+
} else if (op === "or") {// Return first truthy, or last
264+
for (i=0; i < values.length; i+=1) {
260265
current = jsonLogic.apply(values[i], data);
261-
if( jsonLogic.truthy(current) ) {
266+
if ( jsonLogic.truthy(current) ) {
262267
return current;
263268
}
264269
}
265270
return current; // Last
266-
267-
268-
269-
270-
}else if(op === 'filter'){
271+
} else if (op === "filter") {
271272
scopedData = jsonLogic.apply(values[0], data);
272273
scopedLogic = values[1];
273274

274275
if ( ! Array.isArray(scopedData)) {
275-
return [];
276+
return [];
276277
}
277278
// Return only the elements from the array in the first argument,
278279
// that return truthy when passed to the logic in the second argument.
279280
// For parity with JavaScript, reindex the returned array
280-
return scopedData.filter(function(datum){
281-
return jsonLogic.truthy( jsonLogic.apply(scopedLogic, datum));
281+
return scopedData.filter(function(datum) {
282+
return jsonLogic.truthy( jsonLogic.apply(scopedLogic, datum));
282283
});
283-
}else if(op === 'map'){
284+
} else if (op === "map") {
284285
scopedData = jsonLogic.apply(values[0], data);
285286
scopedLogic = values[1];
286287

287288
if ( ! Array.isArray(scopedData)) {
288-
return [];
289+
return [];
289290
}
290291

291-
return scopedData.map(function(datum){
292-
return jsonLogic.apply(scopedLogic, datum);
292+
return scopedData.map(function(datum) {
293+
return jsonLogic.apply(scopedLogic, datum);
293294
});
294-
295-
}else if(op === 'reduce'){
295+
} else if (op === "reduce") {
296296
scopedData = jsonLogic.apply(values[0], data);
297297
scopedLogic = values[1];
298-
initial = typeof values[2] !== 'undefined' ? values[2] : null;
298+
initial = typeof values[2] !== "undefined" ? values[2] : null;
299299

300300
if ( ! Array.isArray(scopedData)) {
301-
return initial;
301+
return initial;
302302
}
303303

304304
return scopedData.reduce(
305-
function(accumulator, current){
306-
return jsonLogic.apply(
307-
scopedLogic,
308-
{'current':current, 'accumulator':accumulator}
309-
);
310-
},
311-
initial
305+
function(accumulator, current) {
306+
return jsonLogic.apply(
307+
scopedLogic,
308+
{current: current, accumulator: accumulator}
309+
);
310+
},
311+
initial
312312
);
313-
314-
}else if(op === "all") {
313+
} else if (op === "all") {
315314
scopedData = jsonLogic.apply(values[0], data);
316315
scopedLogic = values[1];
317316
// All of an empty set is false. Note, some and none have correct fallback after the for loop
318-
if( ! scopedData.length) {
317+
if ( ! scopedData.length) {
319318
return false;
320319
}
321-
for(i=0; i < scopedData.length; i+=1) {
322-
if( ! jsonLogic.truthy( jsonLogic.apply(scopedLogic, scopedData[i]) )) {
320+
for (i=0; i < scopedData.length; i+=1) {
321+
if ( ! jsonLogic.truthy( jsonLogic.apply(scopedLogic, scopedData[i]) )) {
323322
return false; // First falsy, short circuit
324323
}
325324
}
326325
return true; // All were truthy
327-
}else if(op === "none") {
328-
filtered = jsonLogic.apply({'filter' : values}, data);
326+
} else if (op === "none") {
327+
filtered = jsonLogic.apply({filter: values}, data);
329328
return filtered.length === 0;
330-
331-
}else if(op === "some") {
332-
filtered = jsonLogic.apply({'filter' : values}, data);
329+
} else if (op === "some") {
330+
filtered = jsonLogic.apply({filter: values}, data);
333331
return filtered.length > 0;
334332
}
335333

@@ -342,17 +340,17 @@ http://ricostacruz.com/cheatsheets/umdjs.html
342340
// The operation is called with "data" bound to its "this" and "values" passed as arguments.
343341
// Structured commands like % or > can name formal arguments while flexible commands (like missing or merge) can operate on the pseudo-array arguments
344342
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
345-
if(typeof operations[op] === "function") {
343+
if (typeof operations[op] === "function") {
346344
return operations[op].apply(data, values);
347-
}else if(op.indexOf(".") > 0) { // Contains a dot, and not in the 0th position
345+
} else if (op.indexOf(".") > 0) { // Contains a dot, and not in the 0th position
348346
var sub_ops = String(op).split(".");
349347
var operation = operations;
350-
for(i = 0; i < sub_ops.length; i++) {
348+
for (i = 0; i < sub_ops.length; i++) {
351349
// Descending into operations
352350
operation = operation[sub_ops[i]];
353-
if(operation === undefined) {
351+
if (operation === undefined) {
354352
throw new Error("Unrecognized operation " + op +
355-
" (failed at " + sub_ops.slice(0, i+1).join(".") + ")");
353+
" (failed at " + sub_ops.slice(0, i+1).join(".") + ")");
356354
}
357355
}
358356

@@ -365,18 +363,18 @@ http://ricostacruz.com/cheatsheets/umdjs.html
365363
jsonLogic.uses_data = function(logic) {
366364
var collection = [];
367365

368-
if( jsonLogic.is_logic(logic) ) {
366+
if (jsonLogic.is_logic(logic)) {
369367
var op = jsonLogic.get_operator(logic);
370368
var values = logic[op];
371369

372-
if( ! Array.isArray(values)) {
370+
if ( ! Array.isArray(values)) {
373371
values = [values];
374372
}
375373

376-
if(op === "var") {
374+
if (op === "var") {
377375
// This doesn't cover the case where the arg to var is itself a rule.
378376
collection.push(values[0]);
379-
}else{
377+
} else {
380378
// Recursion!
381379
values.map(function(val) {
382380
collection.push.apply(collection, jsonLogic.uses_data(val) );
@@ -397,30 +395,30 @@ http://ricostacruz.com/cheatsheets/umdjs.html
397395

398396
jsonLogic.rule_like = function(rule, pattern) {
399397
// console.log("Is ". JSON.stringify(rule) . " like " . JSON.stringify(pattern) . "?");
400-
if(pattern === rule) {
398+
if (pattern === rule) {
401399
return true;
402400
} // TODO : Deep object equivalency?
403-
if(pattern === "@") {
401+
if (pattern === "@") {
404402
return true;
405403
} // Wildcard!
406-
if(pattern === "number") {
404+
if (pattern === "number") {
407405
return (typeof rule === "number");
408406
}
409-
if(pattern === "string") {
407+
if (pattern === "string") {
410408
return (typeof rule === "string");
411409
}
412-
if(pattern === "array") {
410+
if (pattern === "array") {
413411
// !logic test might be superfluous in JavaScript
414412
return Array.isArray(rule) && ! jsonLogic.is_logic(rule);
415413
}
416414

417-
if(jsonLogic.is_logic(pattern)) {
418-
if(jsonLogic.is_logic(rule)) {
415+
if (jsonLogic.is_logic(pattern)) {
416+
if (jsonLogic.is_logic(rule)) {
419417
var pattern_op = jsonLogic.get_operator(pattern);
420418
var rule_op = jsonLogic.get_operator(rule);
421419

422-
if(pattern_op === "@" || pattern_op === rule_op) {
423-
// echo "\nOperators match, go deeper\n";
420+
if (pattern_op === "@" || pattern_op === rule_op) {
421+
// echo "\nOperators match, go deeper\n";
424422
return jsonLogic.rule_like(
425423
jsonLogic.get_values(rule, false),
426424
jsonLogic.get_values(pattern, false)
@@ -430,22 +428,22 @@ http://ricostacruz.com/cheatsheets/umdjs.html
430428
return false; // pattern is logic, rule isn't, can't be eq
431429
}
432430

433-
if(Array.isArray(pattern)) {
434-
if(Array.isArray(rule)) {
435-
if(pattern.length !== rule.length) {
431+
if (Array.isArray(pattern)) {
432+
if (Array.isArray(rule)) {
433+
if (pattern.length !== rule.length) {
436434
return false;
437435
}
438436
/*
439437
Note, array order MATTERS, because we're using this array test logic to consider arguments, where order can matter. (e.g., + is commutative, but '-' or 'if' or 'var' are NOT)
440438
*/
441-
for(var i = 0; i < pattern.length; i += 1) {
439+
for (var i = 0; i < pattern.length; i += 1) {
442440
// If any fail, we fail
443-
if( ! jsonLogic.rule_like(rule[i], pattern[i])) {
441+
if ( ! jsonLogic.rule_like(rule[i], pattern[i])) {
444442
return false;
445443
}
446444
}
447445
return true; // If they *all* passed, we pass
448-
}else{
446+
} else {
449447
return false; // Pattern is array, rule isn't
450448
}
451449
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
},
99
"dependencies": {},
1010
"devDependencies": {
11-
"eslint": "^3.9.1",
12-
"eslint-config-google": "^0.7.0",
11+
"eslint": "^7.11.0",
12+
"eslint-config-google": "^0.14.0",
1313
"gulp": "^3.9.0",
1414
"qunit": "^0.7.7",
1515
"request": "^2.65.0"

0 commit comments

Comments
 (0)