UNPKG

like-json

Version:

Stringify at perfect performance. +1200%

10 lines (9 loc) 1.71 kB
/* like-json (https://npmjs.com/package/like-json) Copyright 2019 Lucas Barrena Licensed under MIT (https://github.com/LuKks/like-json) */ (function(){var f={_json_cache:{},json:function(a,c){function l(d,a){if("string"===typeof d||d instanceof String)return"\"' + "+a+(c.encode?'.replace(/(\\\\|")/g, "\\\\$1")':"")+" + '\"";if("number"===typeof d||d instanceof Number)return"' + "+(c.finite?"(isFinite("+a+") ? "+a+" : null)":a)+" + '";if("boolean"===typeof d||d instanceof Boolean)return"' + "+a+" + '"}function f(a,c){var d=Array.isArray(a),e=d?"[":"{",b;for(b in a){d&&(b=parseInt(b,10));var g=isFinite(b),k=g?"["+b+"]":"."+b,h=d?"":'"'+ b+'":';null===a[b]?e+=h+"null,":-1!==["function","symbol","undefined"].indexOf(typeof a[b])?d&&g&&(e+=h+"null,"):a[b]&&"function"===typeof a[b].toJSON?e+=h+"\"' + o"+c+k+".toJSON() + '\",":"object"===typeof a[b]&&-1===[String,Number,Boolean].indexOf(a[b].constructor)?e+=h+f(a[b],c+k)+",":(g=l(a[b],"o"+c+k),void 0!==g&&(e+=h+g+","))}","===e[e.length-1]&&(e=e.slice(0,-1));return e+(d?"]":"}")}c=c||{};return null===a?Function("o","return 'null';"):-1!==["function","symbol","undefined"].indexOf(typeof a)? Function("o","return undefined;"):a&&"function"===typeof a.toJSON?Function("o","return '\"' + o.toJSON() + '\"';"):"object"===typeof a&&-1===[String,Number,Boolean].indexOf(a.constructor)?Function("o","return '"+f(a,"")+"';"):Function("o","return '"+l(a,"o")+"';")},stringify:function(a,c,f){this._json_cache[c]||(this._json_cache[c]=this.json(a,f));return this._json_cache[c](a)}};"undefined"!==typeof module&&"undefined"!==typeof module.exports?module.exports=f:"undefined"!==typeof window?window.like= f:console.log("Unable to export.")})();