@asamuzakjp/dom-selector 7.0.7 → 7.0.9

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/package.json CHANGED
@@ -43,7 +43,7 @@
43
43
  "eslint-plugin-regexp": "^3.1.0",
44
44
  "eslint-plugin-unicorn": "^64.0.0",
45
45
  "globals": "^17.4.0",
46
- "jsdom": "^29.0.1",
46
+ "jsdom": "^29.0.2",
47
47
  "mocha": "^11.7.5",
48
48
  "neostandard": "^0.13.0",
49
49
  "prettier": "^3.8.1",
@@ -71,5 +71,5 @@
71
71
  "engines": {
72
72
  "node": "^20.19.0 || ^22.12.0 || >=24.0.0"
73
73
  },
74
- "version": "7.0.7"
74
+ "version": "7.0.9"
75
75
  }
package/src/index.js CHANGED
@@ -6,6 +6,7 @@
6
6
  */
7
7
 
8
8
  /* import */
9
+ import { GenerationalCache } from './js/cache.js';
9
10
  import { Finder } from './js/finder.js';
10
11
  import { filterSelector, getType, initNwsapi } from './js/utility.js';
11
12
 
@@ -21,50 +22,6 @@ import {
21
22
  } from './js/constant.js';
22
23
  const MAX_CACHE = 1024;
23
24
 
24
- /* Generational Cache */
25
- export class GenerationalCache {
26
- constructor(max) {
27
- this.max = Math.ceil(max / 2);
28
- this.current = new Map();
29
- this.old = new Map();
30
- }
31
-
32
- get(key) {
33
- let value = this.current.get(key);
34
- if (value !== undefined) {
35
- return value;
36
- }
37
- value = this.old.get(key);
38
- if (value !== undefined) {
39
- this.set(key, value);
40
- return value;
41
- }
42
- return undefined;
43
- }
44
-
45
- set(key, value) {
46
- this.current.set(key, value);
47
- if (this.current.size >= this.max) {
48
- this.old = this.current;
49
- this.current = new Map();
50
- }
51
- }
52
-
53
- has(key) {
54
- return this.current.has(key) || this.old.has(key);
55
- }
56
-
57
- delete(key) {
58
- this.current.delete(key);
59
- this.old.delete(key);
60
- }
61
-
62
- clear() {
63
- this.current.clear();
64
- this.old.clear();
65
- }
66
- }
67
-
68
25
  /**
69
26
  * @typedef {object} CheckResult
70
27
  * @property {boolean} match - The match result.
@@ -104,7 +61,6 @@ export class DOMSelector {
104
61
  */
105
62
  clear = () => {
106
63
  this.#finder.clearResults(true);
107
- // this.#cache.clear();
108
64
  };
109
65
 
