@zelgadis87/utils-core 5.4.7 → 5.5.0-beta.1

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.
@@ -1431,13 +1431,61 @@ declare class LazyAsync<T> {
1431
1431
  static of<T>(generator: () => Promise<T>): LazyAsync<T>;
1432
1432
  }
1433
1433
 
1434
+ /**
1435
+ * A dictionary that lazily initializes values on first access.
1436
+ *
1437
+ * Values are generated by a user-provided factory function and cached for subsequent lookups.
1438
+ * Each key is initialized at most once — repeated calls to `getOrCreate` return the cached value
1439
+ * without invoking the generator again.
1440
+ *
1441
+ * @typeParam K - Key type (string, number, or symbol).
1442
+ * @typeParam T - The type of values produced by the generator.
1443
+ *
1444
+ * @example
1445
+ * ```ts
1446
+ * const dict = LazyDictionary.of( ( key: string ) => key.toUpperCase() );
1447
+ * dict.getOrCreate( 'hello' ); // → 'HELLO'
1448
+ * dict.getOrCreate( 'hello' ); // → 'HELLO' (cached, generator not called again)
1449
+ * dict.hasKey( 'hello' ); // → true
1450
+ * dict.getEntries(); // → [ [ 'hello', 'HELLO' ] ]
1451
+ * ```
1452
+ */
1434
1453
  declare class LazyDictionary<K extends string | number | symbol, T> {
1435
1454
  private readonly _generator;
1436
- private readonly _dictionary;
1455
+ private readonly _map;
1437
1456
  private constructor();
1457
+ /**
1458
+ * Creates a new {@link LazyDictionary} instance.
1459
+ * @param generator - Factory function that produces a value for a given key.
1460
+ */
1438
1461
  static of<K extends string | number | symbol, T>(generator: TFunction<K, T>): LazyDictionary<K, T>;
1462
+ /**
1463
+ * Returns the cached value for `key`, generating and caching it if this is the first access.
1464
+ * The generator is invoked at most once per key.
1465
+ * @param key - The key to look up or generate.
1466
+ * @returns The value associated with `key`.
1467
+ */
1439
1468
  getOrCreate(key: K): T;
1469
+ /**
1470
+ * Returns the cached value for `key`, or throws if the key has not been generated yet.
1471
+ * Unlike {@link getOrCreate}, this method does **not** invoke the generator.
1472
+ * @param key - The key to look up.
1473
+ * @param errorMessage - Optional message for the thrown Error.
1474
+ * @throws {Error} If the key has not been generated yet.
1475
+ * @returns The value associated with `key`.
1476
+ */
1440
1477
  getOrThrow(key: K, errorMessage?: "Value not initialized"): T;
1478
+ /**
1479
+ * Returns whether a value has been generated for the given key.
1480
+ * @param key - The key to check.
1481
+ */
1482
+ hasKey(key: K): boolean;
1483
+ /**
1484
+ * Returns all key-value pairs that have been generated so far.
1485
+ * Does not invoke the generator for any key.
1486
+ * @returns An array of `[key, value]` tuples.
1487
+ */
1488
+ getEntries(): [K, T][];
1441
1489
  }
1442
1490
 
1443
1491
  /**
package/.rollup/index.mjs CHANGED
@@ -1671,29 +1671,81 @@ class LazyAsync {
1671
1671
  }
1672
1672
  }
1673
1673
 
1674
+ /**
1675
+ * A dictionary that lazily initializes values on first access.
1676
+ *
1677
+ * Values are generated by a user-provided factory function and cached for subsequent lookups.
1678
+ * Each key is initialized at most once — repeated calls to `getOrCreate` return the cached value
1679
+ * without invoking the generator again.
1680
+ *
1681
+ * @typeParam K - Key type (string, number, or symbol).
1682
+ * @typeParam T - The type of values produced by the generator.
1683
+ *
1684
+ * @example
1685
+ * ```ts
1686
+ * const dict = LazyDictionary.of( ( key: string ) => key.toUpperCase() );
1687
+ * dict.getOrCreate( 'hello' ); // → 'HELLO'
1688
+ * dict.getOrCreate( 'hello' ); // → 'HELLO' (cached, generator not called again)
1689
+ * dict.hasKey( 'hello' ); // → true
1690
+ * dict.getEntries(); // → [ [ 'hello', 'HELLO' ] ]
1691
+ * ```
1692
+ */
1674
1693
  class LazyDictionary {
1675
1694
  _generator;
1676
- _dictionary = {};
1695
+ _map = new Map();
1677
1696
  constructor(_generator) {
1678
1697
  this._generator = _generator;
1679
1698
  }
1699
+ /**
1700
+ * Creates a new {@link LazyDictionary} instance.
1701
+ * @param generator - Factory function that produces a value for a given key.
1702
+ */
1680
1703
  static of(generator) {
1681
1704
  return new LazyDictionary(generator);
1682
1705
  }
1706
+ /**
1707
+ * Returns the cached value for `key`, generating and caching it if this is the first access.
1708
+ * The generator is invoked at most once per key.
1709
+ * @param key - The key to look up or generate.
1710
+ * @returns The value associated with `key`.
1711
+ */
1683
1712
  getOrCreate(key) {
1684
- if (key in this._dictionary) {
1685
- return this._dictionary[key];
1713
+ if (!this._map.has(key)) {
1714
+ const value = this._generator(key);
1715
+ this._map.set(key, value);
1716
+ return value;
1686
1717
  }
1687
- const value = this._generator(key);
1688
- this._dictionary[key] = value;
1689
- return value;
1718
+ return this._map.get(key);
1690
1719
  }
1720
+ /**
1721
+ * Returns the cached value for `key`, or throws if the key has not been generated yet.
1722
+ * Unlike {@link getOrCreate}, this method does **not** invoke the generator.
1723
+ * @param key - The key to look up.
1724
+ * @param errorMessage - Optional message for the thrown Error.
1725
+ * @throws {Error} If the key has not been generated yet.
1726
+ * @returns The value associated with `key`.
1727
+ */
1691
1728
  getOrThrow(key, errorMessage) {
1692
- if (key in this._dictionary) {
1693
- return this._dictionary[key];
1729
+ if (this._map.has(key)) {
1730
+ return this._map.get(key);
1694
1731
  }
1695
1732
  throw new Error(errorMessage);
1696
1733
  }
1734
+ /**
1735
+ * Returns whether a value has been generated for the given key.
1736
+ * @param key - The key to check.
1737
+ */
1738
+ hasKey(key) {
1739
+ return this._map.has(key);
1740
+ }
1741
+ /**
1742
+ * Returns all key-value pairs that have been generated so far.
1743
+ * Does not invoke the generator for any key.
1744
+ * @returns An array of `[key, value]` tuples.
1745
+ */
1746
+ getEntries() {
1747
+ return [...this._map.entries()];
1748
+ }
1697
1749
  }
1698
1750
 
1699
1751
  class TimeUnit {