This repository has been archived by the owner on Dec 25, 2018. It is now read-only.
/
Utility.js.map
1 lines (1 loc) · 23.4 KB
/
Utility.js.map
1
{"version":3,"sources":["System/Collections/Array/Utility.js","System/Collections/Array/Utility.ts"],"names":["define","require","exports","Types_1","Integer_1","Compare_1","ArgumentException_1","ArgumentNullException_1","ArgumentOutOfRangeException_1","initialize","length","assert","array","Array","copy","source","sourceIndex","Infinity","copyTo","Math","min","max","destination","destinationIndex","CBN","CBL0","sourceLength","maxLength","isFinite","i","contains","item","equalityComparer","areEqual","isArray","indexOf","replace","old","newValue","count","updateRange","value","index","end","clear","register","len","ok","findIndex","predicate","isFunction","forEach","fn","applyTo","target","removeIndex","exists","splice","remove","repeat","element","result","push","flatten","a","recurseDepth","x","n"],"mappings":";;;;AAIAA,QAAQ,UAAW,UAAW,cAAe,gBAAiB,gBAAiB,qCAAsC,yCAA0C,gDAAiD,SAAUC,EAASC,EAASC,EAASC,EAAWC,EAAWC,EAAqBC,EAAyBC,GACrT,YCeJ,SAAAC,GAA8BC,GAE7BN,EAAAA,WAAQO,OAAOD,EAAQ,SAEvB,IAAIE,EAQJ,OAPGF,GAAO,MACTE,EAAQ,GAAIC,OAAMH,IAGlBE,KACAA,EAAMF,OAASA,GAETE,EAUR,QAAAE,GACCC,EACAC,EACAN,GAEA,MAHA,UAAAM,IAAAA,EAAA,GACA,SAAAN,IAAAA,EAAAO,EAAAA,GAEIF,EACGG,EACNH,EACAN,EAAcU,KAAKC,IAAIV,EAAQS,KAAKE,IAAIN,EAAOL,OAASM,EAAa,KACrEA,EAAa,EAAGN,GAJOK,EAoBzB,QAAAG,GACCH,EACAO,EACAN,EACAO,EACAb,GAEA,GAJA,SAAAM,IAAAA,EAAA,GACA,SAAAO,IAAAA,EAAA,GACA,SAAAb,IAAAA,EAAAO,EAAAA,IAEIF,EACH,KAAM,IAAIR,GAAAA,WAAsB,SAAUiB,EAE3C,KAAIF,EACH,KAAM,IAAIf,GAAAA,WAAsB,cAAeiB,EAEhD,I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OL,GA1XQ3D,EAAAO,WAAUA,EAsBVP,EAAAY,KAAIA,CAYpB,IACCU,GAAM,kBACNC,EAAO,2BAWQvB,GAAAgB,OAAMA,EA6CNhB,EAAA4B,SAAQA,EA4BR5B,EAAAkC,QAAOA,EAsCPlC,EAAAsC,YAAWA,EAsBXtC,EAAA0C,MAAKA,EAeL1C,EAAA2C,SAAQA,EAmBR3C,EAAA8C,UAASA,EAwBT9C,EAAAiD,QAAOA,EA0BPjD,EAAAmD,QAAOA,EAqBPnD,EAAAqD,YAAWA,EAuBXrD,EAAAwD,OAAMA,EAoCNxD,EAAAyD,OAAMA,EAqBNzD,EAAA6D,QAAOA","file":"System/Collections/Array/Utility.js","sourcesContent":["/*!\n * @author electricessence / https://github.com/electricessence/\n * Licensing: MIT https://github.com/electricessence/TypeScript.NET/blob/master/LICENSE.md\n */\ndefine([\"require\", \"exports\", \"../../Types\", \"../../Integer\", \"../../Compare\", \"../../Exceptions/ArgumentException\", \"../../Exceptions/ArgumentNullException\", \"../../Exceptions/ArgumentOutOfRangeException\"], function (require, exports, Types_1, Integer_1, Compare_1, ArgumentException_1, ArgumentNullException_1, ArgumentOutOfRangeException_1) {\n \"use strict\";\n function initialize(length) {\n Integer_1.default.assert(length, 'length');\n var array;\n if (length > 65536)\n array = new Array(length);\n else {\n array = [];\n array.length = length;\n }\n return array;\n }\n exports.initialize = initialize;\n function copy(source, sourceIndex, length) {\n if (sourceIndex === void 0) { sourceIndex = 0; }\n if (length === void 0) { length = Infinity; }\n if (!source)\n return source;\n return copyTo(source, initialize(Math.min(length, Math.max(source.length - sourceIndex, 0))), sourceIndex, 0, length);\n }\n exports.copy = copy;\n var CBN = 'Cannot be null.', CBL0 = 'Cannot be less than zero.';\n function copyTo(source, destination, sourceIndex, destinationIndex, length) {\n if (sourceIndex === void 0) { sourceIndex = 0; }\n if (destinationIndex === void 0) { destinationIndex = 0; }\n if (length === void 0) { length = Infinity; }\n if (!source)\n throw new ArgumentNullException_1.default('source', CBN);\n if (!destination)\n throw new ArgumentNullException_1.default('destination', CBN);\n if (sourceIndex < 0)\n throw new ArgumentOutOfRangeException_1.default('sourceIndex', sourceIndex, CBL0);\n var sourceLength = source.length;\n if (sourceIndex >= sourceLength)\n throw new ArgumentOutOfRangeException_1.default('sourceIndex', sourceIndex, 'Must be less than the length of the source array.');\n if (destination.length < 0)\n throw new ArgumentOutOfRangeException_1.default('destinationIndex', destinationIndex, CBL0);\n var maxLength = source.length - sourceIndex;\n if (isFinite(length) && length > maxLength)\n throw new ArgumentOutOfRangeException_1.default('sourceIndex', sourceIndex, 'Source index + length cannot exceed the length of the source array.');\n length = Math.min(length, maxLength);\n for (var i = 0; i < length; ++i) {\n destination[destinationIndex + i] = source[sourceIndex + i];\n }\n return destination;\n }\n exports.copyTo = copyTo;\n function contains(array, item, equalityComparer) {\n if (equalityComparer === void 0) { equalityComparer = Compare_1.areEqual; }\n if (array && array.length) {\n if (Array.isArray(array))\n return array.indexOf(item) != -1;\n for (var i = 0; i < array.length; ++i) {\n if (equalityComparer(array[i], item))\n return true;\n }\n }\n return false;\n }\n exports.contains = contains;\n function replace(array, old, newValue, max) {\n var count = 0;\n if (max !== 0) {\n if (!max)\n max = Infinity;\n else if (max < 0)\n throw new ArgumentOutOfRangeException_1.default('max', max, CBL0);\n for (var i = (array.length - 1); i >= 0; --i) {\n if (array[i] === old) {\n array[i] = newValue;\n ++count;\n if (!--max)\n break;\n }\n }\n }\n return count;\n }\n exports.replace = replace;\n function updateRange(array, value, index, length) {\n Integer_1.default.assert(index, 'index');\n Integer_1.default.assert(index, 'length');\n var end = index + length;\n for (var i = index; i < end; ++i) {\n array[i] = value;\n }\n }\n exports.updateRange = updateRange;\n function clear(array, index, length) {\n updateRange(array, null, index, length);\n }\n exports.clear = clear;\n function register(array, item, equalityComparer) {\n if (equalityComparer === void 0) { equalityComparer = Compare_1.areEqual; }\n if (!array)\n throw new ArgumentNullException_1.default('array', CBN);\n var len = array.length;\n var ok = !len || !contains(array, item, equalityComparer);\n if (ok)\n array[len] = item;\n return ok;\n }\n exports.register = register;\n function findIndex(array, predicate) {\n if (!array)\n throw new ArgumentNullException_1.default('array', CBN);\n if (!Types_1.default.isFunction(predicate))\n throw new ArgumentException_1.default('predicate', 'Must be a function.');\n var len = array.length;\n for (var i = 0; i < len; ++i) {\n if ((i) in (array) && predicate(array[i]))\n return i;\n }\n return -1;\n }\n exports.findIndex = findIndex;\n function forEach(source, fn) {\n if (!source)\n throw new ArgumentNullException_1.default('source', CBN);\n if (fn) {\n for (var i = 0; i < source.length; ++i) {\n if (fn(source[i]) === false)\n break;\n }\n }\n return source;\n }\n exports.forEach = forEach;\n function applyTo(target, fn) {\n if (!target)\n throw new ArgumentNullException_1.default('target', CBN);\n if (fn) {\n for (var i = 0; i < target.length; ++i) {\n target[i] = fn(target[i]);\n }\n }\n return target;\n }\n exports.applyTo = applyTo;\n function removeIndex(array, index) {\n if (!array)\n throw new ArgumentNullException_1.default('array', CBN);\n Integer_1.default.assert(index, 'index');\n if (index < 0)\n throw new ArgumentOutOfRangeException_1.default('index', index, CBL0);\n var exists = index < array.length;\n if (exists)\n array.splice(index, 1);\n return exists;\n }\n exports.removeIndex = removeIndex;\n function remove(array, value, max, equalityComparer) {\n if (equalityComparer === void 0) { equalityComparer = Compare_1.areEqual; }\n if (!array)\n throw new ArgumentNullException_1.default('array', CBN);\n var count = 0;\n if (array && array.length && max !== 0) {\n if (!max)\n max = Infinity;\n else if (max < 0)\n throw new ArgumentOutOfRangeException_1.default('max', max, CBL0);\n for (var i = (array.length - 1); i >= 0; --i) {\n if (equalityComparer(array[i], value)) {\n array.splice(i, 1);\n ++count;\n if (!--max)\n break;\n }\n }\n }\n return count;\n }\n exports.remove = remove;\n function repeat(element, count) {\n Integer_1.default.assert(count, 'count');\n if (count < 0)\n throw new ArgumentOutOfRangeException_1.default('count', count, CBL0);\n var result = [];\n while (count--) {\n result.push(element);\n }\n return result;\n }\n exports.repeat = repeat;\n function flatten(a, recurseDepth) {\n if (recurseDepth === void 0) { recurseDepth = 0; }\n var result = [];\n for (var i = 0; i < a.length; i++) {\n var x = a[i];\n if (Array.isArray(x)) {\n if (recurseDepth > 0)\n x = flatten(x, recurseDepth - 1);\n for (var n = 0; n < x.length; n++)\n result.push(x[n]);\n }\n else\n result.push(x);\n }\n return result;\n }\n exports.flatten = flatten;\n});\n","/*!\r\n * @author electricessence / https://github.com/electricessence/\r\n * Licensing: MIT https://github.com/electricessence/TypeScript.NET/blob/master/LICENSE.md\r\n */\r\n\r\n///<reference path=\"IArray.d.ts\"/>\r\n///<reference path=\"../../FunctionTypes.d.ts\"/>\r\nimport Type from \"../../Types\";\r\nimport Integer from \"../../Integer\";\r\nimport {areEqual} from \"../../Compare\";\r\nimport ArgumentException from \"../../Exceptions/ArgumentException\";\r\nimport ArgumentNullException from \"../../Exceptions/ArgumentNullException\";\r\nimport ArgumentOutOfRangeException from \"../../Exceptions/ArgumentOutOfRangeException\";\r\n\r\n/**\r\n * Initializes an array depending on the requested capacity.\r\n * The returned array will have a .length equal to the value provided.\r\n * @param length\r\n * @returns {T[]}\r\n */\r\nexport function initialize<T>(length:number):T[]\r\n{\r\n\tInteger.assert(length, 'length');\r\n\t// This logic is based upon JS performance tests that show a significant difference at the level of 65536.\r\n\tvar array:T[];\r\n\tif(length>65536)\r\n\t\tarray = new Array(length);\r\n\telse\r\n\t{\r\n\t\tarray = [];\r\n\t\tarray.length = length;\r\n\t}\r\n\treturn array;\r\n}\r\n\r\n/**\r\n *\r\n * @param source\r\n * @param sourceIndex\r\n * @param length\r\n * @returns {any}\r\n */\r\nexport function copy<T>(\r\n\tsource:IArray<T>,\r\n\tsourceIndex:number = 0,\r\n\tlength:number = Infinity):T[]\r\n{\r\n\tif(!source) return <any>source; // may have passed zero? undefined? or null?\r\n\treturn copyTo(\r\n\t\tsource,\r\n\t\tinitialize<T>(Math.min(length, Math.max(source.length - sourceIndex, 0))),\r\n\t\tsourceIndex, 0, length);\r\n}\r\n\r\nconst\r\n\tCBN = 'Cannot be null.',\r\n\tCBL0 = 'Cannot be less than zero.';\r\n\r\n/**\r\n * Copies one array to another.\r\n * @param source\r\n * @param destination\r\n * @param sourceIndex\r\n * @param destinationIndex\r\n * @param length An optional limit to stop copying.\r\n * @returns The destination array.\r\n */\r\nexport function copyTo<T,TDestination extends IArray<any>>(\r\n\tsource:IArray<T>,\r\n\tdestination:TDestination,\r\n\tsourceIndex:number = 0,\r\n\tdestinationIndex:number = 0,\r\n\tlength:number = Infinity):TDestination\r\n{\r\n\tif(!source)\r\n\t\tthrow new ArgumentNullException('source', CBN);\r\n\r\n\tif(!destination)\r\n\t\tthrow new ArgumentNullException('destination', CBN);\r\n\r\n\tif(sourceIndex<0)\r\n\t\tthrow new ArgumentOutOfRangeException('sourceIndex', sourceIndex, CBL0);\r\n\r\n\tvar sourceLength = source.length;\r\n\tif(sourceIndex>=sourceLength)\r\n\t\tthrow new ArgumentOutOfRangeException('sourceIndex', sourceIndex, 'Must be less than the length of the source array.');\r\n\r\n\tif(destination.length<0)\r\n\t\tthrow new ArgumentOutOfRangeException('destinationIndex', destinationIndex, CBL0);\r\n\r\n\tvar maxLength = source.length - sourceIndex;\r\n\tif(isFinite(length) && length>maxLength)\r\n\t\tthrow new ArgumentOutOfRangeException('sourceIndex', sourceIndex, 'Source index + length cannot exceed the length of the source array.');\r\n\r\n\tlength = Math.min(length, maxLength);\r\n\r\n\tfor(let i = 0; i<length; ++i)\r\n\t{\r\n\t\tdestination[destinationIndex + i] = source[sourceIndex + i];\r\n\t}\r\n\r\n\treturn destination;\r\n}\r\n\r\n/**\r\n * Checks to see if the provided array contains an item.\r\n * If the array value is null, then false is returned.\r\n * @param array\r\n * @param item\r\n * @param {function?} equalityComparer\r\n * @returns {boolean}\r\n */\r\nexport function contains<T>(\r\n\tarray:IArray<T>, item:T,\r\n\tequalityComparer:EqualityComparison<T> = areEqual):boolean\r\n{\r\n\tif(array && array.length)\r\n\t{\r\n\r\n\t\tif(Array.isArray(array)) return array.indexOf(item)!= -1;\r\n\r\n\t\tfor(let i = 0; i<array.length; ++i)\r\n\t\t{\r\n\t\t\t// 'areEqual' includes NaN==NaN evaluation.\r\n\t\t\tif(equalityComparer(array[i], item))\r\n\t\t\t\treturn true;\r\n\t\t}\r\n\t}\r\n\r\n\treturn false;\r\n}\r\n\r\n/**\r\n * Finds and replaces a value from an array. Will replaces all instances unless a maximum is specified.\r\n * @param array\r\n * @param old\r\n * @param newValue\r\n * @param max\r\n * @returns {number}\r\n */\r\nexport function replace<T>(\r\n\tarray:IArray<T>,\r\n\told:T,\r\n\tnewValue:T,\r\n\tmax?:number):number\r\n{\r\n\r\n\tvar count = 0;\r\n\tif(max!==0)\r\n\t{\r\n\t\tif(!max)\r\n\t\t\tmax = Infinity;\r\n\t\telse if(max<0)\r\n\t\t\tthrow new ArgumentOutOfRangeException('max', max, CBL0);\r\n\r\n\t\tfor(let i = (array.length - 1); i>=0; --i)\r\n\t\t{\r\n\t\t\tif(array[i]===old)\r\n\t\t\t{\r\n\t\t\t\tarray[i] = newValue;\r\n\t\t\t\t++count;\r\n\t\t\t\tif(!--max)\r\n\t\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\treturn count;\r\n\r\n}\r\n\r\n/**\r\n * Replaces values of an array across a range of indexes.\r\n * @param array\r\n * @param value\r\n * @param index\r\n * @param length\r\n */\r\nexport function updateRange<T>(\r\n\tarray:T[],\r\n\tvalue:T,\r\n\tindex:number,\r\n\tlength:number):void\r\n{\r\n\tInteger.assert(index, 'index');\r\n\tInteger.assert(index, 'length');\r\n\r\n\tvar end = index + length;\r\n\tfor(let i:number = index; i<end; ++i)\r\n\t{\r\n\t\tarray[i] = value;\r\n\t}\r\n}\r\n\r\n/**\r\n * Clears (sets to null) values of an array across a range of indexes.\r\n * @param array\r\n * @param index\r\n * @param length\r\n */\r\nexport function clear(\r\n\tarray:any[],\r\n\tindex:number,\r\n\tlength:number):void\r\n{\r\n\tupdateRange(array, null, index, length);\r\n}\r\n\r\n/**\r\n * Ensures a value exists within an array. If not found, adds to the end.\r\n * @param array\r\n * @param item\r\n * @param {function?} equalityComparer\r\n * @returns {boolean}\r\n */\r\nexport function register<T>(\r\n\tarray:IArray<T>, item:T,\r\n\tequalityComparer:EqualityComparison<T> = areEqual):boolean\r\n{\r\n\tif(!array)\r\n\t\tthrow new ArgumentNullException('array', CBN);\r\n\tvar len = array.length; // avoid querying .length more than once. *\r\n\tvar ok = !len || !contains(array, item, equalityComparer);\r\n\tif(ok) array[len] = item; // * push would query length again.\r\n\treturn ok;\r\n}\r\n\r\n/**\r\n * Returns the first index of which the provided predicate returns true.\r\n * Returns -1 if always false.\r\n * @param array\r\n * @param predicate\r\n * @returns {number}\r\n */\r\nexport function findIndex<T>(array:IArray<T>, predicate:Predicate<T>):number\r\n{\r\n\tif(!array)\r\n\t\tthrow new ArgumentNullException('array', CBN);\r\n\tif(!Type.isFunction(predicate))\r\n\t\tthrow new ArgumentException('predicate', 'Must be a function.');\r\n\tvar len = array.length;\r\n\tfor(let i = 0; i<len; ++i)\r\n\t{\r\n\t\tif((i)in(array) && predicate(array[i]))\r\n\t\t\treturn i;\r\n\t}\r\n\r\n\treturn -1;\r\n}\r\n\r\n\r\n/**\r\n * Allows for using \"false\" to cause forEach to break.\r\n * Can also be applied to a structure that indexes like an array, but may not be.\r\n * @param source\r\n * @param fn\r\n * @returns {IArray<T>}\r\n */\r\nexport function forEach<T>(\r\n\tsource:IArray<T>,\r\n\tfn:(value:T, index?:number) => (void|boolean)):IArray<T>\r\n{\r\n\tif(!source)\r\n\t\tthrow new ArgumentNullException('source', CBN);\r\n\r\n\tif(fn)\r\n\t{\r\n\t\tfor(let i = 0; i<source.length; ++i)\r\n\t\t{\r\n\t\t\tif(fn(source[i])===false)\r\n\t\t\t\tbreak;\r\n\t\t}\r\n\t}\r\n\treturn source;\r\n}\r\n\r\n\r\n/**\r\n * Is similar to Array.map() but instead of returning a new array, it updates the existing indexes.\r\n * Can also be applied to a structure that indexes like an array, but may not be.\r\n * @param target\r\n * @param fn\r\n * @returns {IArray<T>}\r\n */\r\nexport function applyTo<T>(target:IArray<T>, fn:(a:T) => T):IArray<T>\r\n{\r\n\tif(!target)\r\n\t\tthrow new ArgumentNullException('target', CBN);\r\n\r\n\tif(fn)\r\n\t{\r\n\t\tfor(let i = 0; i<target.length; ++i)\r\n\t\t{\r\n\t\t\ttarget[i] = fn(target[i]);\r\n\t\t}\r\n\t}\r\n\treturn target;\r\n}\r\n\r\n/**\r\n * Removes an entry at a specified index.\r\n * @param array\r\n * @param index\r\n * @returns {boolean} True if the value was able to be removed.\r\n */\r\nexport function removeIndex<T>(array:T[], index:number):boolean\r\n{\r\n\tif(!array)\r\n\t\tthrow new ArgumentNullException('array', CBN);\r\n\r\n\tInteger.assert(index, 'index');\r\n\tif(index<0) throw new ArgumentOutOfRangeException('index', index, CBL0);\r\n\r\n\r\n\tvar exists = index<array.length;\r\n\tif(exists)\r\n\t\tarray.splice(index, 1);\r\n\treturn exists;\r\n}\r\n\r\n/**\r\n * Finds and removes a value from an array. Will remove all instances unless a maximum is specified.\r\n * @param array\r\n * @param value\r\n * @param max\r\n * @param {function?} equalityComparer\r\n * @returns {number} The number of times the value was found and removed.\r\n */\r\nexport function remove<T>(\r\n\tarray:T[], value:T, max?:number,\r\n\tequalityComparer:EqualityComparison<T> = areEqual):number\r\n{\r\n\tif(!array)\r\n\t\tthrow new ArgumentNullException('array', CBN);\r\n\r\n\tvar count = 0;\r\n\tif(array && array.length && max!==0)\r\n\t{\r\n\t\tif(!max)\r\n\t\t\tmax = Infinity;\r\n\t\telse if(max<0)\r\n\t\t\tthrow new ArgumentOutOfRangeException('max', max, CBL0);\r\n\r\n\t\tfor(let i = (array.length - 1); i>=0; --i)\r\n\t\t{\r\n\t\t\tif(equalityComparer(array[i], value))\r\n\t\t\t{\r\n\t\t\t\tarray.splice(i, 1);\r\n\t\t\t\t++count;\r\n\t\t\t\tif(!--max)\r\n\t\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\treturn count;\r\n}\r\n\r\n/**\r\n * Simply repeats a value the number of times specified.\r\n * @param element\r\n * @param count\r\n * @returns {T[]}\r\n */\r\nexport function repeat<T>(element:T, count:number):T[]\r\n{\r\n\tInteger.assert(count, 'count');\r\n\tif(count<0) throw new ArgumentOutOfRangeException('count', count, CBL0);\r\n\r\n\tvar result:T[] = [];\r\n\twhile(count--)\r\n\t{\r\n\t\tresult.push(element);\r\n\t}\r\n\r\n\treturn result;\r\n}\r\n\r\n/**\r\n * Takes any arrays within an array and inserts the values contained within in place of that array.\r\n * For every count higher than 0 in recurseDepth it will attempt an additional pass. Passing Infinity will flatten all arrays contained.\r\n * @param a\r\n * @param recurseDepth\r\n * @returns {any[]}\r\n */\r\nexport function flatten(a:any[], recurseDepth:number = 0):any[]\r\n{\r\n\tvar result:any[] = [];\r\n\tfor(var i = 0; i<a.length; i++)\r\n\t{\r\n\t\tvar x = a[i];\r\n\t\tif(Array.isArray(x))\r\n\t\t{\r\n\t\t\tif(recurseDepth>0) x = flatten(x, recurseDepth - 1);\r\n\t\t\tfor(var n = 0; n<x.length; n++) result.push(x[n]);\r\n\t\t}\r\n\t\telse result.push(x);\r\n\t}\r\n\treturn result;\r\n}\r\n"]}