@@ -75,14 +75,14 @@ http://ricostacruz.com/cheatsheets/umdjs.html
75
75
console . log ( a ) ; return a ;
76
76
} ,
77
77
"in" : function ( a , b ) {
78
- if ( ! b || typeof b . indexOf === "undefined" ) return false ;
78
+ if ( ! b || typeof b . indexOf === "undefined" ) return false ;
79
79
return ( b . indexOf ( a ) !== - 1 ) ;
80
80
} ,
81
81
"cat" : function ( ) {
82
82
return Array . prototype . join . call ( arguments , "" ) ;
83
83
} ,
84
- "substr" :function ( source , start , end ) {
85
- if ( end < 0 ) {
84
+ "substr" : function ( source , start , end ) {
85
+ if ( end < 0 ) {
86
86
// JavaScript doesn't support negative end, this emulates PHP behavior
87
87
var temp = String ( source ) . substr ( start ) ;
88
88
return temp . substr ( 0 , temp . length + end ) ;
@@ -100,9 +100,9 @@ http://ricostacruz.com/cheatsheets/umdjs.html
100
100
} ) ;
101
101
} ,
102
102
"-" : function ( a , b ) {
103
- if ( b === undefined ) {
103
+ if ( b === undefined ) {
104
104
return - a ;
105
- } else {
105
+ } else {
106
106
return a - b ;
107
107
}
108
108
} ,
@@ -123,17 +123,17 @@ http://ricostacruz.com/cheatsheets/umdjs.html
123
123
"var" : function ( a , b ) {
124
124
var not_found = ( b === undefined ) ? null : b ;
125
125
var data = this ;
126
- if ( typeof a === "undefined" || a === "" || a === null ) {
126
+ if ( typeof a === "undefined" || a === "" || a === null ) {
127
127
return data ;
128
128
}
129
129
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 ) {
132
132
return not_found ;
133
133
}
134
134
// Descending into data
135
135
data = data [ sub_props [ i ] ] ;
136
- if ( data === undefined ) {
136
+ if ( data === undefined ) {
137
137
return not_found ;
138
138
}
139
139
}
@@ -150,10 +150,10 @@ http://ricostacruz.com/cheatsheets/umdjs.html
150
150
var missing = [ ] ;
151
151
var keys = Array . isArray ( arguments [ 0 ] ) ? arguments [ 0 ] : arguments ;
152
152
153
- for ( var i = 0 ; i < keys . length ; i ++ ) {
153
+ for ( var i = 0 ; i < keys . length ; i ++ ) {
154
154
var key = keys [ i ] ;
155
155
var value = jsonLogic . apply ( { "var" : key } , this ) ;
156
- if ( value === null || value === "" ) {
156
+ if ( value === null || value === "" ) {
157
157
missing . push ( key ) ;
158
158
}
159
159
}
@@ -164,9 +164,9 @@ http://ricostacruz.com/cheatsheets/umdjs.html
164
164
// missing_some takes two arguments, how many (minimum) items must be present, and an array of keys (just like 'missing') to check for presence.
165
165
var are_missing = jsonLogic . apply ( { "missing" : options } , this ) ;
166
166
167
- if ( options . length - are_missing . length >= need_count ) {
167
+ if ( options . length - are_missing . length >= need_count ) {
168
168
return [ ] ;
169
- } else {
169
+ } else {
170
170
return are_missing ;
171
171
}
172
172
} ,
@@ -187,7 +187,7 @@ http://ricostacruz.com/cheatsheets/umdjs.html
187
187
Spec and rationale here: http://jsonlogic.com/truthy
188
188
*/
189
189
jsonLogic . truthy = function ( value ) {
190
- if ( Array . isArray ( value ) && value . length === 0 ) {
190
+ if ( Array . isArray ( value ) && value . length === 0 ) {
191
191
return false ;
192
192
}
193
193
return ! ! value ;
@@ -204,29 +204,32 @@ http://ricostacruz.com/cheatsheets/umdjs.html
204
204
205
205
jsonLogic . apply = function ( logic , data ) {
206
206
// Does this array contain logic? Only one way to find out.
207
- if ( Array . isArray ( logic ) ) {
207
+ if ( Array . isArray ( logic ) ) {
208
208
return logic . map ( function ( l ) {
209
209
return jsonLogic . apply ( l , data ) ;
210
210
} ) ;
211
211
}
212
212
// You've recursed to a primitive, stop!
213
- if ( ! jsonLogic . is_logic ( logic ) ) {
213
+ if ( ! jsonLogic . is_logic ( logic ) ) {
214
214
return logic ;
215
215
}
216
216
217
217
var op = jsonLogic . get_operator ( logic ) ;
218
218
var values = logic [ op ] ;
219
219
var i ;
220
220
var current ;
221
- var scopedLogic , scopedData , filtered , initial ;
221
+ var scopedLogic ;
222
+ var scopedData ;
223
+ var filtered ;
224
+ var initial ;
222
225
223
226
// easy syntax for unary operators, like {"var" : "x"} instead of strict {"var" : ["x"]}
224
- if ( ! Array . isArray ( values ) ) {
227
+ if ( ! Array . isArray ( values ) ) {
225
228
values = [ values ] ;
226
229
}
227
230
228
231
// '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 == "?:" ) {
230
233
/* 'if' should be called with a odd number of parameters, 3 or greater
231
234
This works on the pattern:
232
235
if( 0 ){ 1 }else{ 2 };
@@ -240,96 +243,91 @@ http://ricostacruz.com/cheatsheets/umdjs.html
240
243
given one parameter, evaluate and return it. (it's an Else and all the If/ElseIf were false)
241
244
given 0 parameters, return NULL (not great practice, but there was no Else)
242
245
*/
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 ) ) ) {
245
248
return jsonLogic . apply ( values [ i + 1 ] , data ) ;
246
249
}
247
250
}
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
+ }
249
254
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 ) {
252
257
current = jsonLogic . apply ( values [ i ] , data ) ;
253
- if ( ! jsonLogic . truthy ( current ) ) {
258
+ if ( ! jsonLogic . truthy ( current ) ) {
254
259
return current ;
255
260
}
256
261
}
257
262
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 ) {
260
265
current = jsonLogic . apply ( values [ i ] , data ) ;
261
- if ( jsonLogic . truthy ( current ) ) {
266
+ if ( jsonLogic . truthy ( current ) ) {
262
267
return current ;
263
268
}
264
269
}
265
270
return current ; // Last
266
-
267
-
268
-
269
-
270
- } else if ( op === 'filter' ) {
271
+ } else if ( op === "filter" ) {
271
272
scopedData = jsonLogic . apply ( values [ 0 ] , data ) ;
272
273
scopedLogic = values [ 1 ] ;
273
274
274
275
if ( ! Array . isArray ( scopedData ) ) {
275
- return [ ] ;
276
+ return [ ] ;
276
277
}
277
278
// Return only the elements from the array in the first argument,
278
279
// that return truthy when passed to the logic in the second argument.
279
280
// 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 ) ) ;
282
283
} ) ;
283
- } else if ( op === ' map' ) {
284
+ } else if ( op === " map" ) {
284
285
scopedData = jsonLogic . apply ( values [ 0 ] , data ) ;
285
286
scopedLogic = values [ 1 ] ;
286
287
287
288
if ( ! Array . isArray ( scopedData ) ) {
288
- return [ ] ;
289
+ return [ ] ;
289
290
}
290
291
291
- return scopedData . map ( function ( datum ) {
292
- return jsonLogic . apply ( scopedLogic , datum ) ;
292
+ return scopedData . map ( function ( datum ) {
293
+ return jsonLogic . apply ( scopedLogic , datum ) ;
293
294
} ) ;
294
-
295
- } else if ( op === 'reduce' ) {
295
+ } else if ( op === "reduce" ) {
296
296
scopedData = jsonLogic . apply ( values [ 0 ] , data ) ;
297
297
scopedLogic = values [ 1 ] ;
298
- initial = typeof values [ 2 ] !== ' undefined' ? values [ 2 ] : null ;
298
+ initial = typeof values [ 2 ] !== " undefined" ? values [ 2 ] : null ;
299
299
300
300
if ( ! Array . isArray ( scopedData ) ) {
301
- return initial ;
301
+ return initial ;
302
302
}
303
303
304
304
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
312
312
) ;
313
-
314
- } else if ( op === "all" ) {
313
+ } else if ( op === "all" ) {
315
314
scopedData = jsonLogic . apply ( values [ 0 ] , data ) ;
316
315
scopedLogic = values [ 1 ] ;
317
316
// 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 ) {
319
318
return false ;
320
319
}
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 ] ) ) ) {
323
322
return false ; // First falsy, short circuit
324
323
}
325
324
}
326
325
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 ) ;
329
328
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 ) ;
333
331
return filtered . length > 0 ;
334
332
}
335
333
@@ -342,17 +340,17 @@ http://ricostacruz.com/cheatsheets/umdjs.html
342
340
// The operation is called with "data" bound to its "this" and "values" passed as arguments.
343
341
// Structured commands like % or > can name formal arguments while flexible commands (like missing or merge) can operate on the pseudo-array arguments
344
342
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
345
- if ( typeof operations [ op ] === "function" ) {
343
+ if ( typeof operations [ op ] === "function" ) {
346
344
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
348
346
var sub_ops = String ( op ) . split ( "." ) ;
349
347
var operation = operations ;
350
- for ( i = 0 ; i < sub_ops . length ; i ++ ) {
348
+ for ( i = 0 ; i < sub_ops . length ; i ++ ) {
351
349
// Descending into operations
352
350
operation = operation [ sub_ops [ i ] ] ;
353
- if ( operation === undefined ) {
351
+ if ( operation === undefined ) {
354
352
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 ( "." ) + ")" ) ;
356
354
}
357
355
}
358
356
@@ -365,18 +363,18 @@ http://ricostacruz.com/cheatsheets/umdjs.html
365
363
jsonLogic . uses_data = function ( logic ) {
366
364
var collection = [ ] ;
367
365
368
- if ( jsonLogic . is_logic ( logic ) ) {
366
+ if ( jsonLogic . is_logic ( logic ) ) {
369
367
var op = jsonLogic . get_operator ( logic ) ;
370
368
var values = logic [ op ] ;
371
369
372
- if ( ! Array . isArray ( values ) ) {
370
+ if ( ! Array . isArray ( values ) ) {
373
371
values = [ values ] ;
374
372
}
375
373
376
- if ( op === "var" ) {
374
+ if ( op === "var" ) {
377
375
// This doesn't cover the case where the arg to var is itself a rule.
378
376
collection . push ( values [ 0 ] ) ;
379
- } else {
377
+ } else {
380
378
// Recursion!
381
379
values . map ( function ( val ) {
382
380
collection . push . apply ( collection , jsonLogic . uses_data ( val ) ) ;
@@ -397,30 +395,30 @@ http://ricostacruz.com/cheatsheets/umdjs.html
397
395
398
396
jsonLogic . rule_like = function ( rule , pattern ) {
399
397
// console.log("Is ". JSON.stringify(rule) . " like " . JSON.stringify(pattern) . "?");
400
- if ( pattern === rule ) {
398
+ if ( pattern === rule ) {
401
399
return true ;
402
400
} // TODO : Deep object equivalency?
403
- if ( pattern === "@" ) {
401
+ if ( pattern === "@" ) {
404
402
return true ;
405
403
} // Wildcard!
406
- if ( pattern === "number" ) {
404
+ if ( pattern === "number" ) {
407
405
return ( typeof rule === "number" ) ;
408
406
}
409
- if ( pattern === "string" ) {
407
+ if ( pattern === "string" ) {
410
408
return ( typeof rule === "string" ) ;
411
409
}
412
- if ( pattern === "array" ) {
410
+ if ( pattern === "array" ) {
413
411
// !logic test might be superfluous in JavaScript
414
412
return Array . isArray ( rule ) && ! jsonLogic . is_logic ( rule ) ;
415
413
}
416
414
417
- if ( jsonLogic . is_logic ( pattern ) ) {
418
- if ( jsonLogic . is_logic ( rule ) ) {
415
+ if ( jsonLogic . is_logic ( pattern ) ) {
416
+ if ( jsonLogic . is_logic ( rule ) ) {
419
417
var pattern_op = jsonLogic . get_operator ( pattern ) ;
420
418
var rule_op = jsonLogic . get_operator ( rule ) ;
421
419
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";
424
422
return jsonLogic . rule_like (
425
423
jsonLogic . get_values ( rule , false ) ,
426
424
jsonLogic . get_values ( pattern , false )
@@ -430,22 +428,22 @@ http://ricostacruz.com/cheatsheets/umdjs.html
430
428
return false ; // pattern is logic, rule isn't, can't be eq
431
429
}
432
430
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 ) {
436
434
return false ;
437
435
}
438
436
/*
439
437
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)
440
438
*/
441
- for ( var i = 0 ; i < pattern . length ; i += 1 ) {
439
+ for ( var i = 0 ; i < pattern . length ; i += 1 ) {
442
440
// If any fail, we fail
443
- if ( ! jsonLogic . rule_like ( rule [ i ] , pattern [ i ] ) ) {
441
+ if ( ! jsonLogic . rule_like ( rule [ i ] , pattern [ i ] ) ) {
444
442
return false ;
445
443
}
446
444
}
447
445
return true ; // If they *all* passed, we pass
448
- } else {
446
+ } else {
449
447
return false ; // Pattern is array, rule isn't
450
448
}
451
449
}
0 commit comments