@dbl0null/slow-json-stringify
Version:
The slow.. well actually fastest JSON stringifier in the galaxy.
3 lines (2 loc) • 2.48 kB
JavaScript
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e||self).slowJsonStringify={})}(this,function(exports){var _replaceString=function(e){return-1!==e.indexOf("string")?'"__par__"':"__par__"},_matchStartRe=/^(",|,|")/,_chunkRegex=/"\w+__sjs"/g,_makeChunks=function(e,r){for(var n,t=e.replace(_chunkRegex,_replaceString).split("__par__"),a=[],i=t.length,u=0;u<i;++u){var _,f=t[u],o='("'+(null==(_=r[u])?void 0:_.name)+'":("?))$',s=(n=u+1)===i||(n=t[n].indexOf("}"))&&(0===n||1===n),p=new RegExp(s?"(,?)"+o:o),c=f.replace(_matchStartRe,"");a.push({flag:!1,pure:f,prevUndef:c,isUndef:f.replace(p,""),bothUndef:c.replace(p,"")})}return a},_this=void 0,__find=function __find(path){for(var str="obj",i=0;i<path.length;++i)str+="?.['"+path[i]+"']";return eval("(obj => "+str+")")};function _arraySerializer(e,r){for(var n="",t=r.length-1,a=0;a<t;++a)n+=e(r[a])+",";return"["+(n+=e(r[t]))+"]"}var _makeArraySerializer=function(e){return"function"==typeof e?_arraySerializer.bind(_this,e):JSON.stringify},fnUser=function(e){return e},attr=function(e,r){var n=r||fnUser;return{isSJS:!0,type:e,serializer:"array"===e?_makeArraySerializer(r):n}},_defaultRegex=/[\t\n\r"\\]/g,_escapeCallback=function(e){return"\\"+e},escape=function(e){return void 0===e&&(e=_defaultRegex),function(r){return r.replace(e,_escapeCallback)}};function _prepareQueue(e,r,n,t){if(void 0===t&&(t=[]),-1===n.toString().indexOf("__sjs"))for(var a=Object.keys(n),i=0;i<a.length;++i){var u=a[i];_prepareQueue(e,r,n[u],[].concat(t,[u]))}else{var _=__find(t),f=_(e);r.push({serializer:f.serializer,find:_,name:t[t.length-1]})}}var _makeQueue=function(e,r){var n=[];return _prepareQueue(r,n,e),n},_stringifyCallback=function(e,r){return r.isSJS?r.type+"__sjs":r},_prepare=function(e){var r=JSON.stringify(e,_stringifyCallback);return{_preparedString:r,_preparedSchema:JSON.parse(r)}},_select=function(e){return function(r,n){var t=e[n];return void 0!==r?t.flag?t.prevUndef+r:t.pure+r:(e[n+1].flag=!0,t.flag?t.bothUndef:t.isUndef)}},sjs=function(e){var r=_prepare(e),n=r._preparedString,t=_makeQueue(r._preparedSchema,e),a=_makeChunks(n,t),i=_select(a);return function(e){for(var r="",n=0;n<t.length;++n){var u=t[n],_=u.serializer,f=(0,u.find)(e);r+=i(_(f),n)}var o=a[a.length-1];return r+(o.flag?o.prevUndef:o.pure)}};exports.attr=attr,exports.escape=escape,exports.sjs=sjs});
//# sourceMappingURL=slow-json-stringify.umd.js.map