@shahen.poghosyan/awilix
Version:
Extremely powerful dependency injection container.
114 lines • 2.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
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;
}
exports.flatten = flatten;
/**
* 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) {
let obj = name;
if (typeof obj === 'string' || typeof obj === 'symbol') {
return { [name]: value };
}
return obj;
}
exports.nameValueToObject = nameValueToObject;
/**
* Returns the last item in the array.
*
* @param {*[]} arr
* The array.
*
* @return {*}
* The last element.
*/
function last(arr) {
return arr[arr.length - 1];
}
exports.last = last;
/**
* Determines if the given function is a class.
*
* @param {Function} fn
* @return {Boolean}
*/
function isClass(fn) {
/*tslint:disable-next-line*/
if (typeof fn !== 'function') {
return false;
}
// Should only need 2 tokens.
const tokenizer = 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;
}
exports.isClass = isClass;
/**
* Determines if the given value is a function.
*
* @param {Any} val
* Any value to check if it's a function.
*
* @return {Boolean}
* true if the value is a function, false otherwise.
*/
function isFunction(val) {
return typeof val === 'function';
}
exports.isFunction = isFunction;
/**
* 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) {
const result = [];
for (const idx in arr) {
const item = arr[idx];
if (result.indexOf(item) === -1) {
result.push(item);
}
}
return result;
}
exports.uniq = uniq;
//# sourceMappingURL=utils.js.map