@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.
package/.rollup/index.cjs CHANGED
@@ -1673,29 +1673,81 @@ 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
- _dictionary = {};
1697
+ _map = new Map();
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
  }
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
+ */
1685
1714
  getOrCreate(key) {
1686
- if (key in this._dictionary) {
1687
- return this._dictionary[key];
1715
+ if (!this._map.has(key)) {
1716
+ const value = this._generator(key);
1717
+ this._map.set(key, value);
1718
+ return value;
1688
1719
  }
1689
- const value = this._generator(key);
1690
- this._dictionary[key] = value;
1691
- return value;
1720
+ return this._map.get(key);
1692
1721
  }
1722
+ /**
1723
+ * Returns the cached value for `key`, or throws if the key has not been generated yet.
1724
+ * Unlike {@link getOrCreate}, this method does **not** invoke the generator.
1725
+ * @param key - The key to look up.
1726
+ * @param errorMessage - Optional message for the thrown Error.
1727
+ * @throws {Error} If the key has not been generated yet.
1728
+ * @returns The value associated with `key`.
1729
+ */
1693
1730
  getOrThrow(key, errorMessage) {
1694
- if (key in this._dictionary) {
1695
- return this._dictionary[key];
1731
+ if (this._map.has(key)) {
1732
+ return this._map.get(key);
1696
1733
  }
1697
1734
  throw new Error(errorMessage);
1698
1735
  }
1736
+ /**
1737
+ * Returns whether a value has been generated for the given key.
1738
+ * @param key - The key to check.
1739
+ */
1740
+ hasKey(key) {
1741
+ return this._map.has(key);
1742
+ }
1743
+ /**
1744
+ * Returns all key-value pairs that have been generated so far.
1745
+ * Does not invoke the generator for any key.
1746
+ * @returns An array of `[key, value]` tuples.
1747
+ */
1748
+ getEntries() {
1749
+ return [...this._map.entries()];
1750
+ }
1699
1751
  }
1700
1752
 
1701
1753
  class TimeUnit {