@ztimson/utils 0.27.7 → 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.d.ts CHANGED
@@ -9,6 +9,7 @@ export * from './files';
9
9
  export * from './emitter';
10
10
  export * from './errors';
11
11
  export * from './http';
12
+ export * from './json';
12
13
  export * from './jwt';
13
14
  export * from './logger';
14
15
  export * from './math';
package/dist/index.mjs CHANGED
@@ -117,6 +117,27 @@ ${opts.message || this.desc}`;
117
117
  `;
118
118
  }
119
119
  }
120
+ function JSONAttemptParse(json, fallback) {
121
+ try {
122
+ return JSON.parse(json);
123
+ } catch {
124
+ return fallback ?? json;
125
+ }
126
+ }
127
+ function JSONSerialize(obj) {
128
+ if (typeof obj == "object" && obj != null) return JSONSanitize(obj);
129
+ return obj;
130
+ }
131
+ function JSONSanitize(obj, space) {
132
+ const cache = [];
133
+ return JSON.stringify(obj, (key, value) => {
134
+ if (typeof value === "object" && value !== null) {
135
+ if (cache.includes(value)) return "[Circular]";
136
+ cache.push(value);
137
+ }
138
+ return value;
139
+ }, space);
140
+ }
120
141
  function applyDeltas(base, ...deltas) {
121
142
  function applyDelta(base2, delta) {
122
143
  if (delta === null) return null;
@@ -267,26 +288,8 @@ function mixin(target, constructors) {
267
288
  });
268
289
  });
269
290
  }
270
- function JSONAttemptParse(json) {
271
- try {
272
- return JSON.parse(json);
273
- } catch {
274
- return json;
275
- }
276
- }
277
- function JSONSerialize(obj) {
278
- if (typeof obj == "object" && obj != null) return JSONSanitize(obj);
279
- return obj;
280
- }
281
- function JSONSanitize(obj, space) {
282
- const cache = [];
283
- return JSON.stringify(obj, (key, value) => {
284
- if (typeof value === "object" && value !== null) {
285
- if (cache.includes(value)) return "[Circular]";
286
- cache.push(value);
287
- }
288
- return value;
289
- }, space);
291
+ function objectMap(obj, fn2) {
292
+ return Object.entries(obj).map(([key, value]) => [key, fn2(key, value)]).reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {});
290
293
  }
291
294
  class ASet extends Array {
292
295
  /** Number of elements in set */
@@ -2531,6 +2534,7 @@ export {
2531
2534
  mixin,
2532
2535
  month,
2533
2536
  numSuffix,
2537
+ objectMap,
2534
2538
  pad,
2535
2539
  parseUrl,
2536
2540
  pascalCase,