ilib
Version:
iLib is a cross-engine library of internationalization (i18n) classes written in pure JS
1 lines • 3.15 kB
JavaScript
var ilib=require("./ilib.js"),JSUtils={shallowCopy:function(source,target){var prop=void 0;if(source&&target)for(prop in source)void 0!==prop&&void 0!==source[prop]&&(target[prop]=source[prop])},deepCopy:function(from,to){var prop;for(prop in from)prop&&("object"==typeof from[prop]?(to[prop]={},JSUtils.deepCopy(from[prop],to[prop])):to[prop]=from[prop]);return to},mapString:function(str,map){var mapped="";if(map&&str)for(var i=0;i<str.length;i++){var c=str.charAt(i);mapped+=map[c]||c}else mapped=str;return mapped},indexOf:function(array,obj){if(!array||!obj)return-1;if("function"==typeof array.indexOf)return array.indexOf(obj);for(var i=0;i<array.length;i++)if(array[i]===obj)return i;return-1},pad:function(str,length,right){"string"!=typeof str&&(str=""+str);var start=0;return"-"===str.charAt(0)&&start++,str.length>=length+start?str:right?str+JSUtils.pad.zeros.substring(0,length-str.length+start):str.substring(0,start)+JSUtils.pad.zeros.substring(0,length-str.length+start)+str.substring(start)}};JSUtils.pad.zeros="00000000000000000000000000000000",JSUtils.toHexString=function(string,limit){var i,result="",lim=limit&&limit<9?limit:4;if(!string)return"";for(i=0;i<string.length;i++){var ch=string.charCodeAt(i).toString(16);result+=JSUtils.pad(ch,lim)}return result.toUpperCase()},JSUtils.isDate=function(object){return"object"==typeof object&&"[object Date]"===Object.prototype.toString.call(object)},JSUtils.merge=function(object1,object2,replace,name1,name2){var prop=void 0,newObj={};for(prop in object1)prop&&void 0!==object1[prop]&&(newObj[prop]=object1[prop]);for(prop in object2)prop&&void 0!==object2[prop]&&(ilib.isArray(object1[prop])&&ilib.isArray(object2[prop])?newObj[prop]="boolean"==typeof replace&&replace?object2[prop]:(newObj[prop]=[].concat(object1[prop]),newObj[prop].concat(object2[prop])):"object"==typeof object1[prop]&&"object"==typeof object2[prop]?newObj[prop]=JSUtils.merge(object1[prop],object2[prop],replace):(name1&&name2&&newObj[prop]==object2[prop]&&console.log("Property "+prop+" in "+name1+" is being overridden by the same value in "+name2),newObj[prop]=object2[prop]));return newObj},JSUtils.isEmpty=function(obj){var prop=void 0;if(!obj)return!0;for(prop in obj)if(prop&&void 0!==obj[prop])return!1;return!0},JSUtils.hashCode=function(obj){var hash=0;function addHash(hash,newValue){return hash*=65543,hash+=newValue,hash%=2147483647}function stringHash(str){for(var hash=0,i=0;i<str.length;i++)hash=addHash(hash,str.charCodeAt(i));return hash}switch(typeof obj){case"undefined":hash=0;break;case"string":hash=stringHash(obj);break;case"function":case"number":case"xml":hash=stringHash(String(obj));break;case"boolean":hash=obj?1:0;break;case"object":var props=[];for(var p in obj)obj.hasOwnProperty(p)&&props.push(p);props.sort();for(var i=0;i<props.length;i++)hash=addHash(hash=addHash(hash,stringHash(props[i])),JSUtils.hashCode(obj[props[i]]))}return hash},JSUtils.callAll=function(arr,action,callback,results){results=results||[],arr&&arr.length?action(arr,function(result){results.push(result),JSUtils.callAll(arr.slice(1),action,callback,results)}):callback(results)},module.exports=JSUtils;