UNPKG

awilix

Version:

Extremely powerful dependency injection container.

109 lines 2.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.flatten = flatten; exports.nameValueToObject = nameValueToObject; exports.last = last; exports.isClass = isClass; exports.isFunction = isFunction; exports.uniq = uniq; const function_tokenizer_1 = require("./function-tokenizer"); /** * Quick flatten utility to flatten a 2-dimensional array. * * @param {Array<Array<Item>>} array * The array to flatten. * * @return {Array<Item>} * The flattened array. */ function flatten(array) { const result = []; array.forEach((arr) => { arr.forEach((item) => { result.push(item); }); }); return result; } /** * Creates a { name: value } object if the input isn't already in that format. * * @param {string|object} name * Either a string or an object. * * @param {*} value * The value, only used if name is not an object. * * @return {object} */ function nameValueToObject(name, value) { const obj = name; if (typeof obj === 'string' || typeof obj === 'symbol') { return { [name]: value }; } return obj; } /** * Returns the last item in the array. * * @param {*[]} arr * The array. * * @return {*} * The last element. */ function last(arr) { return arr[arr.length - 1]; } /** * Determines if the given function is a class. * * @param {Function} fn * @return {boolean} */ function isClass( // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type fn) { if (typeof fn !== 'function') { return false; } // Should only need 2 tokens. const tokenizer = (0, function_tokenizer_1.createTokenizer)(fn.toString()); const first = tokenizer.next(); if (first.type === 'class') { return true; } const second = tokenizer.next(); if (first.type === 'function' && second.value) { if (second.value[0] === second.value[0].toUpperCase()) { return true; } } return false; } /** * Determines if the given value is a function. * * @param {unknown} val * Any value to check if it's a function. * * @return {boolean} * true if the value is a function, false otherwise. */ // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type function isFunction(val) { return typeof val === 'function'; } /** * Returns the unique items in the array. * * @param {Array<T>} * The array to remove dupes from. * * @return {Array<T>} * The deduped array. */ function uniq(arr) { return Array.from(new Set(arr)); } //# sourceMappingURL=utils.js.map