@stackline/stable-stringify 1.0.0

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.
package/dist/index.js ADDED
@@ -0,0 +1,705 @@
1
+ /*! @stackline/stable-stringify v1.0.0 | MIT */
2
+
3
+ // src/index.js
4
+ var hasOwn = Object.prototype.hasOwnProperty;
5
+ var propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
6
+ var objectToString = Object.prototype.toString;
7
+ var OMIT = /* @__PURE__ */ Symbol("omit");
8
+ var ROOT_PATH = null;
9
+ var SAFE_DEFAULTS = Object.freeze({
10
+ bigint: "string",
11
+ cycleValue: "[Circular]",
12
+ maxDepth: 100,
13
+ maxEntries: 1e5,
14
+ maxLength: 1e6,
15
+ onCycle: "marker"
16
+ });
17
+ var CANONICAL_DEFAULTS = Object.freeze({
18
+ maxDepth: 1e3,
19
+ maxEntries: 1e5,
20
+ maxLength: 16 * 1024 * 1024
21
+ });
22
+ var StableStringifyLimitError = class extends RangeError {
23
+ constructor(kind, limit, path) {
24
+ const location = formatPath(path);
25
+ super(`Stable stringify ${kind} limit of ${limit} exceeded at ${location}`);
26
+ this.name = "StableStringifyLimitError";
27
+ this.code = "ERR_STABLE_STRINGIFY_LIMIT";
28
+ this.kind = kind;
29
+ this.limit = limit;
30
+ this.path = location;
31
+ }
32
+ };
33
+ var CanonicalizationError = class extends TypeError {
34
+ constructor(reason, path) {
35
+ const location = formatPath(path);
36
+ super(`JSON canonicalization failed at ${location}: ${reason}`);
37
+ this.name = "CanonicalizationError";
38
+ this.code = "ERR_JSON_CANONICALIZATION";
39
+ this.path = location;
40
+ this.reason = reason;
41
+ }
42
+ };
43
+ function stableStringify(value, inputOptions) {
44
+ const options = normalizeStableOptions(inputOptions);
45
+ return serialize(value, options, false);
46
+ }
47
+ var stringify = stableStringify;
48
+ function configure(inputDefaults) {
49
+ const defaults = typeof inputDefaults === "function" ? { cmp: inputDefaults } : normalizeInputObject(inputDefaults);
50
+ normalizeStableOptions(defaults);
51
+ return function configuredStableStringify(value, inputOptions) {
52
+ const overrides = typeof inputOptions === "function" ? { cmp: inputOptions } : normalizeInputObject(inputOptions);
53
+ return stableStringify(value, { ...defaults, ...overrides });
54
+ };
55
+ }
56
+ function safeStringify(value, replacer, space, inputOptions) {
57
+ const options = normalizeInputObject(inputOptions);
58
+ const stableOptions = {
59
+ ...SAFE_DEFAULTS,
60
+ ...options,
61
+ maxDepth: options.maxDepth === void 0 ? options.depthLimit : options.maxDepth,
62
+ maxEntries: options.maxEntries === void 0 ? options.edgesLimit : options.maxEntries,
63
+ replacer: replacer === void 0 ? options.replacer : replacer,
64
+ space: space === void 0 ? options.space : space
65
+ };
66
+ if (stableOptions.maxDepth === void 0) {
67
+ stableOptions.maxDepth = SAFE_DEFAULTS.maxDepth;
68
+ }
69
+ if (stableOptions.maxEntries === void 0) {
70
+ stableOptions.maxEntries = SAFE_DEFAULTS.maxEntries;
71
+ }
72
+ try {
73
+ return stableStringify(value, stableOptions);
74
+ } catch (error) {
75
+ if (options.throwOnError === true) throw error;
76
+ let name = "UnknownError";
77
+ try {
78
+ if (error && typeof error.name === "string") {
79
+ name = error.name.slice(0, 80);
80
+ }
81
+ } catch (e) {
82
+ }
83
+ return JSON.stringify(`[Unable to serialize: ${name}]`);
84
+ }
85
+ }
86
+ function canonicalize(value, inputOptions) {
87
+ const options = normalizeCanonicalOptions(inputOptions);
88
+ return serialize(value, options, true);
89
+ }
90
+ function canonicalizeBytes(value, inputOptions) {
91
+ return encodeUtf8(canonicalize(value, inputOptions));
92
+ }
93
+ function serialize(rootValue, options, canonical) {
94
+ const state = {
95
+ ancestors: /* @__PURE__ */ new WeakMap(),
96
+ canonical,
97
+ chunks: [],
98
+ entries: 0,
99
+ length: 0,
100
+ options
101
+ };
102
+ const holder = { "": rootValue };
103
+ const prepared = prepareValue(rootValue, holder, "", ROOT_PATH, state);
104
+ if (prepared === OMIT) return void 0;
105
+ const tasks = [
106
+ {
107
+ depth: 0,
108
+ path: ROOT_PATH,
109
+ prepared,
110
+ type: "value"
111
+ }
112
+ ];
113
+ while (tasks.length > 0) {
114
+ const task = tasks.pop();
115
+ if (task.type === "value") {
116
+ processValueTask(task, tasks, state);
117
+ } else {
118
+ processContainerTask(task, tasks, state);
119
+ }
120
+ }
121
+ return state.chunks.join("");
122
+ }
123
+ function processValueTask(task, tasks, state) {
124
+ if (task.prepared.kind === "primitive") {
125
+ append(state, task.prepared.text, task.path);
126
+ return;
127
+ }
128
+ const value = task.prepared.value;
129
+ const previousPath = state.ancestors.get(value);
130
+ if (previousPath !== void 0 || state.ancestors.has(value)) {
131
+ appendCycle(state, previousPath, task.path);
132
+ return;
133
+ }
134
+ enforceDepth(state, task.depth, task.path);
135
+ const isArray = Array.isArray(value);
136
+ if (state.canonical && !isArray) validateCanonicalObject(value, task.path);
137
+ state.ancestors.set(value, task.path);
138
+ const frame = createContainerFrame(
139
+ value,
140
+ isArray,
141
+ task.depth,
142
+ task.path,
143
+ state
144
+ );
145
+ append(state, isArray ? "[" : "{", task.path);
146
+ if (frame.total === 0) {
147
+ append(state, isArray ? "]" : "}", task.path);
148
+ state.ancestors.delete(value);
149
+ return;
150
+ }
151
+ tasks.push(frame);
152
+ }
153
+ function processContainerTask(frame, tasks, state) {
154
+ if (frame.index >= frame.total) {
155
+ if (frame.emitted && state.options.gap !== "") {
156
+ append(state, `
157
+ ${frame.indent}`, frame.path);
158
+ }
159
+ append(state, frame.isArray ? "]" : "}", frame.path);
160
+ state.ancestors.delete(frame.value);
161
+ return;
162
+ }
163
+ if (frame.isArray) {
164
+ const index = frame.index;
165
+ frame.index += 1;
166
+ const path = createPath(frame.path, index);
167
+ consumeEntry(state, path);
168
+ const rawValue = readArrayValue(frame.value, index, path, state);
169
+ let prepared = prepareValue(
170
+ rawValue,
171
+ frame.value,
172
+ String(index),
173
+ path,
174
+ state
175
+ );
176
+ if (prepared === OMIT) prepared = primitive("null");
177
+ appendItemPrefix(frame, path, state);
178
+ tasks.push(frame);
179
+ tasks.push({
180
+ depth: frame.depth + 1,
181
+ path,
182
+ prepared,
183
+ type: "value"
184
+ });
185
+ return;
186
+ }
187
+ while (frame.index < frame.total) {
188
+ const key = frame.keys[frame.index];
189
+ frame.index += 1;
190
+ const path = createPath(frame.path, key);
191
+ consumeEntry(state, path);
192
+ const rawValue = readObjectValue(frame.value, key, path, state);
193
+ const prepared = prepareValue(rawValue, frame.value, key, path, state);
194
+ if (prepared === OMIT) continue;
195
+ appendItemPrefix(frame, path, state);
196
+ append(state, JSON.stringify(key), path);
197
+ append(state, state.options.gap === "" ? ":" : ": ", path);
198
+ tasks.push(frame);
199
+ tasks.push({
200
+ depth: frame.depth + 1,
201
+ path,
202
+ prepared,
203
+ type: "value"
204
+ });
205
+ return;
206
+ }
207
+ tasks.push(frame);
208
+ }
209
+ function createContainerFrame(value, isArray, depth, path, state) {
210
+ let keys;
211
+ let total;
212
+ if (isArray) {
213
+ total = value.length;
214
+ } else {
215
+ keys = getObjectKeys(value, path, state);
216
+ total = keys.length;
217
+ }
218
+ return {
219
+ depth,
220
+ emitted: false,
221
+ indent: state.options.gap === "" ? "" : state.options.gap.repeat(depth),
222
+ index: 0,
223
+ isArray,
224
+ keys,
225
+ path,
226
+ total,
227
+ type: "container",
228
+ value
229
+ };
230
+ }
231
+ function getObjectKeys(value, path, state) {
232
+ let keys;
233
+ if (state.canonical || state.options.propertyList === void 0) {
234
+ keys = Object.keys(value);
235
+ } else {
236
+ keys = state.options.propertyList.slice();
237
+ }
238
+ if (state.canonical) {
239
+ rejectEnumerableSymbols(value, path);
240
+ for (const key of keys) {
241
+ if (hasLoneSurrogate(key)) {
242
+ throw new CanonicalizationError(
243
+ "property names must not contain lone UTF-16 surrogates",
244
+ createPath(path, key)
245
+ );
246
+ }
247
+ }
248
+ return keys.sort();
249
+ }
250
+ if (state.options.accessors !== "invoke") {
251
+ keys = keys.filter((key) => {
252
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
253
+ if (!descriptor || !descriptor.get && !descriptor.set) return true;
254
+ if (state.options.accessors === "throw") {
255
+ throw new TypeError(
256
+ `Refusing to invoke accessor at ${formatPath(createPath(path, key))}`
257
+ );
258
+ }
259
+ return false;
260
+ });
261
+ }
262
+ if (state.options.cmp) {
263
+ const comparator = state.options.cmp;
264
+ keys.sort(
265
+ (left, right) => comparator(
266
+ { key: left, value: readForComparator(value, left, path, state) },
267
+ { key: right, value: readForComparator(value, right, path, state) }
268
+ )
269
+ );
270
+ } else {
271
+ keys.sort();
272
+ }
273
+ return keys;
274
+ }
275
+ function prepareValue(rawValue, holder, key, path, state) {
276
+ if (state.canonical) return prepareCanonicalValue(rawValue, path);
277
+ let value = rawValue;
278
+ if (state.options.toJSON && value !== null && value !== void 0) {
279
+ const method = value.toJSON;
280
+ if (typeof method === "function") value = method.call(value);
281
+ }
282
+ if (state.options.replacerFunction) {
283
+ value = state.options.replacerFunction.call(holder, key, value);
284
+ }
285
+ if (value === null) return primitive("null");
286
+ switch (typeof value) {
287
+ case "string":
288
+ return primitive(JSON.stringify(value));
289
+ case "number":
290
+ return primitive(Number.isFinite(value) ? String(value) : "null");
291
+ case "boolean":
292
+ return primitive(value ? "true" : "false");
293
+ case "bigint":
294
+ return prepareBigInt(value, state.options.bigint, path);
295
+ case "object":
296
+ return { kind: "object", value };
297
+ default:
298
+ return OMIT;
299
+ }
300
+ }
301
+ function prepareCanonicalValue(value, path) {
302
+ if (value === null) return primitive("null");
303
+ switch (typeof value) {
304
+ case "string":
305
+ if (hasLoneSurrogate(value)) {
306
+ throw new CanonicalizationError(
307
+ "strings must not contain lone UTF-16 surrogates",
308
+ path
309
+ );
310
+ }
311
+ return primitive(JSON.stringify(value));
312
+ case "number":
313
+ if (!Number.isFinite(value)) {
314
+ throw new CanonicalizationError(
315
+ "NaN and Infinity are not valid I-JSON numbers",
316
+ path
317
+ );
318
+ }
319
+ return primitive(JSON.stringify(value));
320
+ case "boolean":
321
+ return primitive(value ? "true" : "false");
322
+ case "object":
323
+ return { kind: "object", value };
324
+ case "bigint":
325
+ throw new CanonicalizationError(
326
+ "BigInt values must be represented as JSON strings",
327
+ path
328
+ );
329
+ default:
330
+ throw new CanonicalizationError(
331
+ `values of type ${typeof value} are not valid JSON data`,
332
+ path
333
+ );
334
+ }
335
+ }
336
+ function prepareBigInt(value, mode, path) {
337
+ if (mode === "string") return primitive(JSON.stringify(String(value)));
338
+ if (mode === "number") {
339
+ const number = Number(value);
340
+ if (!Number.isSafeInteger(number)) {
341
+ throw new RangeError(
342
+ `BigInt at ${formatPath(path)} cannot be represented as a safe JSON number`
343
+ );
344
+ }
345
+ return primitive(String(number));
346
+ }
347
+ throw new TypeError("Do not know how to serialize a BigInt");
348
+ }
349
+ function primitive(text) {
350
+ return { kind: "primitive", text };
351
+ }
352
+ function readArrayValue(array, index, path, state) {
353
+ if (state.canonical) {
354
+ if (!hasOwn.call(array, index)) {
355
+ throw new CanonicalizationError(
356
+ "sparse arrays are not valid I-JSON data",
357
+ path
358
+ );
359
+ }
360
+ return readCanonicalDescriptor(array, String(index), path);
361
+ }
362
+ return readStableProperty(array, String(index), path, state.options.accessors);
363
+ }
364
+ function readObjectValue(object, key, path, state) {
365
+ if (state.canonical) return readCanonicalDescriptor(object, key, path);
366
+ return readStableProperty(object, key, path, state.options.accessors);
367
+ }
368
+ function readCanonicalDescriptor(object, key, path) {
369
+ const descriptor = Object.getOwnPropertyDescriptor(object, key);
370
+ if (!descriptor || descriptor.get || descriptor.set) {
371
+ throw new CanonicalizationError(
372
+ "accessor properties are not valid canonical JSON input",
373
+ path
374
+ );
375
+ }
376
+ return descriptor.value;
377
+ }
378
+ function readStableProperty(object, key, path, accessors) {
379
+ if (accessors === "invoke") return object[key];
380
+ const seen = /* @__PURE__ */ new WeakSet();
381
+ let current = object;
382
+ while (current !== null && !seen.has(current)) {
383
+ seen.add(current);
384
+ const descriptor = Object.getOwnPropertyDescriptor(current, key);
385
+ if (descriptor) {
386
+ if (!descriptor.get && !descriptor.set) return descriptor.value;
387
+ if (accessors === "throw") {
388
+ throw new TypeError(`Refusing to invoke accessor at ${formatPath(path)}`);
389
+ }
390
+ return void 0;
391
+ }
392
+ current = Object.getPrototypeOf(current);
393
+ }
394
+ return void 0;
395
+ }
396
+ function readForComparator(object, key, path, state) {
397
+ return readStableProperty(
398
+ object,
399
+ key,
400
+ createPath(path, key),
401
+ state.options.accessors
402
+ );
403
+ }
404
+ function validateCanonicalObject(value, path) {
405
+ const prototype = Object.getPrototypeOf(value);
406
+ if (prototype !== null && objectToString.call(value) !== "[object Object]") {
407
+ throw new CanonicalizationError(
408
+ "only JSON objects and arrays can be canonicalized",
409
+ path
410
+ );
411
+ }
412
+ if (prototype !== null && Object.getPrototypeOf(prototype) !== null) {
413
+ throw new CanonicalizationError(
414
+ "class instances must be converted to plain JSON objects",
415
+ path
416
+ );
417
+ }
418
+ }
419
+ function rejectEnumerableSymbols(value, path) {
420
+ if (typeof Object.getOwnPropertySymbols !== "function") return;
421
+ const symbols = Object.getOwnPropertySymbols(value);
422
+ if (symbols.some((symbol) => propertyIsEnumerable.call(value, symbol))) {
423
+ throw new CanonicalizationError(
424
+ "symbol properties are not valid JSON object members",
425
+ path
426
+ );
427
+ }
428
+ }
429
+ function appendItemPrefix(frame, path, state) {
430
+ if (state.options.gap === "") {
431
+ if (frame.emitted) append(state, ",", path);
432
+ } else {
433
+ const indentation = state.options.gap.repeat(frame.depth + 1);
434
+ append(state, frame.emitted ? `,
435
+ ${indentation}` : `
436
+ ${indentation}`, path);
437
+ }
438
+ frame.emitted = true;
439
+ }
440
+ function appendCycle(state, previousPath, currentPath) {
441
+ if (state.canonical) {
442
+ throw new CanonicalizationError("circular references are not valid JSON", currentPath);
443
+ }
444
+ switch (state.options.onCycle) {
445
+ case "marker":
446
+ append(state, JSON.stringify(state.options.cycleValue), currentPath);
447
+ return;
448
+ case "path":
449
+ append(
450
+ state,
451
+ JSON.stringify(`[Circular ${formatPath(previousPath)}]`),
452
+ currentPath
453
+ );
454
+ return;
455
+ case "null":
456
+ append(state, "null", currentPath);
457
+ return;
458
+ default:
459
+ throw new TypeError("Converting circular structure to JSON");
460
+ }
461
+ }
462
+ function append(state, text, path) {
463
+ const nextLength = state.length + text.length;
464
+ if (nextLength > state.options.maxLength) {
465
+ throw new StableStringifyLimitError(
466
+ "length",
467
+ state.options.maxLength,
468
+ path
469
+ );
470
+ }
471
+ state.length = nextLength;
472
+ state.chunks.push(text);
473
+ }
474
+ function enforceDepth(state, depth, path) {
475
+ if (depth > state.options.maxDepth) {
476
+ throw new StableStringifyLimitError(
477
+ "depth",
478
+ state.options.maxDepth,
479
+ path
480
+ );
481
+ }
482
+ }
483
+ function consumeEntry(state, path) {
484
+ state.entries += 1;
485
+ if (state.entries > state.options.maxEntries) {
486
+ throw new StableStringifyLimitError(
487
+ "entry",
488
+ state.options.maxEntries,
489
+ path
490
+ );
491
+ }
492
+ }
493
+ function normalizeStableOptions(inputOptions) {
494
+ const input = typeof inputOptions === "function" ? { cmp: inputOptions } : normalizeInputObject(inputOptions);
495
+ if (input.cmp !== void 0 && typeof input.cmp !== "function") {
496
+ throw new TypeError("cmp must be a function");
497
+ }
498
+ if (input.cycles !== void 0 && typeof input.cycles !== "boolean") {
499
+ throw new TypeError("cycles must be a boolean");
500
+ }
501
+ if (input.replacer !== void 0 && input.replacer !== null && typeof input.replacer !== "function" && !Array.isArray(input.replacer)) {
502
+ throw new TypeError("replacer must be a function or an array");
503
+ }
504
+ const onCycle = input.onCycle === void 0 ? input.cycles === true ? "marker" : "throw" : input.onCycle;
505
+ if (!["throw", "marker", "path", "null"].includes(onCycle)) {
506
+ throw new TypeError(
507
+ "onCycle must be 'throw', 'marker', 'path', or 'null'"
508
+ );
509
+ }
510
+ const bigint = input.bigint === void 0 ? "throw" : input.bigint;
511
+ if (!["throw", "string", "number"].includes(bigint)) {
512
+ throw new TypeError("bigint must be 'throw', 'string', or 'number'");
513
+ }
514
+ const accessors = input.accessors === void 0 ? "invoke" : input.accessors;
515
+ if (!["invoke", "omit", "throw"].includes(accessors)) {
516
+ throw new TypeError("accessors must be 'invoke', 'omit', or 'throw'");
517
+ }
518
+ if (input.toJSON !== void 0 && typeof input.toJSON !== "boolean") {
519
+ throw new TypeError("toJSON must be a boolean");
520
+ }
521
+ if (input.cycleValue !== void 0 && typeof input.cycleValue !== "string") {
522
+ throw new TypeError("cycleValue must be a string");
523
+ }
524
+ return {
525
+ accessors,
526
+ bigint,
527
+ cmp: input.cmp,
528
+ cycleValue: input.cycleValue === void 0 ? "__cycle__" : input.cycleValue,
529
+ gap: normalizeGap(input.space),
530
+ maxDepth: normalizeLimit(input.maxDepth, Infinity, "maxDepth"),
531
+ maxEntries: normalizeLimit(input.maxEntries, Infinity, "maxEntries"),
532
+ maxLength: normalizeLimit(input.maxLength, Infinity, "maxLength"),
533
+ onCycle,
534
+ propertyList: Array.isArray(input.replacer) ? normalizePropertyList(input.replacer) : void 0,
535
+ replacerFunction: typeof input.replacer === "function" ? input.replacer : void 0,
536
+ toJSON: input.toJSON === void 0 ? true : input.toJSON
537
+ };
538
+ }
539
+ function normalizeCanonicalOptions(inputOptions) {
540
+ const input = normalizeInputObject(inputOptions);
541
+ return {
542
+ accessors: "throw",
543
+ bigint: "throw",
544
+ cmp: void 0,
545
+ cycleValue: "",
546
+ gap: "",
547
+ maxDepth: normalizeLimit(
548
+ input.maxDepth,
549
+ CANONICAL_DEFAULTS.maxDepth,
550
+ "maxDepth"
551
+ ),
552
+ maxEntries: normalizeLimit(
553
+ input.maxEntries,
554
+ CANONICAL_DEFAULTS.maxEntries,
555
+ "maxEntries"
556
+ ),
557
+ maxLength: normalizeLimit(
558
+ input.maxLength,
559
+ CANONICAL_DEFAULTS.maxLength,
560
+ "maxLength"
561
+ ),
562
+ onCycle: "throw",
563
+ propertyList: void 0,
564
+ replacerFunction: void 0,
565
+ toJSON: false
566
+ };
567
+ }
568
+ function normalizeInputObject(value) {
569
+ if (value === void 0 || value === null) return {};
570
+ if (typeof value !== "object") {
571
+ throw new TypeError("options must be an object when provided");
572
+ }
573
+ return { ...value };
574
+ }
575
+ function normalizeLimit(value, fallback, name) {
576
+ const resolved = value === void 0 ? fallback : value;
577
+ if (resolved === Infinity) return resolved;
578
+ if (!Number.isSafeInteger(resolved) || resolved < 0) {
579
+ throw new TypeError(
580
+ `${name} must be a non-negative safe integer or Infinity`
581
+ );
582
+ }
583
+ return resolved;
584
+ }
585
+ function normalizeGap(space) {
586
+ if (typeof space === "number") {
587
+ return " ".repeat(Math.min(10, Math.max(0, Math.trunc(space))));
588
+ }
589
+ if (typeof space === "string") return space.slice(0, 10);
590
+ return "";
591
+ }
592
+ function normalizePropertyList(input) {
593
+ const output = [];
594
+ const seen = /* @__PURE__ */ new Set();
595
+ for (const item of input) {
596
+ let key;
597
+ if (typeof item === "string" || typeof item === "number") {
598
+ key = String(item);
599
+ } else if (item && (objectToString.call(item) === "[object String]" || objectToString.call(item) === "[object Number]")) {
600
+ key = String(item);
601
+ }
602
+ if (key !== void 0 && !seen.has(key)) {
603
+ seen.add(key);
604
+ output.push(key);
605
+ }
606
+ }
607
+ return output;
608
+ }
609
+ function hasLoneSurrogate(value) {
610
+ for (let index = 0; index < value.length; index += 1) {
611
+ const code = value.charCodeAt(index);
612
+ if (code >= 55296 && code <= 56319) {
613
+ const next = value.charCodeAt(index + 1);
614
+ if (!(next >= 56320 && next <= 57343)) return true;
615
+ index += 1;
616
+ } else if (code >= 56320 && code <= 57343) {
617
+ return true;
618
+ }
619
+ }
620
+ return false;
621
+ }
622
+ function createPath(parent, key) {
623
+ return { key, parent };
624
+ }
625
+ function formatPath(path) {
626
+ if (path === ROOT_PATH) return "<root>";
627
+ const parts = [];
628
+ let current = path;
629
+ while (current !== ROOT_PATH) {
630
+ parts.push(current.key);
631
+ current = current.parent;
632
+ }
633
+ parts.reverse();
634
+ let output = "<root>";
635
+ for (const part of parts) {
636
+ if (typeof part === "number") {
637
+ output += `[${part}]`;
638
+ } else if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(part)) {
639
+ output += `.${part}`;
640
+ } else {
641
+ output += `[${JSON.stringify(part)}]`;
642
+ }
643
+ }
644
+ return output;
645
+ }
646
+ function encodeUtf8(value) {
647
+ if (typeof TextEncoder === "function") return new TextEncoder().encode(value);
648
+ const bytes = [];
649
+ for (let index = 0; index < value.length; index += 1) {
650
+ let code = value.charCodeAt(index);
651
+ if (code >= 55296 && code <= 56319) {
652
+ const next = value.charCodeAt(index + 1);
653
+ code = (code - 55296 << 10) + (next - 56320) + 65536;
654
+ index += 1;
655
+ }
656
+ if (code <= 127) {
657
+ bytes.push(code);
658
+ } else if (code <= 2047) {
659
+ bytes.push(192 | code >> 6, 128 | code & 63);
660
+ } else if (code <= 65535) {
661
+ bytes.push(
662
+ 224 | code >> 12,
663
+ 128 | code >> 6 & 63,
664
+ 128 | code & 63
665
+ );
666
+ } else {
667
+ bytes.push(
668
+ 240 | code >> 18,
669
+ 128 | code >> 12 & 63,
670
+ 128 | code >> 6 & 63,
671
+ 128 | code & 63
672
+ );
673
+ }
674
+ }
675
+ return Uint8Array.from(bytes);
676
+ }
677
+ Object.defineProperties(stableStringify, {
678
+ CanonicalizationError: { value: CanonicalizationError },
679
+ StableStringifyLimitError: { value: StableStringifyLimitError },
680
+ canonicalize: { value: canonicalize },
681
+ canonicalizeBytes: { value: canonicalizeBytes },
682
+ configure: { value: configure },
683
+ default: { value: stableStringify },
684
+ safeStringify: { value: safeStringify },
685
+ stable: { value: stableStringify },
686
+ stableStringify: { value: stableStringify },
687
+ stringify: { value: stableStringify }
688
+ });
689
+ Object.defineProperties(safeStringify, {
690
+ stable: { value: stableStringify },
691
+ stableStringify: { value: stableStringify }
692
+ });
693
+ var index_default = stableStringify;
694
+ export {
695
+ CanonicalizationError,
696
+ StableStringifyLimitError,
697
+ canonicalize,
698
+ canonicalizeBytes,
699
+ configure,
700
+ index_default as default,
701
+ safeStringify,
702
+ stableStringify,
703
+ stringify
704
+ };
705
+ //# sourceMappingURL=index.js.map