src/util.js
/*
* @module Util
*/
/*
* Reference to the window object
* @private
*/
export const win = typeof window !== 'undefined' ? window : {};
/**
* Convert `NodeList` to `Array`.
*
* @param {NodeList|Array} collection
* @return {Array}
* @private
*/
export const toArray = collection => {
const length = collection.length;
const result = new Array(length);
for(let i = 0; i < length; i++) {
result[i] = collection[i];
}
return result;
};
/**
* Faster alternative to [].forEach method
*
* @param {Node|NodeList|Array} collection
* @param {Function} callback
* @return {Node|NodeList|Array}
* @private
*/
export const each = (collection, callback, thisArg) => {
const length = collection.length;
if(length !== undefined && collection.nodeType === undefined) {
for(let i = 0; i < length; i++) {
callback.call(thisArg, collection[i], i, collection);
}
} else {
callback.call(thisArg, collection, 0, collection);
}
return collection;
};
/**
* Assign enumerable properties from source object(s) to target object
*
* @method extend
* @param {Object} target Object to extend
* @param {Object} [source] Object to extend from
* @return {Object} Extended object
* @example
* $.extend({a: 1}, {b: 2}); // {a: 1, b: 2}
* @example
* $.extend({a: 1}, {b: 2}, {a: 3}); // {a: 3, b: 2}
*/
export const extend = (target, ...sources) => {
sources.forEach(src => {
for(let prop in src) {
target[prop] = src[prop];
}
});
return target;
};
/**
* Return the collection without duplicates
*
* @param collection Collection to remove duplicates from
* @return {Node|NodeList|Array}
* @private
*/
export const uniq = collection => collection.filter((item, index) => collection.indexOf(item) === index);