Class
caplin.core

ArrayUtility

Utility class that provides methods for array manipulation.

Constructor Summary

Attributes Name and Description
caplin.core.ArrayUtility()

This is a static class that never needs to be instantiated.

Method Summary

Attributes Name and Description
<static> boolean inArray(Array pArray, Variant vValueToFind)

Return true if the input array contains the input value, false otherwise.

<static> Array intersect(Varargs One)

Creates an intersect of the supplied arrays.

<static> boolean isEqual(Array pArray1, Array pArray2)

Compares all elements of two arrays for equality.

<static> Array removeItem(Array pArray, Variant vValueToRemove)

Removes the first example of the specified item in the specified array, or does nothing if the item is not found.

<static> Array subtract(Varargs One)

Subtracts one or more arrays from the first array argument.

<static> Array union(Varargs One)

Creates a union of the supplied arrays.

<static> Array unique(Array pArray)

Returns a new array containing each item in the input array once.

<static> Array uniquePrimitive(Array pArray)

Returns an array of the unique values contained within the input array.

Constructor Detail

caplin.core.ArrayUtility()

This is a static class that never needs to be instantiated.

Method Detail

<static> boolean inArray(Array pArray, Variant vValueToFind)

Return true if the input array contains the input value, false otherwise.

Parameters
Array pArray The Array to test for the presence of the input variant. May not be null or undefined.
Variant vValueToFind Variant whose presence in the input array is to be tested. Works with null, undefined and NaN.
Returns
true if the specified value was found in the array, false otherwise.

<static> Array intersect(Varargs One)

Creates an intersect of the supplied arrays. This is a new array with new indexes that contain all the values that occur in all of the supplied arrays with no duplicates.

Parameters
Varargs One or more array arguments.
Returns
The intersection of the arrays.

<static> boolean isEqual(Array pArray1, Array pArray2)

Compares all elements of two arrays for equality. If either array is null or undefined this function will return false.

Note: The comparison is performed with ===, so if nested arrays or maps are not identical, this function will return false even if they contain the exact same elements. However, this method will work with NaN, so arrays containing NaN can be equal to each other.

Parameters
Array pArray1 The first Array to compare.
Array pArray2 The second Array to compare.
Returns
true if the arrays are the same length and all elements are equal and of same type.

<static> Array removeItem(Array pArray, Variant vValueToRemove)

Removes the first example of the specified item in the specified array, or does nothing if the item is not found.

Since this method uses the built-in indexOf which does work with NaN, it will not work if called with NaN and will throw an ArgumentError. If you need to remove NaN, you'll have to loop over the array and then splice it out.

Parameters
Array pArray The Array from which to remove the specified item. May not be null or undefined.
Variant vValueToRemove The Variant to be removed from the specified array. May not be NaN.
Returns
{Array} The input array with the specified item removed, or the original array if the item was not found.

<static> Array subtract(Varargs One)

Subtracts one or more arrays from the first array argument. In other words, it creates a copy of the first array argument, and then removes all items that appear in any of the subsequent array arguments.

Parameters
Varargs One or more array arguments.
Returns
The complement of the first array argument, with respect to the subsequent arguments.

<static> Array union(Varargs One)

Creates a union of the supplied arrays. This is a new array with new indexes that contain all the values of the supplied arrays with no duplicates.

Parameters
Varargs One or more array arguments.
Returns
The union of the arrays.

<static> Array unique(Array pArray)

Returns a new array containing each item in the input array once.

WARNING: This function is slow for big lists. Consider using uniquePrimitive if your use-case allows.

Parameters
Array pArray The array to be examined for duplicate entries. May not be null or undefined.
Returns
{Array} A new array of unique values found in the input array. Will not be null or undefined.

<static> Array uniquePrimitive(Array pArray)

Returns an array of the unique values contained within the input array.

WARNING: does not work on arrays containing non key-able values (e.g. objects, arrays). use only with arrays containing strings and/or numbers.

Will throw a TypeError if it finds an item in the array of type object.

Parameters
Array pArray The array to be examined for duplicate entries. May not be null or undefined.
Returns
{Array} A new array of unique values found in the input array. Will not be null or undefined.