@ztimson/utils 0.27.8 → 0.27.9

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.cjs CHANGED
@@ -121,6 +121,27 @@ ${opts.message || this.desc}`;
121
121
  `;
122
122
  }
123
123
  }
124
+ function JSONAttemptParse(json, fallback) {
125
+ try {
126
+ return JSON.parse(json);
127
+ } catch {
128
+ return fallback ?? json;
129
+ }
130
+ }
131
+ function JSONSerialize(obj) {
132
+ if (typeof obj == "object" && obj != null) return JSONSanitize(obj);
133
+ return obj;
134
+ }
135
+ function JSONSanitize(obj, space) {
136
+ const cache = [];
137
+ return JSON.stringify(obj, (key, value) => {
138
+ if (typeof value === "object" && value !== null) {
139
+ if (cache.includes(value)) return "[Circular]";
140
+ cache.push(value);
141
+ }
142
+ return value;
143
+ }, space);
144
+ }
124
145
  function applyDeltas(base, ...deltas) {
125
146
  function applyDelta(base2, delta) {
126
147
  if (delta === null) return null;
@@ -274,27 +295,6 @@ ${opts.message || this.desc}`;
274
295
  function objectMap(obj, fn2) {
275
296
  return Object.entries(obj).map(([key, value]) => [key, fn2(key, value)]).reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {});
276
297
  }
277
- function JSONAttemptParse(json) {
278
- try {
279
- return JSON.parse(json);
280
- } catch {
281
- return json;
282
- }
283
- }
284
- function JSONSerialize(obj) {
285
- if (typeof obj == "object" && obj != null) return JSONSanitize(obj);
286
- return obj;
287
- }
288
- function JSONSanitize(obj, space) {
289
- const cache = [];
290
- return JSON.stringify(obj, (key, value) => {
291
- if (typeof value === "object" && value !== null) {
292
- if (cache.includes(value)) return "[Circular]";
293
- cache.push(value);
294
- }
295
- return value;
296
- }, space);
297
- }
298
298
  class ASet extends Array {
299
299
  /** Number of elements in set */
300
300
  get size() {