@tsed/cli-core 6.1.13 → 6.1.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,515 +0,0 @@
1
- import { getValue, isArray, isNumber, isObject, isString } from "@tsed/core";
2
- // @ts-ignore
3
- import createFrame from "create-frame";
4
- // @ts-ignore
5
- import util from "handlebars-utils";
6
- export const helpers = {};
7
- /**
8
- * Returns all of the items in an array after the specified index.
9
- * Opposite of [before](#before).
10
- *
11
- * ```handlebars
12
- * <!-- array: ['a', 'b', 'c'] -->
13
- * {{after array 1}}
14
- * <!-- results in: '["c"]' -->
15
- * ```
16
- * @param {Array} `array` Collection
17
- * @param {Number} `n` Starting index (number of items to exclude)
18
- * @return {Array} Array exluding `n` items.
19
- * @api public
20
- */
21
- helpers.after = function (array, n) {
22
- if (util.isUndefined(array))
23
- return "";
24
- return array.slice(n);
25
- };
26
- /**
27
- * Cast the given `value` to an array.
28
- *
29
- * ```handlebars
30
- * {{arrayify "foo"}}
31
- * <!-- results in: [ "foo" ] -->
32
- * ```
33
- * @param {any} `value`
34
- * @return {Array}
35
- * @api public
36
- */
37
- helpers.arrayify = function (value) {
38
- return value ? (isArray(value) ? value : [value]) : [];
39
- };
40
- /**
41
- * Return all of the items in the collection before the specified
42
- * count. Opposite of [after](#after).
43
- *
44
- * ```handlebars
45
- * <!-- array: ['a', 'b', 'c'] -->
46
- * {{before array 2}}
47
- * <!-- results in: '["a", "b"]' -->
48
- * ```
49
- * @param {Array} `array`
50
- * @param {Number} `n`
51
- * @return {Array} Array excluding items after the given number.
52
- * @api public
53
- */
54
- helpers.before = function (array, n) {
55
- if (util.isUndefined(array))
56
- return "";
57
- return array.slice(0, -n);
58
- };
59
- /**
60
- * ```handlebars
61
- * <!-- array: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] -->
62
- * {{#eachIndex array}}
63
- * {{item}} is {{index}}
64
- * {{/eachIndex}}
65
- * ```
66
- * @param {Array} `array`
67
- * @param {Object} `options`
68
- * @return {String}
69
- * @block
70
- * @api public
71
- */
72
- helpers.eachIndex = function (array, options) {
73
- let result = "";
74
- for (let i = 0; i < array.length; i++) {
75
- result += options.fn({ item: array[i], index: i });
76
- }
77
- return result;
78
- };
79
- /**
80
- * Block helper that filters the given array and renders the block for values that
81
- * evaluate to `true`, otherwise the inverse block is returned.
82
- *
83
- * ```handlebars
84
- * <!-- array: ['a', 'b', 'c'] -->
85
- * {{#filter array "foo"}}AAA{{else}}BBB{{/filter}}
86
- * <!-- results in: 'BBB' -->
87
- * ```
88
- * @param {Array} `array`
89
- * @param {any} `value`
90
- * @param {Object} `options`
91
- * @return {String}
92
- * @block
93
- * @api public
94
- */
95
- helpers.filter = function (array, value, options) {
96
- let content = "";
97
- let results = [];
98
- // filter on a specific property
99
- const prop = options.hash && (options.hash.property || options.hash.prop);
100
- if (prop) {
101
- results = array.filter(function (val) {
102
- return value === getValue(val, prop);
103
- });
104
- }
105
- else {
106
- // filter on a string value
107
- results = array.filter(function (v) {
108
- return value === v;
109
- });
110
- }
111
- if (results && results.length > 0) {
112
- for (let i = 0; i < results.length; i++) {
113
- content += options.fn(results[i]);
114
- }
115
- return content;
116
- }
117
- return options.inverse(this);
118
- };
119
- /**
120
- * Returns the first item, or first `n` items of an array.
121
- *
122
- * ```handlebars
123
- * {{first "['a', 'b', 'c', 'd', 'e']" 2}}
124
- * <!-- results in: '["a", "b"]' -->
125
- * ```
126
- * @param {Array} `array`
127
- * @param {Number} `n` Number of items to return, starting at `0`.
128
- * @return {Array}
129
- * @api public
130
- */
131
- helpers.first = function (array, n) {
132
- if (array === undefined)
133
- return "";
134
- if (!isNumber(n)) {
135
- return array[0];
136
- }
137
- return array.slice(0, n);
138
- };
139
- /**
140
- * Iterates over each item in an array and exposes the current item
141
- * in the array as context to the inner block. In addition to
142
- * the current array item, the helper exposes the following variables
143
- * to the inner block:
144
- *
145
- * - `index`
146
- * - `total`
147
- * - `isFirst`
148
- * - `isLast`
149
- *
150
- * Also, `@index` is exposed as a private variable, and additional
151
- * private variables may be defined as hash arguments.
152
- *
153
- * ```handlebars
154
- * <!-- accounts = [
155
- * {'name': 'John', 'email': 'john@example.com'},
156
- * {'name': 'Malcolm', 'email': 'malcolm@example.com'},
157
- * {'name': 'David', 'email': 'david@example.com'}
158
- * ] -->
159
- *
160
- * {{#forEach accounts}}
161
- * <a href="mailto:{{ email }}" title="Send an email to {{ name }}">
162
- * {{ name }}
163
- * </a>{{#unless isLast}}, {{/unless}}
164
- * {{/forEach}}
165
- * ```
166
- * @source <http://stackoverflow.com/questions/13861007>
167
- * @param {Array} `array`
168
- * @return {String}
169
- * @block
170
- * @api public
171
- */
172
- helpers.forEach = function (array, options) {
173
- const data = createFrame(options, options.hash);
174
- const len = array.length;
175
- let buffer = "";
176
- let i = -1;
177
- while (++i < len) {
178
- const item = array[i];
179
- data.index = i;
180
- item.index = i + 1;
181
- item.total = len;
182
- item.isFirst = i === 0;
183
- item.isLast = i === len - 1;
184
- buffer += options.fn(item, { data: data });
185
- }
186
- return buffer;
187
- };
188
- /**
189
- * Block helper that renders the block if an array has the
190
- * given `value`. Optionally specify an inverse block to render
191
- * when the array does not have the given value.
192
- *
193
- * ```handlebars
194
- * <!-- array: ['a', 'b', 'c'] -->
195
- * {{#inArray array "d"}}
196
- * foo
197
- * {{else}}
198
- * bar
199
- * {{/inArray}}
200
- * <!-- results in: 'bar' -->
201
- * ```
202
- * @param {Array} `array`
203
- * @param {any} `value`
204
- * @param {Object} `options`
205
- * @return {String}
206
- * @block
207
- * @api public
208
- */
209
- helpers.inArray = function (array, value, options) {
210
- return util.value(util.indexOf(array, value) > -1, this, options);
211
- };
212
- /**
213
- * Returns true if `value` is an es5 array.
214
- *
215
- * ```handlebars
216
- * {{isArray "abc"}}
217
- * <!-- results in: false -->
218
- *
219
- * <!-- array: [1, 2, 3] -->
220
- * {{isArray array}}
221
- * <!-- results in: true -->
222
- * ```
223
- * @param {any} `value` The value to test.
224
- * @return {Boolean}
225
- * @api public
226
- */
227
- helpers.isArray = function (value) {
228
- return Array.isArray(value);
229
- };
230
- /**
231
- * Returns the item from `array` at index `idx`.
232
- *
233
- * ```handlebars
234
- * <!-- array: ['a', 'b', 'c'] -->
235
- * {{itemAt array 1}}
236
- * <!-- results in: 'b' -->
237
- * ```
238
- * @param {Array} `array`
239
- * @param {Number} `idx`
240
- * @return {any} `value`
241
- * @block
242
- * @api public
243
- */
244
- helpers.itemAt = function (array, idx) {
245
- array = util.result(array);
246
- if (isArray(array)) {
247
- idx = isNumber(idx) ? +idx : 0;
248
- if (idx < 0) {
249
- return array[array.length + idx];
250
- }
251
- if (idx < array.length) {
252
- return array[idx];
253
- }
254
- }
255
- };
256
- /**
257
- * Join all elements of array into a string, optionally using a
258
- * given separator.
259
- *
260
- * ```handlebars
261
- * <!-- array: ['a', 'b', 'c'] -->
262
- * {{join array}}
263
- * <!-- results in: 'a, b, c' -->
264
- *
265
- * {{join array '-'}}
266
- * <!-- results in: 'a-b-c' -->
267
- * ```
268
- * @param {Array} `array`
269
- * @param {String} `separator` The separator to use. Defaults to `, `.
270
- * @return {String}
271
- * @api public
272
- */
273
- helpers.join = function (array, separator) {
274
- if (isString(array))
275
- return array;
276
- if (!isArray(array))
277
- return "";
278
- separator = util.isString(separator) ? separator : ", ";
279
- return array.join(separator);
280
- };
281
- /**
282
- * Returns true if the the length of the given `value` is equal
283
- * to the given `length`. Can be used as a block or inline helper.
284
- *
285
- * @param {Array|String} `value`
286
- * @param {Number} `length`
287
- * @param {Object} `options`
288
- * @return {String}
289
- * @block
290
- * @api public
291
- */
292
- helpers.equalsLength = function (value, length, options) {
293
- if (util.isOptions(length)) {
294
- options = length;
295
- length = 0;
296
- }
297
- let len = 0;
298
- if (typeof value === "string" || Array.isArray(value)) {
299
- len = value.length;
300
- }
301
- return util.value(len === length, this, options);
302
- };
303
- /**
304
- * Returns the last item, or last `n` items of an array or string.
305
- * Opposite of [first](#first).
306
- *
307
- * ```handlebars
308
- * <!-- var value = ['a', 'b', 'c', 'd', 'e'] -->
309
- *
310
- * {{last value}}
311
- * <!-- results in: ['e'] -->
312
- *
313
- * {{last value 2}}
314
- * <!-- results in: ['d', 'e'] -->
315
- *
316
- * {{last value 3}}
317
- * <!-- results in: ['c', 'd', 'e'] -->
318
- * ```
319
- * @param {Array|String} `value` Array or string.
320
- * @param {Number} `n` Number of items to return from the end of the array.
321
- * @return {Array}
322
- * @api public
323
- */
324
- helpers.last = function (value, n) {
325
- if (!isArray(value) && typeof value !== "string") {
326
- return "";
327
- }
328
- if (!isNumber(n)) {
329
- return value[value.length - 1];
330
- }
331
- return value.slice(-Math.abs(n));
332
- };
333
- /**
334
- * Returns the length of the given string or array.
335
- *
336
- * ```handlebars
337
- * {{length '["a", "b", "c"]'}}
338
- * <!-- results in: 3 -->
339
- *
340
- * <!-- results in: myArray = ['a', 'b', 'c', 'd', 'e']; -->
341
- * {{length myArray}}
342
- * <!-- results in: 5 -->
343
- *
344
- * <!-- results in: myObject = {'a': 'a', 'b': 'b'}; -->
345
- * {{length myObject}}
346
- * <!-- results in: 2 -->
347
- * ```
348
- * @param {Array|Object|String} `value`
349
- * @return {Number} The length of the value.
350
- * @api public
351
- */
352
- helpers.length = function (value) {
353
- if (isObject(value) && !util.isOptions(value)) {
354
- value = Object.keys(value);
355
- }
356
- if (typeof value === "string" || Array.isArray(value)) {
357
- return value.length;
358
- }
359
- return 0;
360
- };
361
- /**
362
- * Returns a new array, created by calling `function` on each
363
- * element of the given `array`. For example,
364
- *
365
- * ```handlebars
366
- * <!-- array: ['a', 'b', 'c'], and "double" is a
367
- * fictitious function that duplicates letters -->
368
- * {{map array double}}
369
- * <!-- results in: '["aa", "bb", "cc"]' -->
370
- * ```
371
- *
372
- * @param {Array} `array`
373
- * @param {Function} `fn`
374
- * @return {String}
375
- * @api public
376
- */
377
- helpers.map = function (array, iter) {
378
- if (!Array.isArray(array))
379
- return "";
380
- const len = array.length;
381
- const res = new Array(len);
382
- let i = -1;
383
- if (typeof iter !== "function") {
384
- return array;
385
- }
386
- while (++i < len) {
387
- res[i] = iter(array[i], i, array);
388
- }
389
- return res;
390
- };
391
- /**
392
- * Map over the given object or array or objects and create an array of values
393
- * from the given `prop`. Dot-notation may be used (as a string) to get
394
- * nested properties.
395
- *
396
- * ```handlebars
397
- * // {{pluck items "data.title"}}
398
- * <!-- results in: '["aa", "bb", "cc"]' -->
399
- * ```
400
- * @param {Array|Object} `collection`
401
- * @param {Function} `prop`
402
- * @return {String}
403
- * @api public
404
- */
405
- helpers.pluck = function (arr, prop) {
406
- if (util.isUndefined(arr))
407
- return "";
408
- const res = [];
409
- for (let i = 0; i < arr.length; i++) {
410
- const val = getValue(arr[i], prop);
411
- if (typeof val !== "undefined") {
412
- res.push(val);
413
- }
414
- }
415
- return res;
416
- };
417
- /**
418
- * Reverse the elements in an array, or the characters in a string.
419
- *
420
- * ```handlebars
421
- * <!-- value: 'abcd' -->
422
- * {{reverse value}}
423
- * <!-- results in: 'dcba' -->
424
- * <!-- value: ['a', 'b', 'c', 'd'] -->
425
- * {{reverse value}}
426
- * <!-- results in: ['d', 'c', 'b', 'a'] -->
427
- * ```
428
- * @param {Array|String} `value`
429
- * @return {Array|String} Returns the reversed string or array.
430
- * @api public
431
- */
432
- helpers.reverse = function (val) {
433
- if (Array.isArray(val)) {
434
- val.reverse();
435
- return val;
436
- }
437
- if (val && isString(val)) {
438
- return val.split("").reverse().join("");
439
- }
440
- };
441
- /**
442
- * Block helper that returns the block if the callback returns true
443
- * for some value in the given array.
444
- *
445
- * ```handlebars
446
- * <!-- array: [1, 'b', 3] -->
447
- * {{#some array isString}}
448
- * Render me if the array has a string.
449
- * {{else}}
450
- * Render me if it doesn't.
451
- * {{/some}}
452
- * <!-- results in: 'Render me if the array has a string.' -->
453
- * ```
454
- * @param {Array} `array`
455
- * @param {Function} `iter` Iteratee
456
- * @param {Options} Handlebars provided options object
457
- * @return {String}
458
- * @block
459
- * @api public
460
- */
461
- helpers.some = function (array, iter, options) {
462
- if (Array.isArray(array)) {
463
- for (let i = 0; i < array.length; i++) {
464
- if (iter(array[i], i, array)) {
465
- return options.fn(this);
466
- }
467
- }
468
- }
469
- return options.inverse(this);
470
- };
471
- /**
472
- * Sort the given `array`. If an array of objects is passed,
473
- * you may optionally pass a `key` to sort on as the second
474
- * argument. You may alternatively pass a sorting function as
475
- * the second argument.
476
- *
477
- * ```handlebars
478
- * <!-- array: ['b', 'a', 'c'] -->
479
- * {{sort array}}
480
- * <!-- results in: '["a", "b", "c"]' -->
481
- * ```
482
- *
483
- * @param {Array} `array` the array to sort.
484
- * @param {String|Function} `key` The object key to sort by, or sorting function.
485
- * @api public
486
- */
487
- helpers.sort = function (array, options) {
488
- if (!Array.isArray(array))
489
- return "";
490
- if (getValue(options, "hash.reverse")) {
491
- return array.sort().reverse();
492
- }
493
- return array.sort();
494
- };
495
- /**
496
- * Block helper that return an array with all duplicate
497
- * values removed. Best used along with a [each](#each) helper.
498
- *
499
- * ```handlebars
500
- * <!-- array: ['a', 'a', 'c', 'b', 'e', 'e'] -->
501
- * {{#each (unique array)}}{{.}}{{/each}}
502
- * <!-- results in: 'acbe' -->
503
- * ```
504
- * @param {Array} `array`
505
- * @param {Object} `options`
506
- * @return {Array}
507
- * @api public
508
- */
509
- helpers.unique = function (array) {
510
- if (util.isUndefined(array))
511
- return "";
512
- return array.filter(function (item, index, arr) {
513
- return arr.indexOf(item) === index;
514
- });
515
- };
@@ -1,60 +0,0 @@
1
- // @ts-ignore
2
- import util from "handlebars-utils";
3
- import { helpers as array } from "./array.js";
4
- import { helpers as object } from "./object.js";
5
- const forEach = array.forEach;
6
- const forOwn = object.forOwn;
7
- export const helpers = {};
8
- /**
9
- * Inline, subexpression, or block helper that returns true (or the block)
10
- * if the given collection is empty, or false (or the inverse block, if
11
- * supplied) if the colleciton is not empty.
12
- *
13
- * ```handlebars
14
- * <!-- array: [] -->
15
- * {{#isEmpty array}}AAA{{else}}BBB{{/isEmpty}}
16
- * <!-- results in: 'AAA' -->
17
- *
18
- * <!-- array: [] -->
19
- * {{isEmpty array}}
20
- * <!-- results in: true -->
21
- * ```
22
- * @param {Object} `collection`
23
- * @param {Object} `options`
24
- * @return {String}
25
- * @block
26
- * @api public
27
- */
28
- helpers.isEmpty = function (collection, options) {
29
- if (!util.isOptions(options)) {
30
- options = collection;
31
- return util.fn(true, this, options);
32
- }
33
- if (Array.isArray(collection) && !collection.length) {
34
- return util.fn(true, this, options);
35
- }
36
- const keys = Object.keys(collection);
37
- const isEmpty = typeof collection === "object" && !keys.length;
38
- return util.value(isEmpty, this, options);
39
- };
40
- /**
41
- * Block helper that iterates over an array or object. If
42
- * an array is given, `.forEach` is called, or if an object
43
- * is given, `.forOwn` is called, otherwise the inverse block
44
- * is returned.
45
- *
46
- * @param {Object|Array} `collection` The collection to iterate over
47
- * @param {Object} `options`
48
- * @return {String}
49
- * @block
50
- * @api public
51
- */
52
- helpers.iterate = function (collection, options) {
53
- if (Array.isArray(collection)) {
54
- return forEach.apply(null, [collection, options]);
55
- }
56
- if (util.isObject(collection)) {
57
- return forOwn.apply(null, [collection, options]);
58
- }
59
- return options.inverse(this);
60
- };