nhb-toolbox 4.0.88 → 4.0.89

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.
@@ -7,19 +7,19 @@ exports.Finder = void 0;
7
7
  */
8
8
  class Finder {
9
9
  static DEFAULT_TTL = 1000 * 60 * 5;
10
- #cache = new Map();
10
+ #cachedResult = new Map();
11
11
  #sortedCache = new Map();
12
12
  #ttl;
13
13
  #items;
14
14
  /**
15
15
  * * Creates a new `Finder` instance.
16
16
  *
17
+ * @param data The initial array of items or a callback returning them.
17
18
  * @param ttl Time-to-live (in milliseconds) for cached search results. Defaults to {@link Finder.DEFAULT_TTL 5 Minutes}.
18
- * @param items The initial array of items to be used for searching.
19
19
  */
20
- constructor(ttl = Finder.DEFAULT_TTL, items) {
20
+ constructor(data, ttl = Finder.DEFAULT_TTL) {
21
21
  this.#ttl = ttl;
22
- this.#items = items;
22
+ this.#items = typeof data === 'function' ? data() : data;
23
23
  }
24
24
  /**
25
25
  * @instance Clears cache globally or for a specific key.
@@ -27,10 +27,10 @@ class Finder {
27
27
  */
28
28
  clearCache(key) {
29
29
  if (key) {
30
- this.#cache.delete(key);
30
+ this.#cachedResult.delete(key);
31
31
  }
32
32
  else {
33
- this.#cache.clear();
33
+ this.#cachedResult.clear();
34
34
  }
35
35
  }
36
36
  /**
@@ -50,12 +50,12 @@ class Finder {
50
50
  matcher.toLowerCase()
51
51
  : matcher;
52
52
  if (cacheKey) {
53
- const entry = this.#cache.get(cacheKey);
53
+ const entry = this.#cachedResult.get(cacheKey);
54
54
  if (entry && Date.now() - entry.timestamp < this.#ttl) {
55
55
  return entry.result;
56
56
  }
57
57
  else {
58
- this.#cache.delete(cacheKey);
58
+ this.#cachedResult.delete(cacheKey);
59
59
  }
60
60
  }
61
61
  let results = [];
@@ -97,7 +97,7 @@ class Finder {
97
97
  });
98
98
  }
99
99
  if (cacheKey) {
100
- this.#cache.set(cacheKey, {
100
+ this.#cachedResult.set(cacheKey, {
101
101
  result: results,
102
102
  timestamp: Date.now(),
103
103
  });
@@ -121,12 +121,12 @@ class Finder {
121
121
  matcher.toLowerCase()
122
122
  : matcher;
123
123
  if (cacheKey) {
124
- const entry = this.#cache.get(cacheKey);
124
+ const entry = this.#cachedResult.get(cacheKey);
125
125
  if (entry && Date.now() - entry.timestamp < this.#ttl) {
126
126
  return entry.result[0];
127
127
  }
128
128
  else {
129
- this.#cache.delete(cacheKey);
129
+ this.#cachedResult.delete(cacheKey);
130
130
  }
131
131
  }
132
132
  let result;
@@ -148,7 +148,7 @@ class Finder {
148
148
  return this.fuzzySearch(source, normalizedMatcher, getKey, caseInsensitive);
149
149
  }
150
150
  if (cacheKey && result) {
151
- this.#cache.set(cacheKey, {
151
+ this.#cachedResult.set(cacheKey, {
152
152
  result: [result],
153
153
  timestamp: Date.now(),
154
154
  });
@@ -1,5 +1,8 @@
1
1
  import type { FindOptions } from './types';
2
- type KeySelector<T> = keyof T | ((item: T) => string | number);
2
+ type OwnKeys<T> = {
3
+ [K in keyof T]: {} extends Pick<T, K> ? never : K;
4
+ }[keyof T];
5
+ type KeySelector<T> = Extract<OwnKeys<T>, string | number> | ((item: T) => string | number);
3
6
  /**
4
7
  * The `Finder` class performs optimized searching on arrays.
5
8
  * It supports binary search, fuzzy search, and smart caching with TTL.
@@ -8,12 +11,19 @@ export declare class Finder<T> {
8
11
  #private;
9
12
  static readonly DEFAULT_TTL: number;
10
13
  /**
11
- * * Creates a new `Finder` instance.
14
+ * * Creates a new `Finder` instance with a static array of items.
12
15
  *
16
+ * @param data An array of items to initialize the search dataset.
17
+ * @param ttl Optional time-to-live (in milliseconds) for cached search results. Defaults to {@link Finder.DEFAULT_TTL 5 Minutes}.
18
+ */
19
+ constructor(data: T[], ttl?: number);
20
+ /**
21
+ * * Creates a new `Finder` instance with a lazy-evaluated item provider.
22
+ *
23
+ * @param cb A function returning an array of items to initialize the search dataset.
13
24
  * @param ttl Time-to-live (in milliseconds) for cached search results. Defaults to {@link Finder.DEFAULT_TTL 5 Minutes}.
14
- * @param items The initial array of items to be used for searching.
15
25
  */
16
- constructor(ttl: number | undefined, items: T[]);
26
+ constructor(cb: () => T[], ttl?: number);
17
27
  /**
18
28
  * @instance Clears cache globally or for a specific key.
19
29
  * @param key Optional key to clear only a specific cache entry.
@@ -1 +1 @@
1
- {"version":3,"file":"Finder.d.ts","sourceRoot":"","sources":["../../../src/array/Finder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C,KAAK,WAAW,CAAC,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,MAAM,GAAG,MAAM,CAAC,CAAC;AAG/D;;;GAGG;AACH,qBAAa,MAAM,CAAC,CAAC;;IACpB,MAAM,CAAC,QAAQ,CAAC,WAAW,SAAiB;IAO5C;;;;;OAKG;gBACS,GAAG,EAAE,MAAM,YAAqB,EAAE,KAAK,EAAE,CAAC,EAAE;IAKxD;;;OAGG;IACH,UAAU,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI;IAQ9B;;;;;OAKG;IACH,OAAO,CACN,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,EAC3B,OAAO,CAAC,EAAE,WAAW,CAAC,CAAC,CAAC,GACtB,CAAC,EAAE;IAyGN;;;;;OAKG;IACH,OAAO,CACN,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,EAC3B,OAAO,CAAC,EAAE,WAAW,CAAC,CAAC,CAAC,GACtB,CAAC,GAAG,SAAS;IA8EhB;;;;;;OAMG;IACG,YAAY,CACjB,QAAQ,EAAE,MAAM,OAAO,CAAC,CAAC,EAAE,CAAC,EAC5B,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,EAC3B,OAAO,CAAC,EAA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AAE,OAAO,CAAC,GACrC,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAMzB;;;;;;;;OAQG;IACH,YAAY,CACX,MAAM,EAAE,CAAC,EAAE,EACX,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,WAAW,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,MAAM,GAAG,MAAM,EACzC,eAAe,EAAE,OAAO,GACtB,CAAC,GAAG,SAAS;IAoBhB;;;;;;;;OAQG;IACH,WAAW,CACV,KAAK,EAAE,CAAC,EAAE,EACV,OAAO,EAAE,MAAM,EACf,WAAW,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,MAAM,GAAG,MAAM,EACzC,eAAe,EAAE,OAAO,GACtB,CAAC,GAAG,SAAS;CAyFhB"}
@@ -4,19 +4,19 @@
4
4
  */
5
5
  export class Finder {
6
6
  static DEFAULT_TTL = 1000 * 60 * 5;
7
- #cache = new Map();
7
+ #cachedResult = new Map();
8
8
  #sortedCache = new Map();
9
9
  #ttl;
10
10
  #items;
11
11
  /**
12
12
  * * Creates a new `Finder` instance.
13
13
  *
14
+ * @param data The initial array of items or a callback returning them.
14
15
  * @param ttl Time-to-live (in milliseconds) for cached search results. Defaults to {@link Finder.DEFAULT_TTL 5 Minutes}.
15
- * @param items The initial array of items to be used for searching.
16
16
  */
17
- constructor(ttl = Finder.DEFAULT_TTL, items) {
17
+ constructor(data, ttl = Finder.DEFAULT_TTL) {
18
18
  this.#ttl = ttl;
19
- this.#items = items;
19
+ this.#items = typeof data === 'function' ? data() : data;
20
20
  }
21
21
  /**
22
22
  * @instance Clears cache globally or for a specific key.
@@ -24,10 +24,10 @@ export class Finder {
24
24
  */
25
25
  clearCache(key) {
26
26
  if (key) {
27
- this.#cache.delete(key);
27
+ this.#cachedResult.delete(key);
28
28
  }
29
29
  else {
30
- this.#cache.clear();
30
+ this.#cachedResult.clear();
31
31
  }
32
32
  }
33
33
  /**
@@ -47,12 +47,12 @@ export class Finder {
47
47
  matcher.toLowerCase()
48
48
  : matcher;
49
49
  if (cacheKey) {
50
- const entry = this.#cache.get(cacheKey);
50
+ const entry = this.#cachedResult.get(cacheKey);
51
51
  if (entry && Date.now() - entry.timestamp < this.#ttl) {
52
52
  return entry.result;
53
53
  }
54
54
  else {
55
- this.#cache.delete(cacheKey);
55
+ this.#cachedResult.delete(cacheKey);
56
56
  }
57
57
  }
58
58
  let results = [];
@@ -94,7 +94,7 @@ export class Finder {
94
94
  });
95
95
  }
96
96
  if (cacheKey) {
97
- this.#cache.set(cacheKey, {
97
+ this.#cachedResult.set(cacheKey, {
98
98
  result: results,
99
99
  timestamp: Date.now(),
100
100
  });
@@ -118,12 +118,12 @@ export class Finder {
118
118
  matcher.toLowerCase()
119
119
  : matcher;
120
120
  if (cacheKey) {
121
- const entry = this.#cache.get(cacheKey);
121
+ const entry = this.#cachedResult.get(cacheKey);
122
122
  if (entry && Date.now() - entry.timestamp < this.#ttl) {
123
123
  return entry.result[0];
124
124
  }
125
125
  else {
126
- this.#cache.delete(cacheKey);
126
+ this.#cachedResult.delete(cacheKey);
127
127
  }
128
128
  }
129
129
  let result;
@@ -145,7 +145,7 @@ export class Finder {
145
145
  return this.fuzzySearch(source, normalizedMatcher, getKey, caseInsensitive);
146
146
  }
147
147
  if (cacheKey && result) {
148
- this.#cache.set(cacheKey, {
148
+ this.#cachedResult.set(cacheKey, {
149
149
  result: [result],
150
150
  timestamp: Date.now(),
151
151
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhb-toolbox",
3
- "version": "4.0.88",
3
+ "version": "4.0.89",
4
4
  "description": "A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",