@dxos/util 0.8.4-main.9735255 → 0.8.4-main.9be5663bfe

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/dist/lib/browser/index.mjs +123 -42
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +123 -42
  5. package/dist/lib/node-esm/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/circular-buffer.d.ts +1 -0
  8. package/dist/types/src/circular-buffer.d.ts.map +1 -1
  9. package/dist/types/src/complex.d.ts +15 -0
  10. package/dist/types/src/complex.d.ts.map +1 -1
  11. package/dist/types/src/composite-key.d.ts +10 -0
  12. package/dist/types/src/composite-key.d.ts.map +1 -0
  13. package/dist/types/src/composite-key.test.d.ts +2 -0
  14. package/dist/types/src/composite-key.test.d.ts.map +1 -0
  15. package/dist/types/src/deep.d.ts +0 -3
  16. package/dist/types/src/deep.d.ts.map +1 -1
  17. package/dist/types/src/di-key.d.ts +7 -5
  18. package/dist/types/src/di-key.d.ts.map +1 -1
  19. package/dist/types/src/index.d.ts +1 -0
  20. package/dist/types/src/index.d.ts.map +1 -1
  21. package/dist/types/src/platform.d.ts.map +1 -1
  22. package/dist/types/src/safe-parse.d.ts +1 -0
  23. package/dist/types/src/safe-parse.d.ts.map +1 -1
  24. package/dist/types/src/safe-stringify.d.ts +1 -1
  25. package/dist/types/src/safe-stringify.d.ts.map +1 -1
  26. package/dist/types/src/safe-stringify.test.d.ts +2 -0
  27. package/dist/types/src/safe-stringify.test.d.ts.map +1 -0
  28. package/dist/types/src/to-fallback.d.ts +18 -4
  29. package/dist/types/src/to-fallback.d.ts.map +1 -1
  30. package/dist/types/src/types.d.ts +1 -1
  31. package/dist/types/src/types.d.ts.map +1 -1
  32. package/dist/types/src/weak.d.ts +15 -0
  33. package/dist/types/src/weak.d.ts.map +1 -1
  34. package/dist/types/tsconfig.tsbuildinfo +1 -1
  35. package/package.json +6 -10
  36. package/src/circular-buffer.test.ts +26 -0
  37. package/src/circular-buffer.ts +5 -0
  38. package/src/complex.ts +32 -0
  39. package/src/composite-key.test.ts +31 -0
  40. package/src/composite-key.ts +16 -0
  41. package/src/deep.ts +0 -6
  42. package/src/di-key.ts +10 -8
  43. package/src/index.ts +1 -0
  44. package/src/platform.ts +28 -6
  45. package/src/safe-parse.ts +8 -0
  46. package/src/safe-stringify.test.ts +96 -0
  47. package/src/safe-stringify.ts +15 -8
  48. package/src/to-fallback.ts +39 -156
  49. package/src/types.ts +1 -1
  50. package/src/weak.ts +52 -14
