package com.rubenswieringa.utils { import com.rubenswieringa.utils.MathTool; /** * Provides additional functionality for Arrays. * * @author Ruben Swieringa * ruben.swieringa@gmail.com * www.rubenswieringa.com * www.rubenswieringa.com/blog * @version 1.0.0 * @see MathTool * * * edit 2 * * Before modifying and/or redistributing this class, please contact Ruben Swieringa (ruben.swieringa@gmail.com). * * * View code documentation at: * http://www.rubenswieringa.com/code/as3/flex/lib_flex_book_com_rubenswieringa_utils_ArrayTool/docs/ * */ public class lib_flex_book_com_rubenswieringa_utils_ArrayTool { /** * Constructor. * @private */ public function lib_flex_book_com_rubenswieringa_utils_ArrayTool ():void {} /** * Adjusts all values in an Array. * * @param array Array whose values to adjust * @param value Value with which to increase, decrease, multiply, or divide a value from array with * @param operator Indicates how to adjust a value from array (addition, minus, multiplication, or division) * * @see MathTool#PLUS * @see MathTool#MINUS * @see MathTool#MULTIPLICATION * @see MathTool#DIVISION * * @return Array with adjusted values */ public static function adjustValues (array:Array, value:*, operator:String="*"):Array { var newArray:Array = []; for (var i:String in array){ switch (operator){ case MathTool.PLUS : newArray[i] = array[i] + value; break; case MathTool.MINUS : newArray[i] = array[i] - value; break; case MathTool.MULTIPLICATION : newArray[i] = array[i] * value; break; case MathTool.DIVISION : newArray[i] = array[i] / value; break; } } return newArray; } /** * Similar to ArrayUtil.getItemIndex(), this method searches an Array for an Object with a given property that has a certain value. Can also search for nested Objects.

* * @example The following code returns 2:

* var array:Array = [ {foo: {bar: 'value1'}}, {foo: {bar: 'value2'}}, {foo: {bar: 'value3'}} ];
* var propChain:Array = ['foo', 'bar'];
* var value:String = 'value3';
* lib_flex_book_com_rubenswieringa_utils_ArrayTool.getValueMatchIndex (array, propChain, value); // outputs 2
* * @param array Array to search. * @param property Property or property-chain to try every item in the Array for. This parameter can either be a String (normal property), Array (property-chain), or numeric value (array index). * @param value Value to be found. * * @return Index of the item where the value was found on the end of the property chain. */ public static function getValueMatchIndex (array:Array, property:*, value:*):int { // if property param is neither an Array nor a String nor a numeric value, try to cast it to a String: if (!(property is Array) && !(property is String) && !(property is uint || property is int || property is Number)){ property = String(property); } // now make sure that we have a chain of properties (in the form of an Array) to loop through: var propertyChain:Array; if (property is Array){ propertyChain = property; }else{ propertyChain = [property]; } // loop through source Array: var path:*; for (var i:int=0; i