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

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,60 @@ 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
1455
  private readonly _dictionary;
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>;
1439
- getOrCreate(key: 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
+ */ getOrCreate(key: K): T;
1468
+ /**
1469
+ * Returns the cached value for `key`, or throws if the key has not been generated yet.
1470
+ * Unlike {@link getOrCreate}, this method does **not** invoke the generator.
1471
+ * @param key - The key to look up.
1472
+ * @param errorMessage - Optional message for the thrown Error.
1473
+ * @throws {Error} If the key has not been generated yet.
1474
+ * @returns The value associated with `key`.
1475
+ */
1440
1476
  getOrThrow(key: K, errorMessage?: "Value not initialized"): T;
1477
+ /**
1478
+ * Returns whether a value has been generated for the given key.
1479
+ * @param key - The key to check.
1480
+ */
1481
+ hasKey(key: K): boolean;
1482
+ /**
1483
+ * Returns all key-value pairs that have been generated so far.
1484
+ * Does not invoke the generator for any key.
1485
+ * @returns An array of `[key, value]` tuples.
1486
+ */
1487
+ getEntries(): [K, T][];
1441
1488
  }
1442
1489
 
1443
1490
  /**
package/.rollup/index.mjs CHANGED
@@ -1671,16 +1671,44 @@ 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
1695
  _dictionary = {};
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
  }
1683
- getOrCreate(key) {
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
+ */ getOrCreate(key) {
1684
1712
  if (key in this._dictionary) {
1685
1713
  return this._dictionary[key];
1686
1714
  }
@@ -1688,12 +1716,35 @@ class LazyDictionary {
1688
1716
  this._dictionary[key] = value;
1689
1717
  return value;
1690
1718
  }
1719
+ /**
1720
+ * Returns the cached value for `key`, or throws if the key has not been generated yet.
1721
+ * Unlike {@link getOrCreate}, this method does **not** invoke the generator.
1722
+ * @param key - The key to look up.
1723
+ * @param errorMessage - Optional message for the thrown Error.
1724
+ * @throws {Error} If the key has not been generated yet.
1725
+ * @returns The value associated with `key`.
1726
+ */
1691
1727
  getOrThrow(key, errorMessage) {
1692
1728
  if (key in this._dictionary) {
1693
1729
  return this._dictionary[key];
1694
1730
  }
1695
1731
  throw new Error(errorMessage);
1696
1732
  }
1733
+ /**
1734
+ * Returns whether a value has been generated for the given key.
1735
+ * @param key - The key to check.
1736
+ */
1737
+ hasKey(key) {
1738
+ return key in this._dictionary;
1739
+ }
1740
+ /**
1741
+ * Returns all key-value pairs that have been generated so far.
1742
+ * Does not invoke the generator for any key.
1743
+ * @returns An array of `[key, value]` tuples.
1744
+ */
1745
+ getEntries() {
1746
+ return Object.entries(this._dictionary);
1747
+ }
1697
1748
  }
1698
1749
 
1699
1750
  class TimeUnit {