This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathfilters.js
677 lines (623 loc) · 15.6 KB
/
filters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
var utils = require('./utils')
var dateFormatter = require('./dateformatter')
/**
* Helper method to recursively run a filter across an object/array and apply it to all of the object/array's values.
* @param {*} input
* @return {*}
* @private
*/
function iterateFilter (input) {
var self = this
var out = {}
if (utils.isArray(input)) {
return utils.map(input, function (value) {
return self.apply(null, arguments)
})
}
if (typeof input === 'object') {
utils.each(input, function (value, key) {
out[key] = self.apply(null, arguments)
})
return out
}
}
/**
* Backslash-escape characters that need to be escaped.
*
* @example
* {{ "\"quoted string\""|addslashes }}
* // => \"quoted string\"
*
* @param {*} input
* @return {*} Backslash-escaped string.
*/
exports.addslashes = function (input) {
var out = iterateFilter.apply(exports.addslashes, arguments)
if (out !== undefined) {
return out
}
return input
.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
.replace(/"/g, '\\"')
}
/**
* Upper-case the first letter of the input and lower-case the rest.
*
* @example
* {{ "i like Burritos"|capitalize }}
* // => I like burritos
*
* @param {*} input If given an array or object, each string member will be run through the filter individually.
* @return {*} Returns the same type as the input.
*/
exports.capitalize = function (input) {
var out = iterateFilter.apply(exports.capitalize, arguments)
if (out !== undefined) {
return out
}
return (
input
.toString()
.charAt(0)
.toUpperCase() +
input
.toString()
.substr(1)
.toLowerCase()
)
}
/**
* Format a date or Date-compatible string.
*
* @example
* // now = new Date();
* {{ now|date('Y-m-d') }}
* // => 2013-08-14
* @example
* // now = new Date();
* {{ now|date('jS \o\f F') }}
* // => 4th of July
*
* @param {?(string|date)} input
* @param {string} format PHP-style date format compatible string. Escape characters with <code>\</code> for string literals.
* @param {number=} offset Timezone offset from GMT in minutes.
* @param {string=} abbr Timezone abbreviation. Used for output only.
* @return {string} Formatted date string.
*/
exports.date = function (input, format, offset, abbr) {
var l = format.length
var date = new dateFormatter.DateZ(input)
var cur
var i = 0
var out = ''
if (offset) {
date.setTimezoneOffset(offset, abbr)
}
for (i; i < l; i += 1) {
cur = format.charAt(i)
if (cur === '\\') {
i += 1
out += i < l ? format.charAt(i) : cur
} else if (dateFormatter.hasOwnProperty(cur)) {
out += dateFormatter[cur](date, offset, abbr)
} else {
out += cur
}
}
return out
}
/**
* If the input is `undefined`, `null`, or `false`, a default return value can be specified.
*
* @example
* {{ null_value|default('Tacos') }}
* // => Tacos
*
* @example
* {{ "Burritos"|default("Tacos") }}
* // => Burritos
*
* @param {*} input
* @param {*} def Value to return if `input` is `undefined`, `null`, or `false`.
* @return {*} `input` or `def` value.
*/
exports['default'] = function (input, def) {
return input !== undefined && (input || typeof input === 'number')
? input
: def
}
/**
* Force escape the output of the variable. Optionally use `e` as a shortcut filter name. This filter will be applied by default if autoescape is turned on.
*
* @example
* {{ "<blah>"|escape }}
* // => <blah>
*
* @example
* {{ "<blah>"|e("js") }}
* // => \u003Cblah\u003E
*
* @param {*} input
* @param {string} [type='html'] If you pass the string js in as the type, output will be escaped so that it is safe for JavaScript execution.
* @return {string} Escaped string.
*/
exports.escape = function (input, type) {
var out = iterateFilter.apply(exports.escape, arguments)
var inp = input
var i = 0
var code
if (out !== undefined) {
return out
}
if (typeof input !== 'string') {
return input
}
out = ''
switch (type) {
case 'js':
inp = inp.replace(/\\/g, '\\u005C')
for (i; i < inp.length; i += 1) {
code = inp.charCodeAt(i)
if (code < 32) {
code = code.toString(16).toUpperCase()
code = code.length < 2 ? '0' + code : code
out += '\\u00' + code
} else {
out += inp[i]
}
}
return out
.replace(/&/g, '\\u0026')
.replace(/</g, '\\u003C')
.replace(/>/g, '\\u003E')
.replace(/'/g, '\\u0027')
.replace(/"/g, '\\u0022')
.replace(/=/g, '\\u003D')
.replace(/-/g, '\\u002D')
.replace(/;/g, '\\u003B')
default:
return inp
.replace(/&(?!amp;|lt;|gt;|quot;|#39;)/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
}
}
exports.e = exports.escape
/**
* Get the first item in an array or character in a string. All other objects will attempt to return the first value available.
*
* @example
* // my_arr = ['a', 'b', 'c']
* {{ my_arr|first }}
* // => a
*
* @example
* // my_val = 'Tacos'
* {{ my_val|first }}
* // T
*
* @param {*} input
* @return {*} The first item of the array or first character of the string input.
*/
exports.first = function (input) {
if (typeof input === 'object' && !utils.isArray(input)) {
var keys = utils.keys(input)
return input[keys[0]]
}
if (typeof input === 'string') {
return input.substr(0, 1)
}
return input[0]
}
/**
* Group an array of objects by a common key. If an array is not provided, the input value will be returned untouched.
*
* @example
* // people = [{ age: 23, name: 'Paul' }, { age: 26, name: 'Jane' }, { age: 23, name: 'Jim' }];
* {% for agegroup in people|groupBy('age') %}
* <h2>{{ loop.key }}</h2>
* <ul>
* {% for person in agegroup %}
* <li>{{ person.name }}</li>
* {% endfor %}
* </ul>
* {% endfor %}
*
* @param {*} input Input object.
* @param {string} key Key to group by.
* @return {object} Grouped arrays by given key.
*/
exports.groupBy = function (input, key) {
if (!utils.isArray(input)) {
return input
}
var out = {}
utils.each(input, function (value) {
if (!value.hasOwnProperty(key)) {
return
}
var keyname = value[key]
var newValue = utils.extend({}, value)
delete newValue[key]
if (!out[keyname]) {
out[keyname] = []
}
out[keyname].push(newValue)
})
return out
}
/**
* Join the input with a string.
*
* @example
* // my_array = ['foo', 'bar', 'baz']
* {{ my_array|join(', ') }}
* // => foo, bar, baz
*
* @example
* // my_key_object = { a: 'foo', b: 'bar', c: 'baz' }
* {{ my_key_object|join(' and ') }}
* // => foo and bar and baz
*
* @param {*} input
* @param {string} glue String value to join items together.
* @return {string}
*/
exports.join = function (input, glue) {
if (utils.isArray(input)) {
return input.join(glue)
}
if (typeof input === 'object') {
var out = []
utils.each(input, function (value) {
out.push(value)
})
return out.join(glue)
}
return input
}
/**
* Return a string representation of an JavaScript object.
*
* Backwards compatible with [email protected] using `json_encode`.
*
* @example
* // val = { a: 'b' }
* {{ val|json }}
* // => {"a":"b"}
*
* @example
* // val = { a: 'b' }
* {{ val|json(4) }}
* // => {
* // "a": "b"
* // }
*
* @param {*} input
* @param {number} [indent] Number of spaces to indent for pretty-formatting.
* @return {string} A valid JSON string.
*/
exports.json = function (input, indent) {
return JSON.stringify(input, null, indent || 0)
}
exports.json_encode = exports.json
/**
* Get the last item in an array or character in a string. All other objects will attempt to return the last value available.
*
* @example
* // my_arr = ['a', 'b', 'c']
* {{ my_arr|last }}
* // => c
*
* @example
* // my_val = 'Tacos'
* {{ my_val|last }}
* // s
*
* @param {*} input
* @return {*} The last item of the array or last character of the string.input.
*/
exports.last = function (input) {
if (typeof input === 'object' && !utils.isArray(input)) {
var keys = utils.keys(input)
return input[keys[keys.length - 1]]
}
if (typeof input === 'string') {
return input.charAt(input.length - 1)
}
return input[input.length - 1]
}
/**
* Get the number of items in an array, string, or object.
*
* @example
* // my_arr = ['a', 'b', 'c']
* {{ my_arr|length }}
* // => 3
*
* @example
* // my_str = 'Tacos'
* {{ my_str|length }}
* // => 5
*
* @example
* // my_obj = {a: 5, b: 20}
* {{ my_obj|length }}
* // => 2
*
* @param {*} input
* @return {*} The length of the input
*/
exports.length = function (input) {
if (typeof input === 'object' && !utils.isArray(input)) {
var keys = utils.keys(input)
return keys.length
}
if (input.hasOwnProperty('length')) {
return input.length
}
return ''
}
/**
* Return the input in all lowercase letters.
*
* @example
* {{ "FOOBAR"|lower }}
* // => foobar
*
* @example
* // myObj = { a: 'FOO', b: 'BAR' }
* {{ myObj|lower|join('') }}
* // => foobar
*
* @param {*} input
* @return {*} Returns the same type as the input.
*/
exports.lower = function (input) {
var out = iterateFilter.apply(exports.lower, arguments)
if (out !== undefined) {
return out
}
return input.toString().toLowerCase()
}
/**
* Deprecated in favor of <a href="#safe">safe</a>.
*/
exports.raw = function (input) {
return exports.safe(input)
}
exports.raw.safe = true
/**
* Returns a new string with the matched search pattern replaced by the given replacement string. Uses JavaScript's built-in String.replace() method.
*
* @example
* // my_var = 'foobar';
* {{ my_var|replace('o', 'e', 'g') }}
* // => feebar
*
* @example
* // my_var = "farfegnugen";
* {{ my_var|replace('^f', 'p') }}
* // => parfegnugen
*
* @example
* // my_var = 'a1b2c3';
* {{ my_var|replace('\w', '0', 'g') }}
* // => 010203
*
* @param {string} input
* @param {string} search String or pattern to replace from the input.
* @param {string} replacement String to replace matched pattern.
* @param {string} [flags] Regular Expression flags. 'g': global match, 'i': ignore case, 'm': match over multiple lines
* @return {string} Replaced string.
*/
exports.replace = function (input, search, replacement, flags) {
var r = new RegExp(search, flags)
return input.replace(r, replacement)
}
/**
* Reverse sort the input. This is an alias for <code data-language="swig">{{ input|sort(true) }}</code>.
*
* @example
* // val = [1, 2, 3];
* {{ val|reverse }}
* // => 3,2,1
*
* @param {array} input
* @return {array} Reversed array. The original input object is returned if it was not an array.
*/
exports.reverse = function (input) {
return exports.sort(input, true)
}
/**
* Forces the input to not be auto-escaped. Use this only on content that you know is safe to be rendered on your page.
*
* @example
* // my_var = "<p>Stuff</p>";
* {{ my_var|safe }}
* // => <p>Stuff</p>
*
* @param {*} input
* @return {*} The input exactly how it was given, regardless of autoescaping status.
*/
exports.safe = function (input) {
// This is a magic filter. Its logic is hard-coded into Swig's parser.
return input
}
exports.safe.safe = true
/**
* Sort the input in an ascending direction.
* If given an object, will return the keys as a sorted array.
* If given a string, each character will be sorted individually.
*
* @example
* // val = [2, 6, 4];
* {{ val|sort }}
* // => 2,4,6
*
* @example
* // val = 'zaq';
* {{ val|sort }}
* // => aqz
*
* @example
* // val = { bar: 1, foo: 2 }
* {{ val|sort(true) }}
* // => foo,bar
*
* @param {*} input
* @param {boolean} [reverse=false] Output is given reverse-sorted if true.
* @return {*} Sorted array;
*/
exports.sort = function (input, reverse) {
var out, clone
if (utils.isArray(input)) {
clone = utils.extend([], input)
out = clone.sort()
} else {
switch (typeof input) {
case 'object':
out = utils.keys(input).sort()
break
case 'string':
out = input.split('')
if (reverse) {
return out.reverse().join('')
}
return out.sort().join('')
}
}
if (out && reverse) {
return out.reverse()
}
return out || input
}
/**
* Strip HTML tags.
*
* @example
* // stuff = '<p>foobar</p>';
* {{ stuff|striptags }}
* // => foobar
*
* @param {*} input
* @return {*} Returns the same object as the input, but with all string values stripped of tags.
*/
exports.striptags = function (input) {
var out = iterateFilter.apply(exports.striptags, arguments)
if (out !== undefined) {
return out
}
return input.toString().replace(/(<([^>]+)>)/gi, '')
}
/**
* Capitalizes every word given and lower-cases all other letters.
*
* @example
* // my_str = 'this is soMe text';
* {{ my_str|title }}
* // => This Is Some Text
*
* @example
* // my_arr = ['hi', 'this', 'is', 'an', 'array'];
* {{ my_arr|title|join(' ') }}
* // => Hi This Is An Array
*
* @param {*} input
* @return {*} Returns the same object as the input, but with all words in strings title-cased.
*/
exports.title = function (input) {
var out = iterateFilter.apply(exports.title, arguments)
if (out !== undefined) {
return out
}
return input.toString().replace(/\w\S*/g, function (str) {
return str.charAt(0).toUpperCase() + str.substr(1).toLowerCase()
})
}
/**
* Remove all duplicate items from an array.
*
* @example
* // my_arr = [1, 2, 3, 4, 4, 3, 2, 1];
* {{ my_arr|uniq|join(',') }}
* // => 1,2,3,4
*
* @param {array} input
* @return {array} Array with unique items. If input was not an array, the original item is returned untouched.
*/
exports.uniq = function (input) {
var result
if (!input || !utils.isArray(input)) {
return ''
}
result = []
utils.each(input, function (v) {
if (result.indexOf(v) === -1) {
result.push(v)
}
})
return result
}
/**
* Convert the input to all uppercase letters. If an object or array is provided, all values will be uppercased.
*
* @example
* // my_str = 'tacos';
* {{ my_str|upper }}
* // => TACOS
*
* @example
* // my_arr = ['tacos', 'burritos'];
* {{ my_arr|upper|join(' & ') }}
* // => TACOS & BURRITOS
*
* @param {*} input
* @return {*} Returns the same type as the input, with all strings upper-cased.
*/
exports.upper = function (input) {
var out = iterateFilter.apply(exports.upper, arguments)
if (out !== undefined) {
return out
}
return input.toString().toUpperCase()
}
/**
* URL-encode a string. If an object or array is passed, all values will be URL-encoded.
*
* @example
* // my_str = 'param=1&anotherParam=2';
* {{ my_str|url_encode }}
* // => param%3D1%26anotherParam%3D2
*
* @param {*} input
* @return {*} URL-encoded string.
*/
exports.url_encode = function (input) {
var out = iterateFilter.apply(exports.url_encode, arguments)
if (out !== undefined) {
return out
}
return encodeURIComponent(input)
}
/**
* URL-decode a string. If an object or array is passed, all values will be URL-decoded.
*
* @example
* // my_str = 'param%3D1%26anotherParam%3D2';
* {{ my_str|url_decode }}
* // => param=1&anotherParam=2
*
* @param {*} input
* @return {*} URL-decoded string.
*/
exports.url_decode = function (input) {
var out = iterateFilter.apply(exports.url_decode, arguments)
if (out !== undefined) {
return out
}
return decodeURIComponent(input)
}