@qubit-ltd/json 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +219 -0
  3. package/README.zh_CN.md +194 -0
  4. package/dist/json.cjs +623 -0
  5. package/dist/json.cjs.map +1 -0
  6. package/dist/json.min.cjs +1 -0
  7. package/dist/json.min.cjs.map +1 -0
  8. package/dist/json.min.mjs +1 -0
  9. package/dist/json.min.mjs.map +1 -0
  10. package/dist/json.mjs +610 -0
  11. package/dist/json.mjs.map +1 -0
  12. package/doc/api/LosslessNumber.html +610 -0
  13. package/doc/api/fonts/OpenSans-Bold-webfont.eot +0 -0
  14. package/doc/api/fonts/OpenSans-Bold-webfont.svg +1838 -0
  15. package/doc/api/fonts/OpenSans-Bold-webfont.woff +0 -0
  16. package/doc/api/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
  17. package/doc/api/fonts/OpenSans-BoldItalic-webfont.svg +1838 -0
  18. package/doc/api/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
  19. package/doc/api/fonts/OpenSans-Italic-webfont.eot +0 -0
  20. package/doc/api/fonts/OpenSans-Italic-webfont.svg +1838 -0
  21. package/doc/api/fonts/OpenSans-Italic-webfont.woff +0 -0
  22. package/doc/api/fonts/OpenSans-Light-webfont.eot +0 -0
  23. package/doc/api/fonts/OpenSans-Light-webfont.svg +1839 -0
  24. package/doc/api/fonts/OpenSans-Light-webfont.woff +0 -0
  25. package/doc/api/fonts/OpenSans-LightItalic-webfont.eot +0 -0
  26. package/doc/api/fonts/OpenSans-LightItalic-webfont.svg +1843 -0
  27. package/doc/api/fonts/OpenSans-LightItalic-webfont.woff +0 -0
  28. package/doc/api/fonts/OpenSans-Regular-webfont.eot +0 -0
  29. package/doc/api/fonts/OpenSans-Regular-webfont.svg +1839 -0
  30. package/doc/api/fonts/OpenSans-Regular-webfont.woff +0 -0
  31. package/doc/api/fonts/OpenSans-Semibold-webfont.eot +0 -0
  32. package/doc/api/fonts/OpenSans-Semibold-webfont.svg +1838 -0
  33. package/doc/api/fonts/OpenSans-Semibold-webfont.ttf +0 -0
  34. package/doc/api/fonts/OpenSans-Semibold-webfont.woff +0 -0
  35. package/doc/api/fonts/OpenSans-SemiboldItalic-webfont.eot +0 -0
  36. package/doc/api/fonts/OpenSans-SemiboldItalic-webfont.svg +1838 -0
  37. package/doc/api/fonts/OpenSans-SemiboldItalic-webfont.ttf +0 -0
  38. package/doc/api/fonts/OpenSans-SemiboldItalic-webfont.woff +0 -0
  39. package/doc/api/global.html +1274 -0
  40. package/doc/api/index.html +236 -0
  41. package/doc/api/scripts/linenumber.js +34 -0
  42. package/doc/api/scripts/prettify/Apache-License-2.0.txt +202 -0
  43. package/doc/api/scripts/prettify/lang-css.js +2 -0
  44. package/doc/api/scripts/prettify/prettify.js +28 -0
  45. package/doc/api/styles/jsdoc-default.css +699 -0
  46. package/doc/api/styles/prettify-jsdoc.css +120 -0
  47. package/doc/api/styles/prettify-tomorrow.css +141 -0
  48. package/doc/json.min.visualization.html +4949 -0
  49. package/doc/json.visualization.html +4949 -0
  50. package/package.json +87 -0
