#if COPYRIGHT //------------------------------------------------------------------------------ // // Copyright (c) Microsoft Corporation. All rights reserved. // //------------------------------------------------------------------------------ #endif Array.__typeName = 'Array'; Array.__class = true; Array.add = Array.enqueue = function(array, item) { /// Adds an element at the end of the array. /// The array to add to. /// The object to add. // Setting Array[Array.length] is faster than Array.push() for a single element. array[array.length] = item; } Array.addRange = function(array, items) { /// Adds a range of items at the end of the array. /// The array to add to. /// The array of items to append. // Array.push() for multiple elements is faster than setting Array[Array.length] in a loop. array.push.apply(array, items); } Array.clear = function(array) { /// Clears the array of its elements. /// The array to clear. array.length = 0; } Array.clone = function(array) { /// Makes a clone of the array. /// The array to clone. /// A clone of the array. if (array.length === 1) { return [array[0]]; } else { // When the Array ctor is called with 0 or 2 or more arguments, it creates a new // Array with the elements from the argument list. return Array.apply(null, array); } } Array.contains = function(array, item) { /// Use this method to determine if an array contains the specified element. /// The array to look into. /// The object to find in the array. /// True if the object was found. return (Sys._indexOf(array, item) >= 0); } Array.dequeue = function(array) { /// Removes and returns the object at the beginning of the array. /// The object that is removed from the beginning of the array. return array.shift(); } Array.forEach = function(array, method, instance) { /// Calls the specified function on each element of the array. /// The array to enumerate. /// The method to call. /// The method should take the array element, the index of the element and /// the array itself as its parameters. /// /// The context under which the function must run (i.e. what 'this' means inside the function). for (var i = 0, l = array.length; i < l; i++) { var elt = array[i]; if (typeof(elt) !== 'undefined') method.call(instance, elt, i, array); } } Array.indexOf = function(array, item, start) { /// Finds the index in the array of the provided item. /// The array to look into. /// The object to find. /// The index where the search begins. /// The index of the item or -1 if it wasn't found. return Sys._indexOf(array, item, start); } Array.insert = function(array, index, item) { /// Inserts an item at the specified index. /// The array to insert into. /// The index where the item will be inserted. /// The item to insert. array.splice(index, 0, item); } Array.parse = function(value) { /// Creates an array from a string representation of the form "[elt1, elt2, elt3]". /// The string representation of the array. /// An array built from the string representation. if (!value) return []; #if DEBUG var v = eval(value); if (!Array.isInstanceOfType(v)) throw Error.argument('value', Sys.Res.arrayParseBadFormat); return v; #else return eval(value); #endif } Array.remove = function(array, item) { /// Removes the first occurence of an item from the array. /// The array to remove from. /// The item to remove. /// True if the item was found. var index = Sys._indexOf(array, item); if (index >= 0) { array.splice(index, 1); } return (index >= 0); } Array.removeAt = function(array, index) { /// Removes the item at the specified index from the array. /// The array to remove from. /// The index of the item to remove. array.splice(index, 1); } // a private _indexOf allows for other public Array members to perform indexOf() without // causing a double validation of its public parameters, a huge source of slowness in debug mode // for code that uses any of the Array members that call indexOf (Array.remove and Array.contains). // It is Sys._indexOf instead of Array._indexOf to avoid adding another member to a built-in type. Sys._indexOf = function(array, item, start) { if (typeof(item) === "undefined") return -1; var length = array.length; if (length !== 0) { // Coerce into number ("1a" will become NaN, which is consistent with the built-in behavior of similar Array methods) start = start - 0; // NaN becomes zero if (isNaN(start)) { start = 0; } else { // If start is positive or negative infinity, don't try to truncate it. // The infinite values will be handled correctly by the subsequent code. if (isFinite(start)) { // This is faster than doing Math.floor or Math.ceil start = start - (start % 1); } // Negative start indices start from the end if (start < 0) { start = Math.max(0, length + start); } } // A do/while loop seems to have equal performance to a for loop in this scenario for (var i = start; i < length; i++) { if ((typeof(array[i]) !== "undefined") && (array[i] === item)) { return i; } } } return -1; }