110
66
  /**
@@ -130,10 +86,8 @@ export class DOMSelector {
130
86
  node.parentNode
131
87
  ) {
132
88
  const cacheKey = `check_${selector}`;
133
- let filterMatches = false;
134
- if (this.#cache.has(cacheKey)) {
135
- filterMatches = this.#cache.get(cacheKey);
136
- } else {
89
+ let filterMatches = this.#cache.get(cacheKey);
90
+ if (filterMatches === undefined) {
137
91
  filterMatches = filterSelector(selector, TARGET_SELF);
138
92
  this.#cache.set(cacheKey, filterMatches);
139
93
  }
@@ -144,9 +98,8 @@ export class DOMSelector {
144
98
  let ast = null;
145
99
  if (match) {
146
100
  const astCacheKey = `check_ast_${selector}`;
147
- if (this.#cache.has(astCacheKey)) {
148
- ast = this.#cache.get(astCacheKey);
149
- } else {
101
+ ast = this.#cache.get(astCacheKey);
102
+ if (ast === undefined) {
150
103
  ast = this.#finder.getAST(selector);
151
104
  this.#cache.set(astCacheKey, ast);
152
105
  }
@@ -193,10 +146,8 @@ export class DOMSelector {
193
146
  node.parentNode
194
147
  ) {
195
148
  const cacheKey = `matches_${selector}`;
196
- let filterMatches = false;
197
- if (this.#cache.has(cacheKey)) {
198
- filterMatches = this.#cache.get(cacheKey);
199
- } else {
149
+ let filterMatches = this.#cache.get(cacheKey);
150
+ if (filterMatches === undefined) {
200
151
  filterMatches = filterSelector(selector, TARGET_SELF);
201
152
  this.#cache.set(cacheKey, filterMatches);
202
153
  }
@@ -245,10 +196,8 @@ export class DOMSelector {
245
196
  node.parentNode
246
197
  ) {
247
198
  const cacheKey = `closest_${selector}`;
248
- let filterMatches = false;
249
- if (this.#cache.has(cacheKey)) {
250
- filterMatches = this.#cache.get(cacheKey);
251
- } else {
199
+ let filterMatches = this.#cache.get(cacheKey);
200
+ if (filterMatches === undefined) {
252
201
  filterMatches = filterSelector(selector, TARGET_LINEAL);
253
202
  this.#cache.set(cacheKey, filterMatches);
254
203
  }
@@ -304,10 +253,8 @@ export class DOMSelector {
304
253
  (node.nodeType !== DOCUMENT_FRAGMENT_NODE || !node.host)
305
254
  ) {
306
255
  const cacheKey = `querySelector_${selector}`;
307
- let filterMatches = false;
308
- if (this.#cache.has(cacheKey)) {
309
- filterMatches = this.#cache.get(cacheKey);
310
- } else {
256
+ let filterMatches = this.#cache.get(cacheKey);
257
+ if (filterMatches === undefined) {
311
258
  filterMatches = filterSelector(selector, TARGET_FIRST);
312
259
  this.#cache.set(cacheKey, filterMatches);
313
260
  }
@@ -357,10 +304,8 @@ export class DOMSelector {
357
304
  (node.nodeType !== DOCUMENT_FRAGMENT_NODE || !node.host)
358
305
  ) {
359
306
  const cacheKey = `querySelectorAll_${selector}`;
360
- let filterMatches = false;
361
- if (this.#cache.has(cacheKey)) {
362
- filterMatches = this.#cache.get(cacheKey);
363
- } else {
307
+ let filterMatches = this.#cache.get(cacheKey);
308
+ if (filterMatches === undefined) {
364
309
  filterMatches = filterSelector(selector, TARGET_ALL);
365
310
  this.#cache.set(cacheKey, filterMatches);
366
311
  }
@@ -0,0 +1,77 @@
1
+ /**
2
+ * cache.js
3
+ */
4
+
5
+ /* Generational Cache */
6
+ export class GenerationalCache {
7
+ #max;
8
+ #boundary;
9
+ #current;
10
+ #old;
11
+
12
+ /**
13
+ * constructor
14
+ * @param {number} max - max cache size
15
+ */
16
+ constructor(max) {
17
+ this.#current = new Map();
18
+ this.#old = new Map();
19
+ this.max = max;
20
+ }
21
+
22
+ get size() {
23
+ return this.#current.size + this.#old.size;
24
+ }
25
+
26
+ /**
27
+ * @returns {number} max cache size
28
+ */
29
+ get max() {
30
+ return this.#max;
31
+ }
32
+
33
+ set max(value) {
34
+ if (Number.isFinite(value) && value > 4) {
35
+ this.#max = value;
36
+ this.#boundary = Math.ceil(value / 2);
37
+ } else {
38
+ this.#max = 4;
39
+ this.#boundary = 2;
40
+ }
41
+ this.clear();
42
+ }
43
+
44
+ get(key) {
45
+ if (this.#current.has(key)) {
46
+ return this.#current.get(key);
47
+ }
48
+ const value = this.#old.get(key);
49
+ if (value !== undefined) {
50
+ this.set(key, value);
51
+ return value;
52
+ }
53
+ return undefined;
54
+ }
55
+
56
+ set(key, value) {
57
+ this.#current.set(key, value);
58
+ if (this.#current.size >= this.#boundary) {
59
+ this.#old = this.#current;
60
+ this.#current = new Map();
61
+ }
62
+ }
63
+
64
+ has(key) {
65
+ return this.#current.has(key) || this.#old.has(key);
66
+ }
67
+
68
+ delete(key) {
69
+ this.#current.delete(key);
70
+ this.#old.delete(key);
71
+ }
72
+
73
+ clear() {
74
+ this.#current.clear();
75
+ this.#old.clear();
76
+ }
77
+ }
package/types/index.d.ts CHANGED
@@ -1,14 +1,3 @@
1
- export class GenerationalCache {
2
- constructor(max: any);
3
- max: number;
4
- current: Map<any, any>;
5
- old: Map<any, any>;
6
- get(key: any): any;
7
- set(key: any, value: any): void;
8
- has(key: any): boolean;
9
- delete(key: any): void;
10
- clear(): void;
11
- }
12
1
  export class DOMSelector {
13
2
  constructor(window: Window, document: Document, opt?: object);
14
3
  clear: () => void;
@@ -0,0 +1,12 @@
1
+ export class GenerationalCache {
2
+ constructor(max: number);
3
+ set max(value: number);
4
+ get max(): number;
5
+ get size(): number;
6
+ get(key: any): any;
7
+ set(key: any, value: any): void;
8
+ has(key: any): boolean;
9
+ delete(key: any): void;
10
+ clear(): void;
11
+ #private;
12
+ }