UNPKG

private-bower

Version:
503 lines (412 loc) 13.6 kB
/*! * Copyright © 2014 Hacklone, * https://github.com/Hacklone * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation files * (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ QArray = (function() { function QArray() { for(var i = 0, len = arguments.length; i < len; i++) { var arg = arguments[i]; if(arg instanceof QArray || isArray(arg)) { this.pushArray(arg); } } } QArray.prototype = []; /** * Returns the index of the item * @param {Object} item * @return {Number} */ QArray.prototype.indexOf = QArray.prototype.indexOf || function(item) { for(var i = 0, len = this.length; i < len; i++) { if(this[i] === item) { return i; } } return -1; }; /** * Inserts an item to the given index * @param {Number} index * @param {Object} item * @return {QArray} */ QArray.prototype.insert = function(index, item) { this.splice.apply(this, [index, 0].concat(Array.prototype.slice.call(arguments, 1))); return this; }; /** * Concats Arrays to QArray * @param {...Array} arrays * @return {QArray} */ QArray.prototype.concat = function() { for(var i = 0, len = arguments.length; i < len; i++) { var arg = arguments[i]; if(arg instanceof QArray || isArray(arg)) { this.pushArray(arg); } } return this; }; /** * Selects the property or new objects from QArray * @param {String|Function} propertyNameOrFunction * @return {QArray} */ QArray.prototype.select = function(propertyNameOrFunction) { var returnArray = new QArray(); if(isString(propertyNameOrFunction)) { for(var i = 0, len = this.length; i < len; i++) { returnArray.push(this[i][propertyNameOrFunction]); } } else if(isFunction(propertyNameOrFunction)) { //could use map -> should check for(var j = 0, leng = this.length; j < leng; j++) { returnArray.push(propertyNameOrFunction(this[j])); } } else { for(var k = 0, length = this.length; k < length; k++) { returnArray.push(this[k]); } } return returnArray; }; /** * Selects the property or new objects from contained Arrays * @param {String|Function} propertyNameOrFunction * @return {QArray} */ QArray.prototype.selectMany = function(propertyNameOrFunction) { var returnArray = new QArray(); for(var i = 0, len = this.length; i < len; i++) { var item = this[i]; if(item instanceof Array) { returnArray.pushArray(new QArray(item).select(propertyNameOrFunction)); } else if(item instanceof QArray) { returnArray.pushArray(item.select(propertyNameOrFunction)); } } return returnArray; }; /** * Iterates through the elements of the QArray * @param {Function} expression * @return {QArray} */ QArray.prototype.forEach = function(expression) { for(var i = 0, len = this.length; i < len; i++) { expression(this[i], i); } return this; }; /** * Sets the given property of all items * @param {String} propertyName * @param {Object|Function} valueOrFunction * @return {QArray} */ QArray.prototype.setProperty = function(propertyName, valueOrFunction) { var isFunctionValue = isFunction(valueOrFunction); for(var i = 0, len = this.length; i < len; i++) { var item = this[i]; if(isFunctionValue) { item[propertyName] = valueOrFunction(item); } else { item[propertyName] = valueOrFunction; } } return this; }; /** * Push the items from the given array to the QArray * @param {...Array} arrays * @return {QArray} */ QArray.prototype.pushArray = function() { for(var i = 0, len = arguments.length; i < len; i++) { var array = arguments[i]; if(array instanceof Array) { if(array.length > 10000) { manualPush.apply(this); } else { Array.prototype.push.apply(this, array instanceof QArray ? array.toArray() : array); } } } return this; function manualPush() { for(var i = 0, len = array.length; i < len; i++) { this.push(array[i]); } } }; /** * Returns if the QArray contains the given item * @param {Object} item * @return {Boolean} */ QArray.prototype.contains = function(item) { return this.indexOf(item) !== -1; }; /** * Removes the specified item from the QArray * @param {Object} item * @return {QArray} */ QArray.prototype.remove = function(item) { var index = this.indexOf(item); if(index !== -1) { this.splice(index, 1); } return this; }; /** * Removes the specified items from the QArray in the given Array * @param {Array|QArray} array * @return {QArray} */ QArray.prototype.removeArray = function(array) { if(!isArray(array)) { return this; } for(var i = 0, len = array.length; i < len; i++) { this.remove(array[i]); } return this; }; /** * Converts the QArray to an ordinary Array * @return {Array} */ QArray.prototype.toArray = function() { var returnArray = []; for(var i = 0, len = this.length; i < len; i++) { returnArray.push(this[i]); } return returnArray; }; /** * Clears the Array * @return {QArray} */ QArray.prototype.clear = function() { this.length = 0; return this; }; /** * Returns the first item that fulfills the conditions (If not found returns null) * @param {Object|Function} configOrExpression * @return {Object|null} */ QArray.prototype.firstOrDefault = function(configOrExpression) { if(!configOrExpression) { return this.length ? this[0] : null; } return isFunction(configOrExpression) ? firstOrDefaultExp.apply(this) : firstOrDefaultConf.apply(this); function firstOrDefaultConf() { for(var i = 0, len = this.length; i < len; i++) { var item = this[i]; if(item[configOrExpression.key] === configOrExpression.value) { return item; } } return null; } function firstOrDefaultExp() { for(var i = 0, len = this.length; i < len; i++) { var item = this[i]; if(configOrExpression(item, i)) { return item; } } return null; } }; /** * Returns the first item that fulfills the conditions (If not found throws Error) * @param {Object|Function} configOrExpression * @return {Object} */ QArray.prototype.first = function(configOrExpression) { var first = this.firstOrDefault(configOrExpression); if(first === null) { throw new Error('The Array does not contain the desired element'); } return first; }; /** * Returns the items that fulfill the given conditions * @param {Object|Function} configOrExpression * @return {QArray} */ QArray.prototype.where = function(configOrExpression) { var returnArray = new QArray(); if(isFunction(configOrExpression)) { whereExp.apply(this); } else { whereConf.apply(this); } return returnArray; function whereConf() { for(var i = 0, len = this.length; i < len; i++) { var item = this[i]; if(item[configOrExpression.key] === configOrExpression.value) { returnArray.push(item); } } } function whereExp() { for(var i = 0, len = this.length; i < len; i++) { var item = this[i]; if(configOrExpression(item, i)) { returnArray.push(item); } } } }; /** * Returns the number of items that fulfill the given conditions * @param {Object|Function} configOrExpression * @return {Number} */ QArray.prototype.count = function(configOrExpression) { var countNumber = 0; if(isFunction(configOrExpression)) { countExp.apply(this); } else { countConf.apply(this); } return countNumber; function countConf() { for(var i = 0, len = this.length; i < len; i++) { var item = this[i]; if(item[configOrExpression.key] === configOrExpression.value) { countNumber++; } } } function countExp() { for(var i = 0, len = this.length; i < len; i++) { var item = this[i]; if(configOrExpression(item, i)) { countNumber++; } } } }; /** * Returns if any item fulfills the given conditions * @param {Object|Function} configOrExpression * @return {Boolean} */ QArray.prototype.any = function(configOrExpression) { return this.firstOrDefault(configOrExpression) !== null; }; /** * Returns if all of the items fulfill the given conditions * @param {Object|Function} configOrExpression * @return {Boolean} */ QArray.prototype.all = function(configOrExpression) { if(!configOrExpression) { return null; } return isFunction(configOrExpression) ? allExp.apply(this) : allConf.apply(this); function allConf() { for(var i = 0, len = this.length; i < len; i++) { var item = this[i]; if(item[configOrExpression.key] !== configOrExpression.value) { return false; } } return true; } function allExp() { for(var i = 0, len = this.length; i < len; i++) { var item = this[i]; if(!configOrExpression(item, i)) { return false; } } return true; } }; /** * Sums the given property * @param {String|Function} propertyNameOrExpression * @return {Number} */ QArray.prototype.sum = function(propertyNameOrExpression) { if(!propertyNameOrExpression) { return 0; } return isFunction(propertyNameOrExpression) ? sumExp.apply(this) : sumProp.apply(this); function sumProp() { var sum = 0; for(var i = 0, len = this.length; i < len; i++) { sum += this[i][propertyNameOrExpression] || 0; } return sum; } function sumExp() { var sum = 0; for(var i = 0, len = this.length; i < len; i++) { sum += propertyNameOrExpression(this[i]) || 0; } return sum; } }; /** * Move the item to the given index * @param {Object} item * @param {String} index * @return {QArray} */ QArray.prototype.moveItemToIndex = function(item, index) { if(index < 0 || index >= this.length || !this.contains(item)) { return this; } this.remove(item).splice(index, 0, item); return this; }; //region helpers function isFunction(value) { return typeof value === 'function'; } function isArray(value) { return toString.call(value) === '[object Array]'; } function isString(value) { return typeof value === 'string'; } //endregion helpers return QArray; })();