/** * Created by cyril on 12/05/2014. */ class Utils { public static toRad(val:number) { return val * Math.PI / 180; } static php_js:any; public static contains(a:string[], obj:string) { for (var i = 0; i < a.length; i++) { if (a[i] === obj) { return true; } } return false; } public static isPrimitive(value:any) { var primitives = [ "array", "boolean", "integer", "number", "null", "object", "string", "void", "Date" ]; return Utils.contains(primitives, value); } public static UcFirst(string) { return string.charAt(0).toUpperCase() + string.slice(1); } public static shuffle(o) { //v1.0 for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); return o; } public static usort(inputArr, sorter) { // discuss at: http://phpjs.org/functions/usort/ // original by: Brett Zamir (http://brett-zamir.me) // improved by: Brett Zamir (http://brett-zamir.me) // note: Utils function deviates from PHP in returning a copy of the array instead // note: of acting by reference and returning true; Utils was necessary because // note: IE does not allow deleting and re-adding of properties without caching // note: of property position; you can set the ini of "phpjs.strictForIn" to true to // note: get the PHP behavior, but use Utils only if you are in an environment // note: such as Firefox extensions where for-in iteration order is fixed and true // note: property deletion is supported. Note that we intend to implement the PHP // note: behavior by default if IE ever does allow it; only gives shallow copy since // note: is by reference in PHP anyways // example 1: stuff = {d: '3', a: '1', b: '11', c: '4'}; // example 1: stuff = usort(stuff, function (a, b) {return(a-b);}); // example 1: $result = stuff; // returns 1: {0: '1', 1: '3', 2: '4', 3: '11'}; var valArr = [], k = '', i = 0, strictForIn = false, populateArr = {}; if (typeof sorter === 'string') { sorter = Utils[sorter]; } else if (Object.prototype.toString.call(sorter) === '[object Array]') { sorter = Utils[sorter[0]][sorter[1]]; } // BEGIN REDUNDANT Utils.php_js = Utils.php_js || {}; Utils.php_js.ini = Utils.php_js.ini || {}; // END REDUNDANT strictForIn = Utils.php_js.ini['phpjs.strictForIn'] && Utils.php_js.ini['phpjs.strictForIn'].local_value && Utils.php_js .ini['phpjs.strictForIn'].local_value !== 'off'; populateArr = strictForIn ? inputArr : populateArr; for (k in inputArr) { // Get key and value arrays if (inputArr.hasOwnProperty(k)) { valArr.push(inputArr[k]); if (strictForIn) { delete inputArr[k]; } } } try { valArr.sort(sorter); } catch (e) { return false; } for (i = 0; i < valArr.length; i++) { // Repopulate the old array populateArr[i] = valArr[i]; } return strictForIn || populateArr; } public static isArray(o) { return Object.prototype.toString.call(o) == "[object Array]"; } public static mergeObjects(target, source) { var item, tItem, o, idx; // If either argument is undefined, return the other. // If both are undefined, return undefined. if (typeof source == 'undefined') { return source; } else if (typeof target == 'undefined') { return target; } // Assume both are objects and don't care about inherited properties for (var prop in source) { item = source[prop]; if (typeof item == 'object' && item !== null) { if (Utils.isArray(item) && item.length) { // deal with arrays, will be either array of primitives or array of objects // If primitives if (typeof item[0] != 'object') { // if target doesn't have a similar property, just reference it tItem = target[prop]; if (!tItem) { target[prop] = item; // Otherwise, copy only those members that don't exist on target } else { // Create an index of items on target o = {}; for (var i = 0, iLen = tItem.length; i < iLen; i++) { o[tItem[i]] = true } // Do check, push missing for (var j = 0, jLen = item.length; j < jLen; j++) { if (!(item[j] in o)) { tItem.push(item[j]); } } } } else { // Deal with array of objects // Create index of objects in target object using ID property // Assume if target has same named property then it will be similar array idx = {}; tItem = target[prop] for (var k = 0, kLen = tItem.length; k < kLen; k++) { idx[tItem[k].id] = tItem[k]; } // Do updates for (var l = 0, ll = item.length; l < ll; l++) { // If target doesn't have an equivalent, just add it if (!(item[l].id in idx)) { tItem.push(item[l]); } else { Utils.mergeObjects(idx[item[l].id], item[l]); } } } } else { // deal with object Utils.mergeObjects(target[prop], item); } } else { // item is a primitive, just copy it over target[prop] = item; } } return target; } } export = Utils;