UNPKG

json-bignumber

Version:

JSONBigNumber.parse/stringify handling **all** JSON numbers using BigNumber. Based on Douglas Crockford [JSON.js](https://github.com/douglascrockford/JSON-js) package and [bignumber.js](https://github.com/MikeMcl/bignumber.js) library.

1 lines 4.78 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("bignumber.js")):"function"==typeof define&&define.amd?define(["bignumber.js"],e):"object"==typeof exports?exports.JSONBigNumber=e(require("bignumber.js")):t.JSONBigNumber=e(t.BigNumber)}(this,(function(t){return(()=>{"use strict";var e={332:e=>{e.exports=t}},r={};function n(t){var o=r[t];if(void 0!==o)return o.exports;var u=r[t]={exports:{}};return e[t](u,u.exports,n),u.exports}n.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return n.d(e,{a:e}),e},n.d=(t,e)=>{for(var r in e)n.o(e,r)&&!n.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:e[r]})},n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),n.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var o={};return(()=>{n.r(o),n.d(o,{stringify:()=>g,parse:()=>N,default:()=>w});var t,e,r,u,i,f,a=n(332),s=n.n(a),c=/[\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;function p(t){return t<10?"0"+t:t}function l(){return this.valueOf()}function y(t){return c.lastIndex=0,c.test(t)?'"'+t.replace(c,(function(t){var e=r[t];return"string"==typeof e?e:"\\u"+("0000"+t.charCodeAt(0).toString(16)).slice(-4)}))+'"':'"'+t+'"'}function b(r,n){var o,i,f,a,c,p=t,l=n[r],g=null!=l&&(l instanceof s()||s().isBigNumber(l));switch(g&&!l.isFinite()&&(l=null),l&&"object"==typeof l&&"function"==typeof l.toJSON&&(l=l.toJSON(r)),"function"==typeof u&&(l=u.call(n,r,l)),typeof l){case"string":return g?l:y(l);case"number":return isFinite(l)?String(l):"null";case"boolean":case"null":return String(l);case"object":if(!l)return"null";if(t+=e,c=[],"[object Array]"===Object.prototype.toString.apply(l)){for(a=l.length,o=0;o<a;o+=1)c[o]=b(o,l)||"null";return f=0===c.length?"[]":t?"[\n"+t+c.join(",\n"+t)+"\n"+p+"]":"["+c.join(",")+"]",t=p,f}if(u&&"object"==typeof u)for(a=u.length,o=0;o<a;o+=1)"string"==typeof u[o]&&(f=b(i=u[o],l))&&c.push(y(i)+(t?": ":":")+f);else for(i in l)Object.prototype.hasOwnProperty.call(l,i)&&(f=b(i,l))&&c.push(y(i)+(t?": ":":")+f);return f=0===c.length?"{}":t?"{\n"+t+c.join(",\n"+t)+"\n"+p+"}":"{"+c.join(",")+"}",t=p,f}}function g(r,n,o){var i;if(t="",e="","number"==typeof o)for(i=0;i<o;i+=1)e+=" ";else"string"==typeof o&&(e=o);if(u=n,n&&"function"!=typeof n&&("object"!=typeof n||"number"!=typeof n.length))throw new Error("JSON.stringify");return b("",{"":r})}"function"!=typeof Date.prototype.toJSON&&(Date.prototype.toJSON=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+p(this.getUTCMonth()+1)+"-"+p(this.getUTCDate())+"T"+p(this.getUTCHours())+":"+p(this.getUTCMinutes())+":"+p(this.getUTCSeconds())+"Z":null},Boolean.prototype.toJSON=l,Number.prototype.toJSON=l,String.prototype.toJSON=l),r={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"};var d,h,j={'"':'"',"\\":"\\","/":"/",b:"\b",f:"\f",n:"\n",r:"\r",t:"\t"},O=function(t){throw{name:"SyntaxError",message:t,at:i,text:d}},S=function(t){return t&&t!==f&&O("Expected '"+t+"' instead of '"+f+"'"),f=d.charAt(i),i+=1,f},v=function(){var t,e="";for("-"===f&&(e="-",S("-"));f>="0"&&f<="9";)e+=f,S();if("."===f)for(e+=".";S()&&f>="0"&&f<="9";)e+=f;if("e"===f||"E"===f)for(e+=f,S(),"-"!==f&&"+"!==f||(e+=f,S());f>="0"&&f<="9";)e+=f,S();if((t=new(s())(e)).isFinite())return t;O("Bad number")},m=function(){var t,e,r,n="";if('"'===f)for(;S();){if('"'===f)return S(),n;if("\\"===f)if(S(),"u"===f){for(r=0,e=0;e<4&&(t=parseInt(S(),16),isFinite(t));e+=1)r=16*r+t;n+=String.fromCharCode(r)}else{if("string"!=typeof j[f])break;n+=j[f]}else n+=f}O("Bad string")},x=function(){for(;f&&f<=" ";)S()};function N(t,e){var r;return d=t,i=0,f=" ",r=h(),x(),f&&O("Syntax error"),"function"==typeof e?function t(r,n){var o,u,i=r[n];if(i&&"object"==typeof i)for(o in i)Object.prototype.hasOwnProperty.call(i,o)&&(void 0!==(u=t(i,o))?i[o]=u:delete i[o]);return e.call(r,n,i)}({"":r},""):r}h=function(){switch(x(),f){case"{":return function(){var t,e={};if("{"===f){if(S("{"),x(),"}"===f)return S("}"),e;for(;f;){if(t=m(),x(),S(":"),Object.hasOwnProperty.call(e,t)&&O("Duplicate key '"+t+"'"),e[t]=h(),x(),"}"===f)return S("}"),e;S(","),x()}}O("Bad object")}();case"[":return function(){var t=[];if("["===f){if(S("["),x(),"]"===f)return S("]"),t;for(;f;){if(t.push(h()),x(),"]"===f)return S("]"),t;S(","),x()}}O("Bad array")}();case'"':return m();case"-":return v();default:return f>="0"&&f<="9"?v():function(){switch(f){case"t":return S("t"),S("r"),S("u"),S("e"),!0;case"f":return S("f"),S("a"),S("l"),S("s"),S("e"),!1;case"n":return S("n"),S("u"),S("l"),S("l"),null}O("Unexpected '"+f+"'")}()}};const w={parse:N,stringify:g}})(),o})()}));