UNPKG

ilib

Version:

iLib is a cross-engine library of internationalization (i18n) classes written in pure JS

1 lines 2.12 kB
var ilib=require("./ilib.js");var JSUtils={};JSUtils.shallowCopy=function(e,r){var t=undefined;if(e&&r){for(t in e){if(t!==undefined&&typeof e[t]!=="undefined"){r[t]=e[t]}}}};JSUtils.deepCopy=function(e,r){var t;for(t in e){if(t){if(typeof e[t]==="object"){r[t]={};JSUtils.deepCopy(e[t],r[t])}else{r[t]=e[t]}}}return r};JSUtils.mapString=function(e,r){var t="";if(r&&e){for(var n=0;n<e.length;n++){var i=e.charAt(n);t+=r[i]||i}}else{t=e}return t};JSUtils.indexOf=function(e,r){if(!e||!r){return-1}if(typeof e.indexOf==="function"){return e.indexOf(r)}else{for(var t=0;t<e.length;t++){if(e[t]===r){return t}}return-1}};JSUtils.toHexString=function(e,r){var t,n="",i=r&&r<9?r:4;if(!e){return""}for(t=0;t<e.length;t++){var o=e.charCodeAt(t).toString(16);n+="00000000".substring(0,i-o.length)+o}return n.toUpperCase()};JSUtils.isDate=function(e){var r;if(typeof e==="object"){r=e;return Object.prototype.toString.call(r)==="[object Date]"}return false};JSUtils.merge=function(e,r,t,n,i){var o=undefined,f={};for(o in e){if(o&&typeof e[o]!=="undefined"){f[o]=e[o]}}for(o in r){if(o&&typeof r[o]!=="undefined"){if(ilib.isArray(e[o])&&ilib.isArray(r[o])){if(typeof t!=="boolean"||!t){f[o]=[].concat(e[o]);f[o]=f[o].concat(r[o])}else{f[o]=r[o]}}else if(typeof e[o]==="object"&&typeof r[o]==="object"){f[o]=JSUtils.merge(e[o],r[o],t)}else{if(n&&i&&f[o]==r[o]){console.log("Property "+o+" in "+n+" is being overridden by the same value in "+i)}f[o]=r[o]}}}return f};JSUtils.isEmpty=function(e){var r=undefined;if(!e){return true}for(r in e){if(r&&typeof e[r]!=="undefined"){return false}}return true};JSUtils.hashCode=function(e){var r=0;function t(e,r){e*=65543;e+=r;e%=2147483647;return e}function n(e){var r=0;for(var n=0;n<e.length;n++){r=t(r,e.charCodeAt(n))}return r}switch(typeof e){case"undefined":r=0;break;case"string":r=n(e);break;case"function":case"number":case"xml":r=n(String(e));break;case"boolean":r=e?1:0;break;case"object":var i=[];for(var o in e){if(e.hasOwnProperty(o)){i.push(o)}}i.sort();for(var f=0;f<i.length;f++){r=t(r,n(i[f]));r=t(r,JSUtils.hashCode(e[i[f]]))}break}return r};module.exports=JSUtils;