@@ -335,6 +335,10 @@ var CircularBuffer = class {
335
335
  get elementCount() {
336
336
  return this._elementCount;
337
337
  }
338
+ clear() {
339
+ this._nextIndex = 0;
340
+ this._elementCount = 0;
341
+ }
338
342
  getLast() {
339
343
  if (this._elementCount === 0) {
340
344
  return void 0;
@@ -528,6 +532,36 @@ var ComplexMap = class _ComplexMap {
528
532
  this._values.set(primitive, value);
529
533
  return this;
530
534
  }
535
+ /**
536
+ * Returns the value for the given key if present, otherwise inserts and returns the default value.
537
+ * @param key - The key to look up or insert.
538
+ * @param defaultValue - The value to insert if the key is not present.
539
+ * @returns The existing or newly inserted value.
540
+ */
541
+ getOrInsert(key, defaultValue) {
542
+ const primitive = this._keyProjection(key);
543
+ if (this._values.has(primitive)) {
544
+ return this._values.get(primitive);
545
+ }
546
+ this.set(key, defaultValue);
547
+ return defaultValue;
548
+ }
549
+ /**
550
+ * Returns the value for the given key if present, otherwise computes, inserts, and returns a new value.
551
+ * The callback is only invoked when the key is missing.
552
+ * @param key - The key to look up or insert.
553
+ * @param callbackfn - Function to compute the value if the key is not present.
554
+ * @returns The existing or newly computed value.
555
+ */
556
+ getOrInsertComputed(key, callbackfn) {
557
+ const primitive = this._keyProjection(key);
558
+ if (this._values.has(primitive)) {
559
+ return this._values.get(primitive);
560
+ }
561
+ const value = callbackfn(key);
562
+ this.set(key, value);
563
+ return value;
564
+ }
531
565
  get size() {
532
566
  return this._keys.size;
533
567
  }
@@ -567,15 +601,18 @@ var makeMap = (keyProjection) => class BoundComplexMap extends ComplexMap {
567
601
  }
568
602
  };
569
603
 
604
+ // src/composite-key.ts
605
+ var SEPARATOR = ":";
606
+ var compositeKey = (...parts) => parts.join(SEPARATOR);
607
+ var splitCompositeKey = (key) => key.split(SEPARATOR);
608
+
570
609
  // src/deep.ts
571
- import get from "lodash.get";
572
- import set from "lodash.set";
573
610
  import { invariant as invariant4 } from "@dxos/invariant";
574
611
  var __dxlog_file4 = "/__w/dxos/dxos/packages/common/util/src/deep.ts";
575
612
  var setDeep = (obj, path, value) => {
576
613
  invariant4(path.length > 0, void 0, {
577
614
  F: __dxlog_file4,
578
- L: 18,
615
+ L: 12,
579
616
  S: void 0,
580
617
  A: [
581
618
  "path.length > 0",
@@ -1018,7 +1055,7 @@ var defaultMap = (map, key, def) => {
1018
1055
  };
1019
1056
 
1020
1057
  // src/instance-id.ts
1021
- var symbol = Symbol.for("dxos.instance-contexts");
1058
+ var symbol = /* @__PURE__ */ Symbol.for("dxos.instance-contexts");
1022
1059
  var instanceContexts = globalThis[symbol] ??= /* @__PURE__ */ new WeakMap();
1023
1060
  var getPrototypeSpecificInstanceId = (instance) => {
1024
1061
  const prototype = Object.getPrototypeOf(instance);
@@ -1400,7 +1437,7 @@ var iosCheck = () => {
1400
1437
  };
1401
1438
  var safariCheck = () => typeof navigator !== "undefined" && /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
1402
1439
  var getHostPlatform = () => {
1403
- if (!("navigator" in window)) {
1440
+ if (!("navigator" in globalThis)) {
1404
1441
  return "unknown";
1405
1442
  }
1406
1443
  const platform = (navigator.userAgentData?.platform || navigator.platform)?.toLowerCase();
@@ -1595,7 +1632,7 @@ var safeAwaitAll = async (source, taskFactory, onError) => {
1595
1632
  };
1596
1633
 
1597
1634
  // src/safe-instanceof.ts
1598
- var instanceTag = Symbol("instanceTag");
1635
+ var instanceTag = /* @__PURE__ */ Symbol("instanceTag");
1599
1636
  var safeInstanceof = (tag) => (target) => {
1600
1637
  target.prototype[instanceTag] = tag;
1601
1638
  Object.defineProperty(target.prototype, Symbol.hasInstance, {
@@ -1632,6 +1669,13 @@ var safeParseJson = (str, defaultValue) => {
1632
1669
  }
1633
1670
  return defaultValue;
1634
1671
  };
1672
+ var safeUrl = (str) => {
1673
+ try {
1674
+ return new URL(str ?? "");
1675
+ } catch {
1676
+ return void 0;
1677
+ }
1678
+ };
1635
1679
 
1636
1680
  // src/safe-stringify.ts
1637
1681
  var SKIP = Object.freeze({});
@@ -1642,6 +1686,9 @@ function safeStringify(obj, filter = defaultFilter, indent = 2) {
1642
1686
  let path = key;
1643
1687
  if (!key) {
1644
1688
  path = "$";
1689
+ if (value != null && typeof value === "object") {
1690
+ seen.set(value, path);
1691
+ }
1645
1692
  return value;
1646
1693
  } else if (this) {
1647
1694
  const parentPath = seen.get(this);
@@ -1653,7 +1700,7 @@ function safeStringify(obj, filter = defaultFilter, indent = 2) {
1653
1700
  if (typeof value === "function") {
1654
1701
  return void 0;
1655
1702
  }
1656
- if (typeof value === "object" && Object.getPrototypeOf(value) !== Object.prototype) {
1703
+ if (typeof value === "object" && Object.getPrototypeOf(value) !== Object.prototype && !Array.isArray(value)) {
1657
1704
  return void 0;
1658
1705
  }
1659
1706
  if (typeof value === "object" && value !== null) {
@@ -1674,7 +1721,11 @@ function safeStringify(obj, filter = defaultFilter, indent = 2) {
1674
1721
  return `ERROR: ${error.message}`;
1675
1722
  }
1676
1723
  }
1677
- return JSON.stringify(obj, replacer, indent);
1724
+ try {
1725
+ return JSON.stringify(obj, replacer, indent);
1726
+ } catch (error) {
1727
+ return `ERROR: ${error.message}`;
1728
+ }
1678
1729
  }
1679
1730
  var createReplacer = ({ omit: omit2, parse, maxDepth, maxArrayLen, maxStringLen } = {}) => {
1680
1731
  let currentDepth = 0;
@@ -1689,6 +1740,9 @@ var createReplacer = ({ omit: omit2, parse, maxDepth, maxArrayLen, maxStringLen
1689
1740
  if (typeof value === "function") {
1690
1741
  return SKIP;
1691
1742
  }
1743
+ if (maxArrayLen != null && Array.isArray(value) && value.length > maxArrayLen) {
1744
+ return `[length: ${value.length}]`;
1745
+ }
1692
1746
  if (value && typeof value === "object") {
1693
1747
  depthMap.set(value, currentDepth);
1694
1748
  if (maxDepth != null && currentDepth >= maxDepth) {
@@ -1705,9 +1759,6 @@ var createReplacer = ({ omit: omit2, parse, maxDepth, maxArrayLen, maxStringLen
1705
1759
  return value;
1706
1760
  }
1707
1761
  }
1708
- if (maxArrayLen != null && Array.isArray(value) && value.length > maxArrayLen) {
1709
- return `[length: ${value.length}]`;
1710
- }
1711
1762
  if (maxStringLen != null && typeof value === "string" && value.length > maxStringLen) {
1712
1763
  return value.slice(0, maxStringLen) + "...";
1713
1764
  }
@@ -1834,18 +1885,6 @@ var throwUnhandledError = (error) => {
1834
1885
 
1835
1886
  // src/to-fallback.ts
1836
1887
  var idEmoji = [
1837
- // When changing this set, please check the result in a console or e.g. RunKit (https://runkit.com/thure/642214441dd6ae000855a8de)
1838
- // Emoji sometimes use a combination of code points, and some code points aren't visible on their own, so by adding or deleting you may unintentionally create non-visible items.
1839
- // This set was chosen from the characters in Unicode Emoji v15.0 based on the following criteria:
1840
- // – not people or isolated anthropomorphic faces
1841
- // – not flags
1842
- // – more concrete than abstract
1843
- // – less culturally specific
1844
- // – less easily confused with another emoji in the set
1845
- // – requires less special knowledge to identify
1846
- // – less likely to evoke negative feelings (no meat, no drugs, no weapons, etc)
1847
- // – less common as a signifier in UX
1848
- // NOTE that this is intentionally an array of strings because of the way emoji graphemes work.
1849
1888
  "\u{1F47B}",
1850
1889
  "\u{1F479}",
1851
1890
  "\u{1F47D}",
@@ -1981,17 +2020,18 @@ var idEmoji = [
1981
2020
  ];
1982
2021
  var idHue = [
1983
2022
  "red",
1984
- // 'orange' as const, /* More shades in these palettes are considered “ugly” */
2023
+ "orange",
1985
2024
  "amber",
1986
- // 'yellow' as const, /* More shades in these palettes are considered “ugly” */
2025
+ "yellow",
1987
2026
  "lime",
1988
2027
  "green",
1989
2028
  "emerald",
1990
2029
  "teal",
1991
2030
  "cyan",
1992
- // 'sky' as const, /* Omitted since it is quite similar to the primary accent palette */
1993
- // 'blue' as const, /* Omitted since it is quite similar to the primary accent palette */
1994
- // 'indigo' as const, /* Omitted since it is quite similar to the primary accent palette */
2031
+ // Omit colors similar to primary accent.
2032
+ // 'sky' as const,
2033
+ // 'blue' as const,
2034
+ // 'indigo' as const,
1995
2035
  "violet",
1996
2036
  "purple",
1997
2037
  "fuchsia",
@@ -2271,15 +2311,19 @@ var createUrl = (url, search) => {
2271
2311
  // src/weak.ts
2272
2312
  var WeakDictionary = class {
2273
2313
  _internal = /* @__PURE__ */ new Map();
2274
- _finalization = new FinalizationRegistry((cleanUpCallback) => {
2275
- cleanUpCallback();
2314
+ _finalization = new FinalizationRegistry(({ key, ref }) => {
2315
+ if (this._internal.get(key) === ref) {
2316
+ this._internal.delete(key);
2317
+ }
2276
2318
  });
2277
2319
  constructor(entries2) {
2278
- this._internal = new Map(entries2?.map(([key, value]) => [
2279
- key,
2280
- new WeakRef(value)
2281
- ]));
2282
- entries2?.forEach(([key, value]) => this._register(key, value));
2320
+ if (entries2) {
2321
+ for (const [key, value] of entries2) {
2322
+ const ref = new WeakRef(value);
2323
+ this._internal.set(key, ref);
2324
+ this._register(key, value, ref);
2325
+ }
2326
+ }
2283
2327
  }
2284
2328
  *entries() {
2285
2329
  for (const [key, value] of this._internal) {
@@ -2319,10 +2363,45 @@ var WeakDictionary = class {
2319
2363
  return this._internal.get(key)?.deref();
2320
2364
  }
2321
2365
  set(key, value) {
2322
- this._internal.set(key, new WeakRef(value));
2323
- this._register(key, value);
2366
+ const previous = this._internal.get(key)?.deref();
2367
+ if (previous) {
2368
+ this._unregister(previous);
2369
+ }
2370
+ const ref = new WeakRef(value);
2371
+ this._internal.set(key, ref);
2372
+ this._register(key, value, ref);
2324
2373
  return this;
2325
2374
  }
2375
+ /**
2376
+ * Returns the value for the given key if present, otherwise inserts and returns the default value.
2377
+ * @param key - The key to look up or insert.
2378
+ * @param defaultValue - The value to insert if the key is not present.
2379
+ * @returns The existing or newly inserted value.
2380
+ */
2381
+ getOrInsert(key, defaultValue) {
2382
+ const existing = this.get(key);
2383
+ if (existing !== void 0) {
2384
+ return existing;
2385
+ }
2386
+ this.set(key, defaultValue);
2387
+ return defaultValue;
2388
+ }
2389
+ /**
2390
+ * Returns the value for the given key if present, otherwise computes, inserts, and returns a new value.
2391
+ * The callback is only invoked when the key is missing.
2392
+ * @param key - The key to look up or insert.
2393
+ * @param callbackfn - Function to compute the value if the key is not present.
2394
+ * @returns The existing or newly computed value.
2395
+ */
2396
+ getOrInsertComputed(key, callbackfn) {
2397
+ const existing = this.get(key);
2398
+ if (existing !== void 0) {
2399
+ return existing;
2400
+ }
2401
+ const value = callbackfn(key);
2402
+ this.set(key, value);
2403
+ return value;
2404
+ }
2326
2405
  has(key) {
2327
2406
  return this._internal.has(key) && this._internal.get(key).deref() !== void 0;
2328
2407
  }
@@ -2353,9 +2432,10 @@ var WeakDictionary = class {
2353
2432
  }
2354
2433
  });
2355
2434
  }
2356
- _register(key, value) {
2357
- this._finalization.register(value, () => {
2358
- this._internal.delete(key);
2435
+ _register(key, value, ref) {
2436
+ this._finalization.register(value, {
2437
+ key,
2438
+ ref
2359
2439
  }, value);
2360
2440
  }
2361
2441
  _unregister(value) {
@@ -2447,6 +2527,7 @@ export {
2447
2527
  compareObject,
2448
2528
  compareScalar,
2449
2529
  compareString,
2530
+ compositeKey,
2450
2531
  createBinder,
2451
2532
  createBucketReducer,
2452
2533
  createFilename,
@@ -2470,7 +2551,6 @@ export {
2470
2551
  exponentialBackoffInterval,
2471
2552
  forEachAsync,
2472
2553
  formatErrorWithCauses,
2473
- get,
2474
2554
  getAsyncProviderValue,
2475
2555
  getDate,
2476
2556
  getDebugName,
@@ -2534,9 +2614,10 @@ export {
2534
2614
  safeParseInt,
2535
2615
  safeParseJson,
2536
2616
  safeStringify,
2537
- set,
2617
+ safeUrl,
2538
2618
  setDeep,
2539
2619
  sortKeys,
2620
+ splitCompositeKey,
2540
2621
  stringToArray,
2541
2622
  stringifyTree,
2542
2623
  stripUndefined,