UNPKG

2.58 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.uniq = exports.isFunction = exports.isClass = exports.last = exports.nameValueToObject = exports.flatten = void 0;
4const function_tokenizer_1 = require("./function-tokenizer");
5/**
6 * Quick flatten utility to flatten a 2-dimensional array.
7 *
8 * @param {Array<Array<Item>>} array
9 * The array to flatten.
10 *
11 * @return {Array<Item>}
12 * The flattened array.
13 */
14function flatten(array) {
15 const result = [];
16 array.forEach((arr) => {
17 arr.forEach((item) => {
18 result.push(item);
19 });
20 });
21 return result;
22}
23exports.flatten = flatten;
24/**
25 * Creates a { name: value } object if the input isn't already in that format.
26 *
27 * @param {string|object} name
28 * Either a string or an object.
29 *
30 * @param {*} value
31 * The value, only used if name is not an object.
32 *
33 * @return {object}
34 */
35function nameValueToObject(name, value) {
36 let obj = name;
37 if (typeof obj === 'string' || typeof obj === 'symbol') {
38 return { [name]: value };
39 }
40 return obj;
41}
42exports.nameValueToObject = nameValueToObject;
43/**
44 * Returns the last item in the array.
45 *
46 * @param {*[]} arr
47 * The array.
48 *
49 * @return {*}
50 * The last element.
51 */
52function last(arr) {
53 return arr[arr.length - 1];
54}
55exports.last = last;
56/**
57 * Determines if the given function is a class.
58 *
59 * @param {Function} fn
60 * @return {boolean}
61 */
62function isClass(fn) {
63 /*tslint:disable-next-line*/
64 if (typeof fn !== 'function') {
65 return false;
66 }
67 // Should only need 2 tokens.
68 const tokenizer = (0, function_tokenizer_1.createTokenizer)(fn.toString());
69 const first = tokenizer.next();
70 if (first.type === 'class') {
71 return true;
72 }
73 const second = tokenizer.next();
74 if (first.type === 'function' && second.value) {
75 if (second.value[0] === second.value[0].toUpperCase()) {
76 return true;
77 }
78 }
79 return false;
80}
81exports.isClass = isClass;
82/**
83 * Determines if the given value is a function.
84 *
85 * @param {Any} val
86 * Any value to check if it's a function.
87 *
88 * @return {boolean}
89 * true if the value is a function, false otherwise.
90 */
91function isFunction(val) {
92 return typeof val === 'function';
93}
94exports.isFunction = isFunction;
95/**
96 * Returns the unique items in the array.
97 *
98 * @param {Array<T>}
99 * The array to remove dupes from.
100 *
101 * @return {Array<T>}
102 * The deduped array.
103 */
104function uniq(arr) {
105 return Array.from(new Set(arr));
106}
107exports.uniq = uniq;
108//# sourceMappingURL=utils.js.map
\No newline at end of file