/** * com.jR.Utils * July 5th, 2006 * Copyright (c) Jānis Radiņš ( janis@mediaverk.lv ) * * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ class com.jR.Utils { private static var trimmableChars:Array = [" ", "\t", "\n", "\r", String.fromCharCode(0)]; /** * Method strip whitespaces (or any other characters defined) from the beginning and end of a string * @param input:String; The string that will be trimmed * @param charlist:Array; [optional] List of charters that should be removed from beginning and end of input. If no value is suplied private static var trimmableChars will be used. * @return Modified string * @usage Utils.trim(" string ") - returns "string" **/ public static function trim(input:String, charlist:Array):String { var __chars:Array; if (charlist !== undefined && charlist instanceof Array == true && charlist.length>0) { __chars = charlist; } else { __chars = trimmableChars; } var i:Number; var __doLoop:Boolean = true; while (__doLoop == true) { __doLoop = false; for (i=0; i<__chars.length; i++) { if (input.charAt(0) == __chars[i]) { input = input.substr(1); if (input.length>0) { __doLoop = true; } break; } } } __doLoop = true; while (__doLoop == true) { __doLoop = false; for (i=0; i<__chars.length; i++) { if (input.charAt(input.length-1) == __chars[i]) { input = input.substr(0, input.length-1); if (input.length>0) { __doLoop = true; } break; } } } return input; } /** * Method for multiplying string N times * @param input:String; The string to be multiply * @param times:Number; Number of times input string should be multiplied * @return Modified string * @usage Utils.multiplyString("#", 10) - returns "##########" **/ public static function multiplyString(input:String, times:Number):String { var __return:String = new String(); for (var i:Number = 0; i 1\n\t[1]:String => a\n}" **/ public static function printR(input:Object, doTrace:Boolean):String { listedObjects = new Array(); var __return = printLoop(input, 0, "top"); if (doTrace !== false) { trace(__return); } listedObjects = null; return __return; } private static var listedObjects:Array = null; private static var nl:String = (newline == undefined ? "\n" : newline); private static function printLoop(inputObject:Object, level:Number, parentLocation:String):String { for (var j:Number = 0; j "+printLoop(inputObject[i], level+1, parentLocation+"."+i)); continue; } subRows.push("\t"+(isArray == false ? i : "["+i+"]")+":"+riseFirstChar(typeof inputObject[i])+" => "+inputObject[i]); } if (isArray == true) { subRows.reverse(); } var returnValue:String; if(level > 0) { returnValue = " {"+nl; } else { if(objectType == "Movieclip") { objectType = inputObject+":"+objectType; } returnValue = objectType+" {"+nl; } var manyTabs:String = multiplyString("\t", level); while (subRows.length>0) { returnValue += manyTabs+subRows.shift()+nl; } returnValue += manyTabs+"}"; return returnValue; } /** * Modified mx.utils.delegate.create method in order to enable extra arguments for delegate function. I'm not the author of idea, thought I don't remember where I've seen this aproach. * @param targetScope:Objec; Object on which scope assigned function should be called * @param callFunc:Function; Function to call * @return Function * @usage Utils.delegate(this, someMethod, 1, 2, 3, 4) will do the same as this.someMethod(1, 2, 3, 4) **/ public static function delegate(targetScope:Object, callFunc:Function):Function { var args:Array = new Array(); if (arguments.length>2) { args = arguments.slice(2); } var f:Function = function () { var target:Object = arguments.callee.target; var func:Function = arguments.callee.func; return func.apply(target, arguments.concat(arguments.callee.args)); }; f.target = targetScope; f.func = callFunc; f.args = args; return f; } /** * Simple math function to round some number to certain amount of digits * @param input:Number; Number to be rounded * @param digits:Number; Count of signs after delimiter * @return Rounded number * @usage Utils.roundTo(10.11111, 3) - returns 10.111 **/ public static function roundTo(input:Number, digits:Number):Number { return Math.round(input*Math.pow(10, digits))/Math.pow(10, digits); } /** * Method for cloning Array or Object (hash array) * @param object:Object; Object to be cloned * @param cloneProperties:Boolean; A Boolean value whether include hash properties into return Array. If no value is provided value is supposed to be true * @return Object copy * @usage Utils.clone({a:1, b:2}) will return new object with exactly same contents **/ public static function clone(object:Object, cloneProperties:Boolean):Object { var new_object:Object = (object instanceof Array == true ? new Array() : new Object()); if (object instanceof Array == true) { new_object = object.concat(); } if (cloneProperties !== false) { var keys:Array = new Array(); for (var key in object) { if (new_object[key] !== undefined) { continue; } keys.push(key); } for (var i:Number = keys.length-1; i>=0; i--) { new_object[keys[i]] = object[keys[i]]; } } return new_object; } /** * Shorthand method for creating new instance of TextFormat class with provided properties * @param params:Object; Properties of new instance * @return TextFormat object with provided properties * @usage var myLovelyTextFormat:TextFormat = Utils.createTextFormat({font:"_sans", size:11, bold:true}); **/ public static function createTextFormat(params:Object):TextFormat { var _fmt:TextFormat = new TextFormat(); for (var i in params) { _fmt[i] = params[i]; } return _fmt; } /** * Method for padding string with another string to certain length * @param input:String; String that should be modified * @param pad_length:Number; Length of new string * @param pad_string:String; [optional] String that should be used for padding input String. If no value is provided value is supposed to be " " * @param pad_right:Boolean; [optional] Boolean value indicating whether pad_string should be added to the right side of input string or left. If no value is provided value is supposed to be false * @return Modified String * @usage var padStr:String = Utils.str_pad("10", 10, "0") - returns "0000000010" **/ public static function str_pad(input:String, pad_length:Number, pad_string:String, pad_right:Boolean):String { if (typeof pad_string != "string" || pad_string.length == 0) { pad_string = " "; } while (input.lengthpad_length) { if (pad_right == true) { input = input.substr(0, pad_length); } else { input = input.substr(input.length-pad_length, pad_length); } } return input; } /** * Method for generating array or random values within certain range * @param min_value:Number; Minimal value for generated values * @param max_value:Number; maximal value for generated values * @param count:Number; Count of return values. If uniqueValues isset to true this value must not be larger than max_value-min_value * @param uniqueValues:Boolean; Switch whether return only unique values * @return Array of values * @usage getRandomRange(10, 20, 5, true); **/ public static function getRandomRange(min_value:Number, max_value:Number, count:Number, uniqueValues:Boolean):Array { if(uniqueValues == undefined) { uniqueValues = false; } if (min_value > max_value || isNaN(min_value) == true || isNaN(max_value) == true || isNaN(count) == true || count <= 0 || (uniqueValues == true && max_value - min_value < count)) { return null; } var returnValue:Array = new Array(); var currentNumber:Number; var i:Number; var doLoop:Boolean; while (true) { currentNumber = Math.floor(Math.random()*(max_value-min_value))+min_value; if (uniqueValues == false) { returnValue.push(currentNumber); } else { doLoop = false; for (i=0; i