package/dist/json.cjs ADDED
@@ -0,0 +1,623 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
6
+ var _createClass = require('@babel/runtime/helpers/createClass');
7
+ var _defineProperty = require('@babel/runtime/helpers/defineProperty');
8
+
9
+ var T$1=Object.defineProperty;var C$1=(t,a)=>T$1(t,"name",{value:a,configurable:!0});/**
10
+ * https://github.com/jawj/json-custom-numbers
11
+ * @copyright Copyright (c) 2023 George MacKerron
12
+ * @license MIT
13
+ *
14
+ * This file implements a non-recursive JSON parser that's intended to
15
+ * precisely match native `JSON.parse` behaviour but also allow for custom
16
+ * number parsing.
17
+ */const v=/[^"\\\u0000-\u001f]*/y,D$1=/-?(0|[1-9][0-9]*)([.][0-9]+)?([eE][-+]?[0-9]+)?|true|false|null/y,
18
+ J=/[ \n\t\r]*$/y,X=/^.{0,32}\n[ \t]/,h=/[ \n\t\r]*/y,j=`........................\
19
+ .........."............./.............................................\\......\b..\
20
+ ..\f........
21
+ ....\r.. `.split("."),x$1=C$1(()=>new Uint32Array(103),"hlArr"),k=x$1(),m=x$1(),w=x$1(),$=x$1();
22
+ let i=0;for(;i<48;i++)k[i]=m[i]=w[i]=$[i]=65536;for(;i<58;i++)k[i]=(m[i]=(w[i]=($[i]=
23
+ i-48)<<4)<<4)<<4;for(;i<65;i++)k[i]=m[i]=w[i]=$[i]=65536;for(;i<71;i++)k[i]=(m[i]=
24
+ (w[i]=($[i]=i-55)<<4)<<4)<<4;for(;i<97;i++)k[i]=m[i]=w[i]=$[i]=65536;for(;i<103;i++)
25
+ k[i]=(m[i]=(w[i]=($[i]=i-87)<<4)<<4)<<4;function O(t,a=""){if(!(t>=0))return "end\
26
+ of JSON input";if(t>31&&t<127)return `'${a}${String.fromCharCode(t)}'`;if(t===10)
27
+ return "\\n";if(t===9)return "\\t";const p=t.toString(16),y="0000".slice(p.length)+
28
+ p;return (t>31?`'${a}${String.fromCharCode(t)}' or `:"")+`\\u${y}`}C$1(O,"chDesc");
29
+ function R(t,a){const p=Object.keys(a),y=p.length;for(let d=0;d<y;d++){const n=p[d],
30
+ e=t.call(a,n,a[n]);e!==void 0?a[n]=e:delete a[n];}}C$1(R,"revive");function q$1(t,a,p){
31
+ const y=p===!0?" in array":p===!1?" in object":"",d=t.slice(0,a),n=d.match(/[^\n]{0,69}$/)[0],
32
+ e=n.length<d.length?"..."+n:n,o=a-(d.length-e.length),u=t.slice(a),r=u.match(/[^\n]{0,5}/)[0],
33
+ c=r.length<u.length?r+"...":r,A=e+c,I=" ".repeat(o<1?0:o-1)+"^";return `${y}
34
+ At position ${a} in JSON:
35
+ ${A}
36
+ ${I}`}C$1(q$1,"errContext");const N={maxDepth:1/0};function K$1(t,a,p,y){var U;typeof t!=
37
+ "string"&&(t=String(t)),typeof a!="function"&&(a=void 0);const d=y===void 0?N.maxDepth:
38
+ typeof y=="number"?y:(U=y.maxDepth)!=null?U:N.maxDepth;let n=0,e,o,u,r,c;function A(l){
39
+ throw new SyntaxError(l+q$1(t,n,u))}C$1(A,"err");function I(){A(`JSON structure too \
40
+ deeply nested (current max depth: ${d})`);}C$1(I,"tooDeep");function b(l){A(`Unexpe\
41
+ cted ${O(e)}, expecting ${l}`);}C$1(b,"expected");function E(){const l=n-1;if(D$1.lastIndex=
42
+ l,D$1.test(t)!==!0&&b("JSON value"),n=D$1.lastIndex,e<102){const g=t.slice(l,n);return p?
43
+ p.call(o,r,g):+g}return e===110?null:e===116}C$1(E,"word");function S(){let l="";e:
44
+ for(;;){v.lastIndex=n,v.test(t);const f=v.lastIndex;for(f>n&&(l+=t.slice(n,f),n=
45
+ f),e=t.charCodeAt(n++);;){switch(e){case 34:return l;case 92:if(e=t.charCodeAt(n++),
46
+ e===117){const s=k[t.charCodeAt(n++)]+m[t.charCodeAt(n++)]+w[t.charCodeAt(n++)]+
47
+ $[t.charCodeAt(n++)];if(s<65536){l+=String.fromCharCode(s);break}A("Invalid \\uXX\
48
+ XX escape in string");}const g=j[e];if(g!==""&&g!==void 0){l+=g;break}A(`Invalid \
49
+ escape sequence: ${O(e,"\\")} in string`);default:e>=0||A("Unterminated string"),
50
+ A(`Invalid unescaped ${O(e)} in string`);}if(e=t.charCodeAt(n),e!==92&&e!==34)continue e;
51
+ n++;}}}C$1(S,"string");e:{do e=t.charCodeAt(n++);while(e<=32&&(e===32||e===10||e===
52
+ 13||e===9));switch(e){case 123:d===0&&I(),o={},r=void 0,u=!1;break;case 91:d===0&&
53
+ I(),o=[],r=0,u=!0;break;case 34:c=S();break e;default:c=E();break e}const l=d+d-
54
+ 2,f=[],g=X.test(t);let s=0;t:for(;;)if(u===!0)for(;;){g===!0&&s>2&&(h.lastIndex=
55
+ n,h.test(t),n=h.lastIndex);do e=t.charCodeAt(n++);while(e<=32&&(e===32||e===10||
56
+ e===13||e===9));if(e===93){if(a!==void 0&&R(a,o),c=o,s===0)break e;if(o=f[--s],r=
57
+ f[--s],u=typeof r=="number",u===!0){o[r++]=c;continue}else {o[r]=c;continue t}}if(r!==
58
+ 0){e!==44&&b("',' or ']' after value"),g===!0&&s>2&&(h.lastIndex=n,h.test(t),n=h.
59
+ lastIndex);do e=t.charCodeAt(n++);while(e<=32&&(e===32||e===10||e===13||e===9))}
60
+ switch(e){case 34:o[r++]=S();continue;case 123:s===l&&I(),f[s++]=r,f[s++]=o,o={},
61
+ r=void 0,u=!1;continue t;case 91:s===l&&I(),f[s++]=r,f[s++]=o,o=[],r=0;continue;default:
62
+ o[r++]=E();}}else for(;;){g===!0&&s>2&&(h.lastIndex=n,h.test(t),n=h.lastIndex);do
63
+ e=t.charCodeAt(n++);while(e<=32&&(e===32||e===10||e===13||e===9));if(e===125){if(a!==
64
+ void 0&&R(a,o),c=o,s===0)break e;if(o=f[--s],r=f[--s],u=typeof r=="number",u===!0){
65
+ o[r++]=c;continue t}else {o[r]=c;continue}}if(r!==void 0){e!==44&&b("',' or '}' a\
66
+ fter value"),g===!0&&s>2&&(h.lastIndex=n,h.test(t),n=h.lastIndex);do e=t.charCodeAt(
67
+ n++);while(e<=32&&(e===32||e===10||e===13||e===9))}e!==34&&b("'}' or double-quot\
68
+ ed key"),r=S();do e=t.charCodeAt(n++);while(e<=32&&(e===32||e===10||e===13||e===
69
+ 9));e!==58&&b("':' after key");do e=t.charCodeAt(n++);while(e<=32&&(e===32||e===
70
+ 10||e===13||e===9));switch(e){case 34:o[r]=S();continue;case 123:s===l&&I(),f[s++]=
71
+ r,f[s++]=o,o={},r=void 0;continue;case 91:s===l&&I(),f[s++]=r,f[s++]=o,o=[],r=0,
72
+ u=!0;continue t;default:o[r]=E();}}}return J.lastIndex=n,J.test(t)===!1&&A("Unexp\
73
+ ected data after end of JSON input"),a!==void 0&&(c={"":c},R(a,c),c=c[""]),c}C$1(K$1,
74
+ "parse");
75
+
76
+ var E=Object.defineProperty;var T=Object.getOwnPropertySymbols;var F=Object.prototype.hasOwnProperty,V=Object.prototype.propertyIsEnumerable;var D=(t,e,i)=>e in t?E(t,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):
77
+ t[e]=i,u=(t,e)=>{for(var i in e||(e={}))F.call(e,i)&&D(t,i,e[i]);if(T)for(var i of T(
78
+ e))V.call(e,i)&&D(t,i,e[i]);return t};var K=(t,e)=>E(t,"name",{value:e,configurable:!0});/**
79
+ * https://github.com/jawj/json-custom-numbers
80
+ * @copyright Copyright (c) 2023 George MacKerron
81
+ * @license MIT
82
+ *
83
+ * This file implements a non-recursive JSON stringifier that's intended to
84
+ * precisely match native `JSON.stringify` behaviour but also allow for custom
85
+ * stringifying of numbers.
86
+ */const x=/["\\\u0000-\u001f]/,q=Object.prototype.hasOwnProperty,z={maxDepth:5e4,
87
+ skipToJSON:!1};function C(t,e,i,j,g={}){typeof g=="number"&&(g={maxDepth:g}),g=u(
88
+ u({},z),g);const{maxDepth:J,skipToJSON:L}=g;let O,S;e!==void 0&&(typeof e=="func\
89
+ tion"?O=e:Array.isArray(e)&&(S=e.map(l=>String(l)))),i!==void 0&&(i=typeof i=="s\
90
+ tring"?i.slice(0,10):typeof i=="number"?" ".slice(0,i):void 0);const P=J*
91
+ (i===void 0?5:6);let r,s={"":t},d=0,c=[""],h=!1,k=1,f=[],n=0,y="",b=`
92
+ `,o,p=new Set([]);do{if(d===k){p.delete(s),i!==void 0&&(b=f[--n],y+=b),y+=c===void 0?
93
+ "]":"}",k=f[--n],h=f[--n],c=f[--n],d=f[--n],s=f[--n];continue}let l,w;c===void 0?
94
+ (r=String(d),t=s[d]):(r=c[d],t=s[r]);let a=typeof t;if(L===!1&&t&&a==="object"&&
95
+ typeof t.toJSON=="function"&&(t=t.toJSON(r),a=typeof t),O!==void 0&&(t=O.call(s,
96
+ r,t),a=typeof t),j===void 0||(o=j(r,t,a))===void 0)switch(a){case"string":o=x.test(
97
+ t)?JSON.stringify(t):'"'+t+'"';break;case"number":o=isFinite(t)?String(t):"null";
98
+ break;case"boolean":o=t===!0?"true":"false";break;case"object":if(t===null){o="n\
99
+ ull";break}if(Array.isArray(t)){const m=t.length;m===0?o="[]":(o="[",l=void 0,w=
100
+ m);break}const N=S===void 0?Object.keys(t):S.filter(m=>q.call(t,m)),A=N.length;A===
101
+ 0?o="{}":(o="{",l=N,w=A);break;case"bigint":throw new TypeError("Do not know how\
102
+ to serialize a BigInt: please provide a custom serializer function");default:o=
103
+ void 0;}if(c===void 0?(d>0&&(y+=","),i!==void 0&&(y+=b),y+=o===void 0?"null":o):o!==
104
+ void 0&&(h?y+=",":h=!0,n>0&&(y+=i===void 0?(x.test(r)?JSON.stringify(r):'"'+r+'"')+
105
+ ":":b+(x.test(r)?JSON.stringify(r):'"'+r+'"')+": "),y+=o),d++,w!==void 0){if(f[n++]=
106
+ s,f[n++]=d,f[n++]=c,f[n++]=h,f[n++]=k,i!==void 0&&(f[n++]=b,b+=i),s=t,d=0,c=l,h=
107
+ !1,k=w,n>P)throw new RangeError(`Maximum nesting depth exceeded (current maximum\
108
+ is ${J})`);if(p.has(s))throw new TypeError("Cannot stringify circular structure");
109
+ p.add(s);}}while(n!==0);return y||void 0}K(C,"stringify");
110
+
111
+ ////////////////////////////////////////////////////////////////////////////////
112
+ //
113
+ // Copyright (c) 2022 - 2024.
114
+ // Haixing Hu, Qubit Co. Ltd.
115
+ //
116
+ // All rights reserved.
117
+ //
118
+ ////////////////////////////////////////////////////////////////////////////////
119
+
120
+ /**
121
+ * A regular expression for testing whether a string represents an integer.
122
+ *
123
+ * @type {RegExp}
124
+ * @private
125
+ */
126
+ var INTEGER_REGEX = /^[+-]?\d+$/;
127
+
128
+ /**
129
+ * Test whether a string represents an integer.
130
+ *
131
+ * @param value
132
+ * The string to test.
133
+ * @return {boolean}
134
+ * true if the string contains an integer; false otherwise.
135
+ */
136
+ function isInteger(value) {
137
+ return INTEGER_REGEX.test(value);
138
+ }
139
+
140
+ ////////////////////////////////////////////////////////////////////////////////
141
+ //
142
+ // Copyright (c) 2022 - 2024.
143
+ // Haixing Hu, Qubit Co. Ltd.
144
+ //
145
+ // All rights reserved.
146
+ //
147
+ ////////////////////////////////////////////////////////////////////////////////
148
+
149
+ var EXPONENTIAL_PART_REGEX = /[eE][+-]?\d+$/;
150
+ var LEADING_MINUS_AND_ZEROS_REGEX = /^-?(0*)?/;
151
+ var DOT_REGEX = /\./;
152
+ var TRAILING_ZEROS_REGEX = /0+$/;
153
+
154
+ /**
155
+ * Get the significant digits of a number.
156
+ *
157
+ * For example:
158
+ * - '2.34' returns '234'
159
+ * - '-77' returns '77'
160
+ * - '0.003400' returns '34'
161
+ * - '120.5e+30' returns '1205'
162
+ *
163
+ * @param {string} value
164
+ * The string representation of the number.
165
+ * @return {string}
166
+ * The significant digits of the number.
167
+ * @private
168
+ */
169
+ function extractSignificantDigits(value) {
170
+ return value.replace(EXPONENTIAL_PART_REGEX, '') // from "-0.250e+30" to "-0.250"
171
+ .replace(DOT_REGEX, '') // from "-0.250" to "-0250"
172
+ .replace(TRAILING_ZEROS_REGEX, '') // from "-0250" to "-025"
173
+ .replace(LEADING_MINUS_AND_ZEROS_REGEX, ''); // from "-025" to "25"
174
+ }
175
+
176
+ ////////////////////////////////////////////////////////////////////////////////
177
+ //
178
+ // Copyright (c) 2022 - 2024.
179
+ // Haixing Hu, Qubit Co. Ltd.
180
+ //
181
+ // All rights reserved.
182
+ //
183
+ ////////////////////////////////////////////////////////////////////////////////
184
+ var DEFAULT_APPROX = false;
185
+ var DEFAULT_REQUIRED_DIGITS = 14;
186
+
187
+ /**
188
+ * Test whether a string can be safely represented with a number without
189
+ * information loss.
190
+ *
191
+ * When the argument `approx` is true, floating point numbers that lose a few
192
+ * digits but are still approximately equal in value are considered safe too.
193
+ * Integer numbers must still be exactly equal.
194
+ *
195
+ * @param {string} value
196
+ * The string to test.
197
+ * @param {object} options
198
+ * The options of the test. It may contain the following properties:
199
+ * - `approx: boolean`: indicates whether to consider approximately equal
200
+ * floating point numbers as safe. Default is `false`.
201
+ * - `requiredDigits: number`: the number of significant digits required for
202
+ * a floating point number to be considered approximately equal. Default
203
+ * is `14`.
204
+ * @return {boolean}
205
+ * true if the string can be safely represented with a number; false
206
+ * otherwise.
207
+ */
208
+ function isSafeNumber(value) {
209
+ var _options$approx;
210
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
211
+ var num = parseFloat(value);
212
+ var str = String(num);
213
+ if (value === str) {
214
+ return true;
215
+ }
216
+ var v = extractSignificantDigits(value);
217
+ var s = extractSignificantDigits(str);
218
+ if (v === s) {
219
+ return true;
220
+ }
221
+ var approx = (_options$approx = options === null || options === void 0 ? void 0 : options.approx) !== null && _options$approx !== void 0 ? _options$approx : DEFAULT_APPROX;
222
+ if (approx === true) {
223
+ var _options$requiredDigi;
224
+ // A value is approximately equal when:
225
+ // 1. it is a floating point number, not an integer
226
+ // 2. it has at least requiredDigits digits
227
+ // 3. the first requiredDigits digits are equal
228
+ var requiredDigits = (_options$requiredDigi = options === null || options === void 0 ? void 0 : options.requiredDigits) !== null && _options$requiredDigi !== void 0 ? _options$requiredDigi : DEFAULT_REQUIRED_DIGITS;
229
+ if (!isInteger(value) && s.length >= requiredDigits && v.startsWith(s.substring(0, requiredDigits))) {
230
+ return true;
231
+ }
232
+ }
233
+ return false;
234
+ }
235
+
236
+ ////////////////////////////////////////////////////////////////////////////////
237
+ //
238
+ // Copyright (c) 2022 - 2024.
239
+ // Haixing Hu, Qubit Co. Ltd.
240
+ //
241
+ // All rights reserved.
242
+ //
243
+ ////////////////////////////////////////////////////////////////////////////////
244
+
245
+ /**
246
+ * A regular expression for testing whether a string represents a number.
247
+ *
248
+ * @type {RegExp}
249
+ * @see http://stackoverflow.com/questions/13340717/json-numbers-regular-expression
250
+ * @private
251
+ */
252
+ var NUMBER_REGEX = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/;
253
+
254
+ /**
255
+ * Test whether a string represents a number
256
+ *
257
+ * @param {string} value
258
+ * The string to test.
259
+ * @return {boolean}
260
+ * true if the string contains a number; false otherwise.
261
+ */
262
+ function isNumber(value) {
263
+ return NUMBER_REGEX.test(value);
264
+ }
265
+
266
+ ////////////////////////////////////////////////////////////////////////////////
267
+ //
268
+ // Copyright (c) 2022 - 2024.
269
+ // Haixing Hu, Qubit Co. Ltd.
270
+ //
271
+ // All rights reserved.
272
+ //
273
+ ////////////////////////////////////////////////////////////////////////////////
274
+
275
+ /**
276
+ * When the provided value is an unsafe number, describe what the reason is
277
+ * - `'overflow'`
278
+ * - `'underflow'`
279
+ * - `'truncate_integer'`
280
+ * - `'truncate_float'`
281
+ * - `'none'` (when the value is safe)
282
+ *
283
+ * @param {string} value
284
+ * The string represents a number to test.
285
+ * @return {string}
286
+ * The reason why the value is unsafe. Returns `'none'` when the value is safe.
287
+ */
288
+ function getUnsafeReason(value) {
289
+ var num = parseFloat(value);
290
+ var str = String(num);
291
+ if (value === str) {
292
+ return 'none';
293
+ }
294
+ var v = extractSignificantDigits(value);
295
+ var s = extractSignificantDigits(str);
296
+ if (v === s) {
297
+ return 'none';
298
+ }
299
+ if (isInteger(value)) {
300
+ return 'truncate_integer';
301
+ }
302
+ if (!Number.isFinite(num)) {
303
+ return 'overflow';
304
+ }
305
+ if (num === 0) {
306
+ return 'underflow';
307
+ }
308
+ return 'truncate_float';
309
+ }
310
+
311
+ /**
312
+ * The class of lossless numbers, which stores its numeric value as string.
313
+ */
314
+ var LosslessNumber = /*#__PURE__*/function () {
315
+ /**
316
+ * Constructs a new instance of `LosslessNumber`.
317
+ *
318
+ * @param {string} value
319
+ * The string representation of the number.
320
+ */
321
+ function LosslessNumber(value) {
322
+ _classCallCheck(this, LosslessNumber);
323
+ /**
324
+ * The string representation of the number.
325
+ *
326
+ * @type {string}
327
+ */
328
+ _defineProperty(this, "value", '');
329
+ /**
330
+ * Whether the value is a lossless number.
331
+ *
332
+ * @type {boolean}
333
+ */
334
+ _defineProperty(this, "isLosslessNumber", true);
335
+ if (!isNumber(value)) {
336
+ throw new Error("Invalid number (value: \"".concat(value, "\")"));
337
+ }
338
+ this.value = value;
339
+ }
340
+
341
+ /**
342
+ * Get the value of the LosslessNumber as number or bigint.
343
+ *
344
+ * - a number is returned for safe numbers and decimal values that only lose
345
+ * some insignificant digits;
346
+ * - a bigint is returned for big integer numbers;
347
+ * - an Error is thrown for values that will overflow or underflow;
348
+ *
349
+ * Note that you can implement your own strategy for conversion by just
350
+ * getting the value as string via `.toString()`, and using util functions
351
+ * like `isInteger`, `isSafeNumber`, `getUnsafeReason`, and `toSafeNumberOrThrow`
352
+ * to convert it to a numeric value.
353
+ *
354
+ * @return {number|bigint}
355
+ * the numeric value of the LosslessNumber, as number or bigint.
356
+ */
357
+ return _createClass(LosslessNumber, [{
358
+ key: "valueOf",
359
+ value: function valueOf() {
360
+ var unsafeReason = getUnsafeReason(this.value);
361
+ switch (unsafeReason) {
362
+ case 'none':
363
+ return parseFloat(this.value);
364
+ case 'truncate_float':
365
+ return parseFloat(this.value);
366
+ case 'truncate_integer':
367
+ return BigInt(this.value);
368
+ case 'overflow':
369
+ case 'underflow':
370
+ throw new Error('Cannot safely CONVERT TO NUMBER: ' + "the value '".concat(this.value, "' would ").concat(unsafeReason, " ") + "and become ".concat(parseFloat(this.value)));
371
+ default:
372
+ throw new Error('Unknown unsafe reason');
373
+ }
374
+ }
375
+
376
+ /**
377
+ * Get the value of this `LosslessNumber` as string.
378
+ *
379
+ * @return {string}
380
+ * the string representation of this `LosslessNumber`.
381
+ */
382
+ }, {
383
+ key: "toString",
384
+ value: function toString() {
385
+ return this.value;
386
+ }
387
+
388
+ // Note: we do NOT implement a .toJSON() method, and you should not implement
389
+ // or use that, it cannot safely turn the numeric value in the string into
390
+ // stringified JSON since it has to be parsed into a number first.
391
+ }]);
392
+ }();
393
+
394
+ ////////////////////////////////////////////////////////////////////////////////
395
+ //
396
+ // Copyright (c) 2022 - 2024.
397
+ // Haixing Hu, Qubit Co. Ltd.
398
+ //
399
+ // All rights reserved.
400
+ //
401
+ ////////////////////////////////////////////////////////////////////////////////
402
+
403
+ /**
404
+ * A custom function to parse a number from a string, which supports lossless
405
+ * number parsing.
406
+ *
407
+ * @param {string} key
408
+ * The key of the property.
409
+ * @param {string} value
410
+ * The value of the property.
411
+ * @return {any}
412
+ * The parsed value.
413
+ * @private
414
+ */
415
+ function parseNumber(key, value) {
416
+ // if (isBigInt(value)) {
417
+ // return BigInt(value.slice(0, -1));
418
+ // }
419
+ if (isSafeNumber(value, {
420
+ approx: false
421
+ })) {
422
+ return parseFloat(value);
423
+ }
424
+ if (isInteger(value)) {
425
+ return BigInt(value);
426
+ }
427
+ return new LosslessNumber(value);
428
+ }
429
+
430
+ ////////////////////////////////////////////////////////////////////////////////
431
+ //
432
+ // Copyright (c) 2022 - 2024.
433
+ // Haixing Hu, Qubit Co. Ltd.
434
+ //
435
+ // All rights reserved.
436
+ //
437
+ ////////////////////////////////////////////////////////////////////////////////
438
+
439
+ /**
440
+ * Formats a number value.
441
+ *
442
+ * @param {string} key
443
+ * The key of the property.
444
+ * @param {string} value
445
+ * The value of the property.
446
+ * @param {string} type
447
+ * The type of the value.
448
+ * @return {undefined|string}
449
+ * The formatted value. If the value is not a number, then return undefined.
450
+ * @private
451
+ */
452
+ function formatNumber(key, value, type) {
453
+ switch (type) {
454
+ case 'bigint':
455
+ return value.toString();
456
+ case 'object':
457
+ // if (value instanceof Set) {
458
+ // return Array.from(value);
459
+ // }
460
+ // if (value instanceof Map) {
461
+ // return Array.from(value);
462
+ // }
463
+ if (value instanceof LosslessNumber) {
464
+ return value.toString();
465
+ }
466
+ return undefined;
467
+ default:
468
+ return undefined;
469
+ }
470
+ }
471
+
472
+ ////////////////////////////////////////////////////////////////////////////////
473
+ //
474
+ // Copyright (c) 2022 - 2024.
475
+ // Haixing Hu, Qubit Co. Ltd.
476
+ //
477
+ // All rights reserved.
478
+ //
479
+ ////////////////////////////////////////////////////////////////////////////////
480
+
481
+ /**
482
+ * A JSON replacer which serialize collections as arrays.
483
+ *
484
+ * @param {string} key
485
+ * The key of the property.
486
+ * @param {any} value
487
+ * The value of the property.
488
+ * @param {undefined|null|function} fallbackReplacer
489
+ * The fallback replacer.
490
+ * @return {any}
491
+ * The serialized value.
492
+ * @private
493
+ */
494
+ function collectionReplacer(key, value, fallbackReplacer) {
495
+ if (value instanceof Set) {
496
+ return Array.from(value);
497
+ }
498
+ if (value instanceof Map) {
499
+ return Array.from(value);
500
+ }
501
+ if (fallbackReplacer) {
502
+ return fallbackReplacer(key, value);
503
+ }
504
+ return value;
505
+ }
506
+
507
+ ////////////////////////////////////////////////////////////////////////////////
508
+ //
509
+ // Copyright (c) 2022 - 2024.
510
+ // Haixing Hu, Qubit Co. Ltd.
511
+ //
512
+ // All rights reserved.
513
+ //
514
+ ////////////////////////////////////////////////////////////////////////////////
515
+
516
+ /**
517
+ * A customized JSON object which supports lossless number parsing and stringifying.
518
+ *
519
+ * This object provides two methods: `parse` and `stringify`, which are the same as the
520
+ * `JSON.parse` and `JSON.stringify` methods, except that they support lossless number
521
+ * parsing and stringifying.
522
+ *
523
+ * @type {object}
524
+ */
525
+ var json = {
526
+ parse: function parse(text, reviver) {
527
+ return K$1(text, reviver, parseNumber);
528
+ },
529
+ stringify: function stringify(value, replacer, space) {
530
+ return C(value, function (k, v) {
531
+ return collectionReplacer(k, v, replacer);
532
+ }, space, formatNumber);
533
+ }
534
+ };
535
+ json[Symbol.toStringTag] = 'JSON';
536
+
537
+ ////////////////////////////////////////////////////////////////////////////////
538
+ //
539
+ // Copyright (c) 2022 - 2024.
540
+ // Haixing Hu, Qubit Co. Ltd.
541
+ //
542
+ // All rights reserved.
543
+ //
544
+ ////////////////////////////////////////////////////////////////////////////////
545
+ /**
546
+ * A regular expression for testing whether a string represents a BigInt.
547
+ *
548
+ * @type {RegExp}
549
+ * @private
550
+ */
551
+ var BIG_INTEGER_REGEX = /^[+-]?\d+n$/;
552
+
553
+ /**
554
+ * Test whether a string represents a BigInt.
555
+ *
556
+ * @param value
557
+ * The string to test.
558
+ * @return {boolean}
559
+ * true if the string contains a BigInt; false otherwise.
560
+ */
561
+ function isBigInt(value) {
562
+ return BIG_INTEGER_REGEX.test(value);
563
+ }
564
+
565
+ ////////////////////////////////////////////////////////////////////////////////
566
+ //
567
+ // Copyright (c) 2022 - 2024.
568
+ // Haixing Hu, Qubit Co. Ltd.
569
+ //
570
+ // All rights reserved.
571
+ //
572
+ ////////////////////////////////////////////////////////////////////////////////
573
+
574
+ /**
575
+ * Convert a string into a number when it is safe to do so.
576
+ * Throws an error otherwise, explaining the reason.
577
+ *
578
+ * @param {string} value
579
+ * The string to convert into a number.
580
+ * @param {object} options
581
+ * The options for conversion.
582
+ * @return {number}
583
+ * The converted safe number.
584
+ * @throws {Error}
585
+ * When the value is not safe to convert.
586
+ */
587
+ function toSafeNumberOrThrow(value) {
588
+ var _options$approx;
589
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
590
+ var number = parseFloat(value);
591
+ var unsafeReason = getUnsafeReason(value);
592
+ if (unsafeReason === 'none') {
593
+ return number;
594
+ }
595
+ var approx = (_options$approx = options === null || options === void 0 ? void 0 : options.approx) !== null && _options$approx !== void 0 ? _options$approx : DEFAULT_APPROX;
596
+ // when approx is true, we allow truncating float values
597
+ if (approx && unsafeReason === 'truncate_float') {
598
+ return number;
599
+ }
600
+ var unsafeReasonText = unsafeReason === null || unsafeReason === void 0 ? void 0 : unsafeReason.replace(/_/, ' ');
601
+ throw new Error('Cannot safely convert to number: ' + "the value '".concat(value, "' would ").concat(unsafeReasonText, " and become ").concat(number));
602
+ }
603
+
604
+ ////////////////////////////////////////////////////////////////////////////////
605
+ //
606
+ // Copyright (c) 2022 - 2024.
607
+ // Haixing Hu, Qubit Co. Ltd.
608
+ //
609
+ // All rights reserved.
610
+ //
611
+ ////////////////////////////////////////////////////////////////////////////////
612
+
613
+ exports.Json = json;
614
+ exports.LosslessNumber = LosslessNumber;
615
+ exports.default = json;
616
+ exports.getUnsafeReason = getUnsafeReason;
617
+ exports.isBigInt = isBigInt;
618
+ exports.isInteger = isInteger;
619
+ exports.isNumber = isNumber;
620
+ exports.isSafeNumber = isSafeNumber;
621
+ exports.toSafeNumberOrThrow = toSafeNumberOrThrow;
622
+ module.exports = Object.assign(exports.default, exports);
623
+ //# sourceMappingURL=json.cjs.map