@dxos/util 0.8.4-main.fd6878d → 0.8.4-main.fffef41

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.
@@ -103,10 +103,10 @@ function assumeType(value) {
103
103
  }
104
104
 
105
105
  // src/binder.ts
106
- import util from "@dxos/node-std/util";
106
+ import { promisify } from "@dxos/node-std/util";
107
107
  var createBinder = (obj) => ({
108
108
  fn: (fn) => fn.bind(obj),
109
- async: (fn) => util.promisify(fn.bind(obj))
109
+ async: (fn) => promisify(fn.bind(obj))
110
110
  });
111
111
 
112
112
  // src/bitfield.ts
@@ -377,11 +377,10 @@ import { inspectObject, raise } from "@dxos/debug";
377
377
  var MAX_SERIALIZATION_LENGTH = 10;
378
378
  var ComplexSet = class {
379
379
  _projection;
380
- _values;
380
+ _values = /* @__PURE__ */ new Map();
381
381
  // prettier-ignore
382
382
  constructor(_projection, values) {
383
383
  this._projection = _projection;
384
- this._values = /* @__PURE__ */ new Map();
385
384
  if (values) {
386
385
  for (const value of values) {
387
386
  this.add(value);
@@ -472,13 +471,11 @@ var makeSet = (projection) => {
472
471
  };
473
472
  var ComplexMap = class _ComplexMap {
474
473
  _keyProjection;
475
- _keys;
476
- _values;
474
+ _keys = /* @__PURE__ */ new Map();
475
+ _values = /* @__PURE__ */ new Map();
477
476
  // prettier-ignore
478
477
  constructor(_keyProjection, entries2) {
479
478
  this._keyProjection = _keyProjection;
480
- this._keys = /* @__PURE__ */ new Map();
481
- this._values = /* @__PURE__ */ new Map();
482
479
  if (entries2) {
483
480
  for (const [key, value] of entries2) {
484
481
  this.set(key, value);
@@ -599,10 +596,6 @@ var getDeep = (obj, path) => {
599
596
  // src/defer-function.ts
600
597
  var deferFunction = (fnProvider) => (...args) => fnProvider()(...args);
601
598
 
602
- // src/explicit-resource-management-polyfill.ts
603
- Symbol.dispose ??= Symbol("Symbol.dispose");
604
- Symbol.asyncDispose ??= Symbol("Symbol.asyncDispose");
605
-
606
599
  // src/defer.ts
607
600
  var defer = (fn) => new DeferGuard(fn);
608
601
  var DeferGuard = class {
@@ -664,33 +657,6 @@ var MapEntry = class {
664
657
  }
665
658
  };
666
659
 
667
- // src/first-two-chars.ts
668
- var renderableCharRegex = /^(?![\p{Control}\p{Mark}\p{Separator}\p{Surrogate}\p{Unassigned}\p{P}])[\p{L}\p{N}\p{S}\p{Emoji}]$/u;
669
- var getFirstTwoRenderableChars = (label) => {
670
- const characters = Array.from(label);
671
- const result = [
672
- "",
673
- ""
674
- ];
675
- let foundFirst = false;
676
- for (let i = 0; i < characters.length; i++) {
677
- const char = characters[i];
678
- if (renderableCharRegex.test(char)) {
679
- if (!foundFirst) {
680
- result[0] = char;
681
- foundFirst = true;
682
- } else {
683
- const textBetween = characters.slice(result[0].length, i).join("");
684
- if (/[^\p{L}\p{N}_]/u.test(textBetween)) {
685
- result[1] = char;
686
- break;
687
- }
688
- }
689
- }
690
- }
691
- return result;
692
- };
693
-
694
660
  // src/for-each-async.ts
695
661
  var forEachAsync = (items, fn) => Promise.all(items.map(fn));
696
662
 
@@ -1222,32 +1188,9 @@ var deepMapValues = (value, fn) => {
1222
1188
  };
1223
1189
  var DeepMapper = class {
1224
1190
  _fn;
1225
- _cyclic;
1191
+ _cyclic = /* @__PURE__ */ new Map();
1226
1192
  constructor(_fn) {
1227
1193
  this._fn = _fn;
1228
- this._cyclic = /* @__PURE__ */ new Map();
1229
- this._recurse = (value) => {
1230
- if (this._cyclic.has(value)) {
1231
- return this._cyclic.get(value);
1232
- }
1233
- if (Array.isArray(value)) {
1234
- const res = new Array(value.length);
1235
- this._cyclic.set(value, res);
1236
- for (let i = 0; i < value.length; i++) {
1237
- res[i] = this._map(value[i], i);
1238
- }
1239
- return res;
1240
- } else if (value !== null && typeof value === "object") {
1241
- const res = {};
1242
- this._cyclic.set(value, res);
1243
- for (const key in value) {
1244
- res[key] = this._map(value[key], key);
1245
- }
1246
- return res;
1247
- } else {
1248
- return value;
1249
- }
1250
- };
1251
1194
  }
1252
1195
  map(value) {
1253
1196
  return this._map(value, void 0);
@@ -1258,39 +1201,37 @@ var DeepMapper = class {
1258
1201
  }
1259
1202
  return this._fn(value, this._recurse, key);
1260
1203
  }
1261
- _recurse;
1204
+ _recurse = (value) => {
1205
+ if (this._cyclic.has(value)) {
1206
+ return this._cyclic.get(value);
1207
+ }
1208
+ if (Array.isArray(value)) {
1209
+ const res = new Array(value.length);
1210
+ this._cyclic.set(value, res);
1211
+ for (let i = 0; i < value.length; i++) {
1212
+ res[i] = this._map(value[i], i);
1213
+ }
1214
+ return res;
1215
+ } else if (value !== null && typeof value === "object") {
1216
+ const res = {};
1217
+ this._cyclic.set(value, res);
1218
+ for (const key in value) {
1219
+ res[key] = this._map(value[key], key);
1220
+ }
1221
+ return res;
1222
+ } else {
1223
+ return value;
1224
+ }
1225
+ };
1262
1226
  };
1263
1227
  var deepMapValuesAsync = (value, fn) => {
1264
1228
  return new DeepMapperAsync(fn).map(value);
1265
1229
  };
1266
1230
  var DeepMapperAsync = class {
1267
1231
  _fn;
1268
- _cyclic;
1232
+ _cyclic = /* @__PURE__ */ new Map();
1269
1233
  constructor(_fn) {
1270
1234
  this._fn = _fn;
1271
- this._cyclic = /* @__PURE__ */ new Map();
1272
- this._recurse = async (value) => {
1273
- if (this._cyclic.has(value)) {
1274
- return this._cyclic.get(value);
1275
- }
1276
- if (Array.isArray(value)) {
1277
- const res = new Array(value.length);
1278
- this._cyclic.set(value, res);
1279
- for (let i = 0; i < value.length; i++) {
1280
- res[i] = await this._map(value[i], i);
1281
- }
1282
- return res;
1283
- } else if (value !== null && typeof value === "object") {
1284
- const res = {};
1285
- this._cyclic.set(value, res);
1286
- for (const key in value) {
1287
- res[key] = await this._map(value[key], key);
1288
- }
1289
- return res;
1290
- } else {
1291
- return value;
1292
- }
1293
- };
1294
1235
  }
1295
1236
  map(value) {
1296
1237
  return this._map(value, void 0);
@@ -1301,7 +1242,28 @@ var DeepMapperAsync = class {
1301
1242
  }
1302
1243
  return this._fn(value, this._recurse, key);
1303
1244
  }
1304
- _recurse;
1245
+ _recurse = async (value) => {
1246
+ if (this._cyclic.has(value)) {
1247
+ return this._cyclic.get(value);
1248
+ }
1249
+ if (Array.isArray(value)) {
1250
+ const res = new Array(value.length);
1251
+ this._cyclic.set(value, res);
1252
+ for (let i = 0; i < value.length; i++) {
1253
+ res[i] = await this._map(value[i], i);
1254
+ }
1255
+ return res;
1256
+ } else if (value !== null && typeof value === "object") {
1257
+ const res = {};
1258
+ this._cyclic.set(value, res);
1259
+ for (const key in value) {
1260
+ res[key] = await this._map(value[key], key);
1261
+ }
1262
+ return res;
1263
+ } else {
1264
+ return value;
1265
+ }
1266
+ };
1305
1267
  };
1306
1268
  var visitValues = (object, visitor) => {
1307
1269
  if (Array.isArray(object)) {
@@ -1623,7 +1585,7 @@ var safeParseInt = (value, defaultValue) => {
1623
1585
  try {
1624
1586
  const n = parseInt(value ?? "");
1625
1587
  return isNaN(n) ? defaultValue : n;
1626
- } catch (err) {
1588
+ } catch {
1627
1589
  return defaultValue;
1628
1590
  }
1629
1591
  };
@@ -1638,12 +1600,79 @@ var safeParseJson = (data, defaultValue) => {
1638
1600
  if (data && data.length > 0) {
1639
1601
  try {
1640
1602
  return JSON.parse(data);
1641
- } catch (err) {
1603
+ } catch {
1642
1604
  }
1643
1605
  }
1644
1606
  return defaultValue;
1645
1607
  };
1646
1608
 
1609
+ // src/safe-stringify.ts
1610
+ var SKIP = Object.freeze({});
1611
+ function safeStringify(obj, filter = defaultFilter, indent = 2) {
1612
+ const seen = /* @__PURE__ */ new WeakSet();
1613
+ function replacer(key, value) {
1614
+ if (typeof value === "object" && value !== null) {
1615
+ if (seen.has(value)) {
1616
+ return "[Circular]";
1617
+ }
1618
+ seen.add(value);
1619
+ }
1620
+ if (filter) {
1621
+ const v2 = filter?.(key, value);
1622
+ if (v2 !== void 0) {
1623
+ return v2 === SKIP ? void 0 : v2;
1624
+ }
1625
+ }
1626
+ return value;
1627
+ }
1628
+ let result = "";
1629
+ try {
1630
+ result = JSON.stringify(obj, replacer, indent);
1631
+ } catch (error) {
1632
+ result = `Error: ${error.message}`;
1633
+ }
1634
+ return result;
1635
+ }
1636
+ var createReplacer = ({ omit: omit2, parse, maxDepth, maxArrayLen, maxStringLen } = {}) => {
1637
+ let currentDepth = 0;
1638
+ const depthMap = /* @__PURE__ */ new WeakMap();
1639
+ return function(key, value) {
1640
+ if (key === "") {
1641
+ currentDepth = 0;
1642
+ } else if (this && typeof this === "object") {
1643
+ const parentDepth = depthMap.get(this) ?? 0;
1644
+ currentDepth = parentDepth + 1;
1645
+ }
1646
+ if (typeof value === "function") {
1647
+ return void 0;
1648
+ }
1649
+ if (value && typeof value === "object") {
1650
+ depthMap.set(value, currentDepth);
1651
+ if (maxDepth != null && currentDepth >= maxDepth) {
1652
+ return Array.isArray(value) ? `[{ length: ${value.length} }]` : `{ keys: ${Object.keys(value).length} }`;
1653
+ }
1654
+ }
1655
+ if (omit2?.includes(key)) {
1656
+ return void 0;
1657
+ }
1658
+ if (parse?.includes(key) && typeof value === "string") {
1659
+ try {
1660
+ return JSON.parse(value);
1661
+ } catch {
1662
+ return value;
1663
+ }
1664
+ }
1665
+ if (maxArrayLen != null && Array.isArray(value) && value.length > maxArrayLen) {
1666
+ return `[length: ${value.length}]`;
1667
+ }
1668
+ if (maxStringLen != null && typeof value === "string" && value.length > maxStringLen) {
1669
+ return value.slice(0, maxStringLen) + "...";
1670
+ }
1671
+ return value;
1672
+ };
1673
+ };
1674
+ var defaultFilter = createReplacer();
1675
+
1647
1676
  // src/sliding-window-summary.ts
1648
1677
  import { invariant as invariant5 } from "@dxos/invariant";
1649
1678
  var __dxlog_file5 = "/__w/dxos/dxos/packages/common/util/src/sliding-window-summary.ts";
@@ -1748,6 +1777,7 @@ function trim(strings, ...values) {
1748
1777
  const minIndent = Math.min(...lines.filter((l) => l.trim()).map((l) => l.match(/^[ \t]*/)?.[0].length ?? 0));
1749
1778
  return lines.map((l) => l.slice(minIndent)).join("\n");
1750
1779
  }
1780
+ var kebabize = (str) => str.replace(/[A-Z]+(?![a-z])|[A-Z]/g, ($, ofs) => (ofs ? "-" : "") + $.toLowerCase());
1751
1781
 
1752
1782
  // src/sum.ts
1753
1783
  var sum = (values) => values.reduce((a, b) => a + b, 0);
@@ -2048,7 +2078,7 @@ var stringifyTree = (node, ancestors = [], rows = []) => {
2048
2078
  };
2049
2079
 
2050
2080
  // src/types.ts
2051
- var isNotFalsy = (value) => !!value;
2081
+ var isTruthy = (value) => !!value;
2052
2082
  var isNonNullable = (value) => value != null;
2053
2083
  var doAsync = async (fn) => fn();
2054
2084
  var getProviderValue = (provider, arg) => {
@@ -2079,6 +2109,103 @@ var arrayMove = (array, from, to) => {
2079
2109
  return array;
2080
2110
  };
2081
2111
 
2112
+ // src/unit.ts
2113
+ var createFormat = (unit) => {
2114
+ return (n, precision = unit.precision ?? 0) => {
2115
+ const value = n / unit.quotient;
2116
+ return {
2117
+ unit,
2118
+ value,
2119
+ formattedValue: value.toFixed(precision),
2120
+ toString: () => `${value.toFixed(precision)}${unit.symbol}`
2121
+ };
2122
+ };
2123
+ };
2124
+ var MS_SECONDS = 1e3;
2125
+ var MS_MINUTES = 60 * MS_SECONDS;
2126
+ var MS_HOURS = 60 * MS_MINUTES;
2127
+ var Unit = {
2128
+ // General.
2129
+ Percent: createFormat({
2130
+ symbol: "%",
2131
+ quotient: 1 / 100,
2132
+ precision: 2
2133
+ }),
2134
+ Thousand: createFormat({
2135
+ symbol: "k",
2136
+ quotient: 1e3,
2137
+ precision: 2
2138
+ }),
2139
+ // Bytes (note KB vs KiB).
2140
+ Gigabyte: createFormat({
2141
+ symbol: "GB",
2142
+ quotient: 1e3 * 1e3 * 1e3,
2143
+ precision: 2
2144
+ }),
2145
+ Megabyte: createFormat({
2146
+ symbol: "MB",
2147
+ quotient: 1e3 * 1e3,
2148
+ precision: 2
2149
+ }),
2150
+ Kilobyte: createFormat({
2151
+ symbol: "KB",
2152
+ quotient: 1e3,
2153
+ precision: 2
2154
+ }),
2155
+ // Time.
2156
+ Hour: createFormat({
2157
+ symbol: "h",
2158
+ quotient: MS_HOURS
2159
+ }),
2160
+ Minute: createFormat({
2161
+ symbol: "m",
2162
+ quotient: MS_MINUTES
2163
+ }),
2164
+ Second: createFormat({
2165
+ symbol: "s",
2166
+ quotient: MS_SECONDS,
2167
+ precision: 1
2168
+ }),
2169
+ Millisecond: createFormat({
2170
+ symbol: "ms",
2171
+ quotient: 1
2172
+ }),
2173
+ Duration: (n) => {
2174
+ const hours = Math.floor(n / MS_HOURS);
2175
+ const minutes = Math.floor(n % MS_HOURS / MS_MINUTES);
2176
+ if (hours) {
2177
+ const formattedValue = minutes ? `${hours}h ${minutes}m` : `${hours}h`;
2178
+ return {
2179
+ unit: {
2180
+ symbol: "h",
2181
+ quotient: MS_HOURS
2182
+ },
2183
+ value: hours,
2184
+ formattedValue,
2185
+ toString: () => formattedValue
2186
+ };
2187
+ }
2188
+ if (minutes) {
2189
+ const seconds2 = (n - MS_MINUTES * minutes) / MS_SECONDS;
2190
+ const formattedValue = seconds2 ? `${minutes}m ${seconds2}s` : `${minutes}m`;
2191
+ return {
2192
+ unit: {
2193
+ symbol: "m",
2194
+ quotient: MS_MINUTES
2195
+ },
2196
+ value: minutes,
2197
+ formattedValue,
2198
+ toString: () => formattedValue
2199
+ };
2200
+ }
2201
+ const seconds = n >= MS_SECONDS;
2202
+ if (seconds) {
2203
+ return Unit.Second(n);
2204
+ }
2205
+ return Unit.Millisecond(n);
2206
+ }
2207
+ };
2208
+
2082
2209
  // src/url.ts
2083
2210
  var createUrl = (url, search) => {
2084
2211
  const base = typeof url === "string" ? new URL(url) : url;
@@ -2191,8 +2318,10 @@ export {
2191
2318
  ComplexSet,
2192
2319
  HumanHasher,
2193
2320
  MapEntry,
2321
+ SKIP,
2194
2322
  SlidingWindowSummary,
2195
2323
  Tracer,
2324
+ Unit,
2196
2325
  WeakDictionary,
2197
2326
  accessBy,
2198
2327
  arrayMove,
@@ -2214,11 +2343,13 @@ export {
2214
2343
  createBinder,
2215
2344
  createBucketReducer,
2216
2345
  createGroupReducer,
2346
+ createReplacer,
2217
2347
  createSetDispatch,
2218
2348
  createUrl,
2219
2349
  decamelize,
2220
2350
  deepMapValues,
2221
2351
  deepMapValuesAsync,
2352
+ defaultFilter,
2222
2353
  defaultMap,
2223
2354
  defer,
2224
2355
  deferAsync,
@@ -2235,7 +2366,6 @@ export {
2235
2366
  getDate,
2236
2367
  getDebugName,
2237
2368
  getDeep,
2238
- getFirstTwoRenderableChars,
2239
2369
  getHostPlatform,
2240
2370
  getPrototypeSpecificInstanceId,
2241
2371
  getProviderValue,
@@ -2253,12 +2383,13 @@ export {
2253
2383
  iosCheck,
2254
2384
  isNode,
2255
2385
  isNonNullable,
2256
- isNotFalsy,
2386
+ isTruthy,
2257
2387
  joinTables,
2258
2388
  jsonKeyReplacer,
2259
2389
  jsonReplacer,
2260
2390
  jsonify,
2261
2391
  jsonlogify,
2392
+ kebabize,
2262
2393
  keyToEmoji,
2263
2394
  keyToFallback,
2264
2395
  keyToHue,
@@ -2289,6 +2420,7 @@ export {
2289
2420
  safeParseFloat,
2290
2421
  safeParseInt,
2291
2422
  safeParseJson,
2423
+ safeStringify,
2292
2424
  set,
2293
2425
  setDeep,
2294
2426
  sortKeys,