@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.
package/.rollup/index.cjs CHANGED
@@ -1673,16 +1673,44 @@ class LazyAsync {
1673
1673
  }
1674
1674
  }
1675
1675
 
1676
+ /**
1677
+ * A dictionary that lazily initializes values on first access.
1678
+ *
1679
+ * Values are generated by a user-provided factory function and cached for subsequent lookups.
1680
+ * Each key is initialized at most once — repeated calls to `getOrCreate` return the cached value
1681
+ * without invoking the generator again.
1682
+ *
1683
+ * @typeParam K - Key type (string, number, or symbol).
1684
+ * @typeParam T - The type of values produced by the generator.
1685
+ *
1686
+ * @example
1687
+ * ```ts
1688
+ * const dict = LazyDictionary.of( ( key: string ) => key.toUpperCase() );
1689
+ * dict.getOrCreate( 'hello' ); // → 'HELLO'
1690
+ * dict.getOrCreate( 'hello' ); // → 'HELLO' (cached, generator not called again)
1691
+ * dict.hasKey( 'hello' ); // → true
1692
+ * dict.getEntries(); // → [ [ 'hello', 'HELLO' ] ]
1693
+ * ```
1694
+ */
1676
1695
  class LazyDictionary {
1677
1696
  _generator;
1678
1697
  _dictionary = {};
1679
1698
  constructor(_generator) {
1680
1699
  this._generator = _generator;
1681
1700
  }
1701
+ /**
1702
+ * Creates a new {@link LazyDictionary} instance.
1703
+ * @param generator - Factory function that produces a value for a given key.
1704
+ */
1682
1705
  static of(generator) {
1683
1706
  return new LazyDictionary(generator);
1684
1707
  }
1685
- getOrCreate(key) {
1708
+ /**
1709
+ * Returns the cached value for `key`, generating and caching it if this is the first access.
1710
+ * The generator is invoked at most once per key.
1711
+ * @param key - The key to look up or generate.
1712
+ * @returns The value associated with `key`.
1713
+ */ getOrCreate(key) {
1686
1714
  if (key in this._dictionary) {
1687
1715
  return this._dictionary[key];
1688
1716
  }
@@ -1690,12 +1718,35 @@ class LazyDictionary {
1690
1718
  this._dictionary[key] = value;
1691
1719
  return value;
1692
1720
  }
1721
+ /**
1722
+ * Returns the cached value for `key`, or throws if the key has not been generated yet.
1723
+ * Unlike {@link getOrCreate}, this method does **not** invoke the generator.
1724
+ * @param key - The key to look up.
1725
+ * @param errorMessage - Optional message for the thrown Error.
1726
+ * @throws {Error} If the key has not been generated yet.
1727
+ * @returns The value associated with `key`.
1728
+ */
1693
1729
  getOrThrow(key, errorMessage) {
1694
1730
  if (key in this._dictionary) {
1695
1731
  return this._dictionary[key];
1696
1732
  }
1697
1733
  throw new Error(errorMessage);
1698
1734
  }
1735
+ /**
1736
+ * Returns whether a value has been generated for the given key.
1737
+ * @param key - The key to check.
1738
+ */
1739
+ hasKey(key) {
1740
+ return key in this._dictionary;
1741
+ }
1742
+ /**
1743
+ * Returns all key-value pairs that have been generated so far.
1744
+ * Does not invoke the generator for any key.
1745
+ * @returns An array of `[key, value]` tuples.
1746
+ */
1747
+ getEntries() {
1748
+ return Object.entries(this._dictionary);
1749
+ }
1699
1750
  }
1700
1751
 
1701
1752
  class TimeUnit {