@tb-dev/prototype-dom 6.1.0 → 6.1.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.
Files changed (3) hide show
  1. package/dist/index.d.ts +14 -23
  2. package/dist/index.js +195 -256
  3. package/package.json +4 -4
package/dist/index.d.ts CHANGED
@@ -1,37 +1,28 @@
1
1
  declare global {
2
- interface ArrayConstructor {
3
- fromElements: <T extends Element[] | NodeList | string, V>(source: T, valueSelector: (element: Element) => V) => V[];
4
- }
5
2
  interface Document {
6
- queryStrict: <T extends Element>(selector: string) => T;
7
- queryAsArray: <T = Element>(selector: string, valueSelector?: (element: Element) => T) => T[];
8
- queryAsSet: <T = Element>(selector: string, valueSelector?: (element: Element) => T) => Set<T>;
9
- queryAsMap: <T extends Element, K, V = T>(selector: string, keySelector: (element: T) => K, valueSelector?: (element: T) => V) => Map<K, V>;
3
+ queryStrict: <T extends Element = Element>(selectors: string) => T;
4
+ queryAsArray: <T = Element>(selector: string, valueFn?: (element: Element) => T) => T[];
5
+ queryAsSet: <T = Element>(selector: string, valueFn?: (element: Element) => T) => Set<T>;
6
+ queryAsMap: <T extends Element, K, V = T>(selector: string, keyFn: (element: T) => K, valueFn?: (element: T) => V) => Map<K, V>;
10
7
  waitChild: (selector: string, timeout?: number) => Promise<Element>;
11
8
  }
12
9
  interface Element {
13
- getAttributeStrict: <T extends string>(qualifiedName: string) => T;
14
- getAttributeAsFloatStrict: (qualifiedName: string, allowNegative?: boolean) => number;
15
- getAttributeAsIntStrict: (qualifiedName: string, radix?: number, allowNegative?: boolean) => number;
16
- getTextStrict: <T extends string>() => T;
17
- getTextAsIntStrict: (radix?: number, allowNegative?: boolean) => number;
18
- getTextAsFloatStrict: (allowNegative?: boolean) => number;
19
- queryAsArray: <T = Element>(selector: string, valueSelector?: (element: Element) => T) => T[];
20
- queryAsSet: <T = Element>(selector: string, valueSelector?: (element: Element) => T) => Set<T>;
10
+ getAttributeStrict: (name: string) => string;
11
+ getAttributeAsFloatStrict: (name: string) => number;
12
+ getAttributeAsIntStrict: (name: string, radix?: number) => number;
13
+ getTextStrict: () => string;
14
+ getTextAsIntStrict: (radix?: number) => number;
15
+ getTextAsFloatStrict: () => number;
16
+ queryAsArray: <T = Element>(selector: string, valueFn?: (element: Element) => T) => T[];
17
+ queryAsSet: <T = Element>(selector: string, valueFn?: (element: Element) => T) => Set<T>;
21
18
  queryStrict: <T extends Element>(selector: string) => T;
22
- queryAsMap: <T extends Element, K, V = T>(selector: string, keySelector: (element: T) => K, valueSelector?: (element: T) => V) => Map<K, V>;
19
+ queryAsMap: <T extends Element, K, V = T>(selector: string, keyFn: (element: T) => K, valueFn?: (element: T) => V) => Map<K, V>;
23
20
  waitChild: (selector: string, timeout?: number) => Promise<Element>;
24
21
  }
25
22
  interface URLSearchParams {
26
- getStrict: <T extends string>(name: string) => T;
23
+ getStrict: (name: string) => string;
27
24
  getAsInteger: (name: string, radix?: number) => number | null;
28
25
  getAsIntegerStrict: (name: string, radix?: number) => number;
29
26
  }
30
- interface MapConstructor {
31
- fromElements: <T extends Element[] | NodeList | string, K, V>(source: T, keySelector: (element: Element) => K, valueSelector: (element: Element) => V) => Map<K, V>;
32
- }
33
- interface SetConstructor {
34
- fromElements: <T extends Element[] | NodeList | string, K>(source: T, valueSelector: (element: Element) => K) => Set<K>;
35
- }
36
27
  }
37
28
  export {};
package/dist/index.js CHANGED
@@ -1,273 +1,212 @@
1
1
  (function () {
2
2
  'use strict';
3
3
 
4
- Document.prototype.queryStrict = function (selector) {
5
- const element = this.querySelector(selector);
6
- if (!element)
7
- throw new Error(`No element found for selector "${selector}"`);
8
- return element;
9
- };
10
- Document.prototype.queryAsArray = function (selector, valueSelector) {
11
- valueSelector ??= (element) => element;
12
- const elements = this.querySelectorAll(selector);
13
- return Array.from(elements, valueSelector);
14
- };
15
- Document.prototype.queryAsSet = function (selector, valueSelector) {
16
- valueSelector ??= (element) => element;
17
- const elements = this.queryAsArray(selector, valueSelector);
18
- return new Set(elements);
19
- };
20
- Document.prototype.queryAsMap = function (selector, keySelector, valueSelector = (el) => el) {
21
- const elements = this.queryAsArray(selector);
22
- const map = new Map();
23
- for (const element of elements) {
24
- const key = keySelector(element);
25
- const value = valueSelector(element);
26
- map.set(key, value);
27
- }
28
- return map;
29
- };
30
- Document.prototype.waitChild = function (selector, timeoutMillis = 300_000) {
31
- const el = this.querySelector(selector);
32
- if (el)
33
- return Promise.resolve(el);
34
- return new Promise((resolve, reject) => {
35
- let timeout = null;
36
- let interval = null;
37
- const observer = new MutationObserver((mutations) => {
38
- const element = this.querySelector(selector);
39
- if (element) {
40
- onElementFound(element);
41
- return;
42
- }
43
- for (const mutation of mutations) {
44
- if (mutation.type !== 'childList')
45
- continue;
46
- for (const node of Array.from(mutation.addedNodes)) {
47
- if (node instanceof Element && node.matches(`:has(${selector})`)) {
48
- onElementFound(node);
49
- return;
50
- }
51
- }
52
- }
53
- });
54
- function onElementFound(element) {
55
- if (typeof timeout === 'number')
56
- clearTimeout(timeout);
57
- if (typeof interval === 'number')
58
- clearInterval(interval);
59
- observer.disconnect();
60
- resolve(element);
4
+ function getAttributeStrict() {
5
+ return function (attribute) {
6
+ const attr = this.getAttribute(attribute)?.trim();
7
+ if (typeof attr !== 'string' || attr.length === 0) {
8
+ throw new Error(`attribute "${attribute}" not found`);
61
9
  }
62
- function onInterval() {
63
- const element = this.querySelector(selector);
64
- if (element) {
65
- onElementFound(element);
66
- }
10
+ return attr;
11
+ };
12
+ }
13
+ function getAttributeAsFloatStrict() {
14
+ return function (attribute) {
15
+ const attr = this.getAttributeStrict(attribute);
16
+ const value = Number.parseFloat(attr.trim());
17
+ if (!Number.isFinite(value)) {
18
+ throw new TypeError(`could not parse attribute "${attribute}" as float`);
67
19
  }
68
- function onTimeout() {
69
- if (typeof interval === 'number')
70
- clearInterval(interval);
71
- observer.disconnect();
72
- reject(new Error(`timeout waiting for element: ${selector}`));
20
+ return value;
21
+ };
22
+ }
23
+ function getAttributeAsIntStrict() {
24
+ return function (attribute, radix = 10) {
25
+ const attr = this.getAttributeStrict(attribute);
26
+ const value = Number.parseInt(attr.trim(), radix);
27
+ if (!Number.isInteger(value)) {
28
+ throw new TypeError(`could not parse attribute "${attribute}" as integer`);
73
29
  }
74
- interval = setInterval(onInterval.bind(this), 100);
75
- timeout = setTimeout(onTimeout, timeoutMillis);
76
- observer.observe(this, { childList: true, subtree: true });
77
- });
78
- };
79
- Element.prototype.getAttributeStrict = function (attribute) {
80
- const value = this.getAttribute(attribute)?.trim();
81
- if (typeof value !== 'string' || value.length === 0) {
82
- throw new Error(`attribute "${attribute}" not found`);
83
- }
84
- return value;
85
- };
86
- Element.prototype.getAttributeAsFloatStrict = function (attribute) {
87
- const value = this.getAttributeStrict(attribute);
88
- const parsed = Number.parseFloat(value);
89
- if (!Number.isFinite(parsed))
90
- throw new Error(`could not parse attribute "${attribute}" as float`);
91
- return parsed;
92
- };
93
- Element.prototype.getAttributeAsIntStrict = function (attribute, radix = 10) {
94
- const value = this.getAttributeStrict(attribute);
95
- const parsed = Number.parseInt(value.replace(/\D/g, ''), radix);
96
- if (!Number.isInteger(parsed)) {
97
- throw new TypeError(`could not parse attribute "${attribute}" as integer`);
98
- }
99
- return parsed;
100
- };
101
- Element.prototype.getTextStrict = function () {
102
- const content = this.textContent?.trim();
103
- if (typeof content !== 'string' || content.length === 0) {
104
- throw new Error('element has no text content');
105
- }
106
- return content;
107
- };
108
- Element.prototype.getTextAsIntStrict = function (radix = 10) {
109
- const content = this.getTextStrict();
110
- const parsed = Number.parseInt(content.replace(/\D/g, ''), radix);
111
- if (!Number.isInteger(parsed)) {
112
- throw new TypeError('could not parse text content as integer');
113
- }
114
- return parsed;
115
- };
116
- Element.prototype.getTextAsFloatStrict = function () {
117
- const content = this.getTextStrict();
118
- const parsed = Number.parseFloat(content);
119
- if (!Number.isFinite(parsed)) {
120
- throw new TypeError('could not parse text content as float');
121
- }
122
- return parsed;
123
- };
124
- Element.prototype.queryStrict = function (selector) {
125
- const element = this.querySelector(selector);
126
- if (!element)
127
- throw new Error(`no element found for selector "${selector}"`);
128
- return element;
129
- };
130
- Element.prototype.queryAsArray = function (selector, valueSelector) {
131
- valueSelector ??= (element) => element;
132
- const elements = this.querySelectorAll(selector);
133
- return Array.from(elements, valueSelector);
134
- };
135
- Element.prototype.queryAsSet = function (selector, valueSelector) {
136
- valueSelector ??= (element) => element;
137
- const elements = this.queryAsArray(selector, valueSelector);
138
- return new Set(elements);
139
- };
140
- Element.prototype.queryAsMap = function (selector, keySelector, valueSelector = (el) => el) {
141
- const elements = this.queryAsArray(selector);
142
- const map = new Map();
143
- for (const element of elements) {
144
- const key = keySelector(element);
145
- const value = valueSelector(element);
146
- map.set(key, value);
147
- }
148
- return map;
149
- };
150
- Element.prototype.waitChild = function (selector, timeoutMillis = 300_000) {
151
- const el = this.querySelector(selector);
152
- if (el)
153
- return Promise.resolve(el);
154
- return new Promise((resolve, reject) => {
155
- let timeout = null;
156
- let interval = null;
157
- const observer = new MutationObserver((mutations) => {
158
- const element = this.querySelector(selector);
159
- if (element) {
160
- onElementFound(element);
161
- return;
162
- }
163
- for (const mutation of mutations) {
164
- if (mutation.type !== 'childList')
165
- continue;
166
- for (const node of Array.from(mutation.addedNodes)) {
167
- if (node instanceof Element && node.matches(`:has(${selector})`)) {
168
- onElementFound(node);
169
- return;
30
+ return value;
31
+ };
32
+ }
33
+ function getTextStrict() {
34
+ return function () {
35
+ const text = this.textContent?.trim();
36
+ if (typeof text !== 'string' || text.length === 0) {
37
+ throw new Error('element has no text content');
38
+ }
39
+ return text;
40
+ };
41
+ }
42
+ function getTextAsIntStrict() {
43
+ return function (radix = 10) {
44
+ const value = Number.parseInt(this.getTextStrict(), radix);
45
+ if (!Number.isInteger(value)) {
46
+ throw new TypeError('could not parse text content as integer');
47
+ }
48
+ return value;
49
+ };
50
+ }
51
+ function getTextAsFloatStrict() {
52
+ return function () {
53
+ const value = Number.parseFloat(this.getTextStrict());
54
+ if (!Number.isFinite(value)) {
55
+ throw new TypeError('could not parse text content as float');
56
+ }
57
+ return value;
58
+ };
59
+ }
60
+ function queryStrict() {
61
+ return function (selector) {
62
+ const element = this.querySelector(selector);
63
+ if (!element)
64
+ throw new Error(`no element found for selector "${selector}"`);
65
+ return element;
66
+ };
67
+ }
68
+ function queryAsArray() {
69
+ return function (selector, valueFn) {
70
+ valueFn ??= (element) => element;
71
+ const elements = this.querySelectorAll(selector);
72
+ return Array.from(elements, valueFn);
73
+ };
74
+ }
75
+ function queryAsSet() {
76
+ return function (selector, valueFn) {
77
+ valueFn ??= (element) => element;
78
+ const elements = this.queryAsArray(selector, valueFn);
79
+ return new Set(elements);
80
+ };
81
+ }
82
+ function queryAsMap() {
83
+ return function (selector, keyFn, valueFn) {
84
+ valueFn ??= (element) => element;
85
+ const elements = this.queryAsArray(selector);
86
+ const map = new Map();
87
+ for (const element of elements) {
88
+ const key = keyFn(element);
89
+ const value = valueFn(element);
90
+ map.set(key, value);
91
+ }
92
+ return map;
93
+ };
94
+ }
95
+ function waitChild() {
96
+ return function (selector, timeoutMillis = 300_000) {
97
+ const el = this.querySelector(selector);
98
+ if (el)
99
+ return Promise.resolve(el);
100
+ return new Promise((resolve, reject) => {
101
+ let timeout = null;
102
+ let interval = null;
103
+ const observer = new MutationObserver((mutations) => {
104
+ const element = this.querySelector(selector);
105
+ if (element) {
106
+ onElementFound(element);
107
+ return;
108
+ }
109
+ for (const mutation of mutations) {
110
+ if (mutation.type !== 'childList')
111
+ continue;
112
+ for (const node of Array.from(mutation.addedNodes)) {
113
+ if (node instanceof Element && node.matches(`:has(${selector})`)) {
114
+ onElementFound(node);
115
+ return;
116
+ }
170
117
  }
171
118
  }
119
+ });
120
+ function onElementFound(element) {
121
+ if (typeof timeout === 'number')
122
+ clearTimeout(timeout);
123
+ if (typeof interval === 'number')
124
+ clearInterval(interval);
125
+ observer.disconnect();
126
+ resolve(element);
172
127
  }
173
- });
174
- function onElementFound(element) {
175
- if (typeof timeout === 'number')
176
- clearTimeout(timeout);
177
- if (typeof interval === 'number')
178
- clearInterval(interval);
179
- observer.disconnect();
180
- resolve(element);
181
- }
182
- function onInterval() {
183
- const element = this.querySelector(selector);
184
- if (element) {
185
- onElementFound(element);
128
+ function onInterval() {
129
+ const element = this.querySelector(selector);
130
+ if (element)
131
+ onElementFound(element);
186
132
  }
187
- }
188
- function onTimeout() {
189
- if (typeof interval === 'number')
190
- clearInterval(interval);
191
- observer.disconnect();
192
- reject(new Error(`timeout waiting for element: ${selector}`));
193
- }
194
- interval = setInterval(onInterval.bind(this), 100);
195
- timeout = setTimeout(onTimeout, timeoutMillis);
196
- observer.observe(this, { childList: true, subtree: true });
197
- });
198
- };
199
- URLSearchParams.prototype.getStrict = function (name) {
200
- const item = this.get(name);
201
- if (item === null) {
202
- throw new Error(`key "${name}" not found in URL search parameters`);
203
- }
204
- return item;
205
- };
206
- URLSearchParams.prototype.getAsInteger = function (name, radix = 10) {
207
- const item = this.get(name);
208
- if (item === null)
209
- return item;
210
- return Number.parseInt(item, radix);
211
- };
212
- URLSearchParams.prototype.getAsIntegerStrict = function (name, radix = 10) {
213
- const item = this.getStrict(name);
214
- const parsed = Number.parseInt(item, radix);
215
- if (!Number.isInteger(parsed)) {
216
- throw new TypeError(`could not parse "${item}" as an integer`);
217
- }
218
- return parsed;
219
- };
220
- Array.fromElements = function (source, valueSelector) {
221
- isValidElementSource(source);
222
- const elements = parseElementSource(source);
223
- return Array.from(elements, valueSelector);
133
+ function onTimeout() {
134
+ if (typeof interval === 'number')
135
+ clearInterval(interval);
136
+ observer.disconnect();
137
+ reject(new Error(`timeout waiting for element: ${selector}`));
138
+ }
139
+ interval = setInterval(onInterval.bind(this), 100);
140
+ timeout = setTimeout(onTimeout, timeoutMillis);
141
+ observer.observe(this, { childList: true, subtree: true });
142
+ });
143
+ };
144
+ }
145
+ const element = {
146
+ getAttributeStrict,
147
+ getAttributeAsFloatStrict,
148
+ getAttributeAsIntStrict,
149
+ getTextStrict,
150
+ getTextAsIntStrict,
151
+ getTextAsFloatStrict,
152
+ queryStrict,
153
+ queryAsArray,
154
+ queryAsSet,
155
+ queryAsMap,
156
+ waitChild
224
157
  };
225
- Map.fromElements = function (source, keySelector, valueSelector) {
226
- isValidElementSource(source);
227
- const elements = parseElementSource(source);
228
- const map = new Map();
229
- for (const element of elements) {
230
- if (!(element instanceof Element)) {
231
- throw new TypeError('item in source array is not an element');
158
+
159
+ function getStrict() {
160
+ return function (name) {
161
+ const item = this.get(name);
162
+ if (typeof item !== 'string') {
163
+ throw new TypeError(`key "${name}" not found in URL search parameters`);
232
164
  }
233
- const key = keySelector(element);
234
- const value = valueSelector(element);
235
- map.set(key, value);
236
- }
237
- return map;
238
- };
239
- Set.fromElements = function (source, valueSelector) {
240
- isValidElementSource(source);
241
- const elements = parseElementSource(source);
242
- const set = new Set();
243
- for (const element of elements) {
244
- if (!(element instanceof Element)) {
245
- throw new TypeError('item in source array is not an element');
165
+ return item;
166
+ };
167
+ }
168
+ function getAsInteger() {
169
+ return function (name, radix = 10) {
170
+ const item = this.get(name);
171
+ if (typeof item !== 'string') {
172
+ return null;
246
173
  }
247
- const value = valueSelector(element);
248
- set.add(value);
249
- }
250
- return set;
251
- };
252
- function isValidElementSource(source) {
253
- if (!Array.isArray(source) &&
254
- !(source instanceof NodeList) &&
255
- (typeof source !== 'string' || source.length === 0)) {
256
- throw new TypeError('source must be an array, a NodeList or a string');
257
- }
258
- return true;
174
+ return Number.parseInt(item, radix);
175
+ };
259
176
  }
260
- function parseElementSource(source) {
261
- if (typeof source === 'string') {
262
- return document.queryAsArray(source);
263
- }
264
- else if (Array.isArray(source)) {
265
- return source;
266
- }
267
- else if (source instanceof NodeList) {
268
- return Array.from(source);
269
- }
270
- throw new TypeError('source must be an array, a NodeList or a string');
177
+ function getAsIntegerStrict() {
178
+ return function (name, radix = 10) {
179
+ const value = Number.parseInt(this.getStrict(name), radix);
180
+ if (!Number.isInteger(value)) {
181
+ throw new TypeError(`could not parse "${name}" as an integer`);
182
+ }
183
+ return value;
184
+ };
271
185
  }
186
+ const urlSearchParams = {
187
+ getStrict,
188
+ getAsInteger,
189
+ getAsIntegerStrict
190
+ };
191
+
192
+ Document.prototype.queryStrict = element.queryStrict();
193
+ Document.prototype.queryAsArray = element.queryAsArray();
194
+ Document.prototype.queryAsSet = element.queryAsSet();
195
+ Document.prototype.queryAsMap = element.queryAsMap();
196
+ Document.prototype.waitChild = element.waitChild();
197
+ Element.prototype.getAttributeStrict = element.getAttributeStrict();
198
+ Element.prototype.getAttributeAsFloatStrict = element.getAttributeAsFloatStrict();
199
+ Element.prototype.getAttributeAsIntStrict = element.getAttributeAsIntStrict();
200
+ Element.prototype.getTextStrict = element.getTextStrict();
201
+ Element.prototype.getTextAsIntStrict = element.getTextAsIntStrict();
202
+ Element.prototype.getTextAsFloatStrict = element.getTextAsFloatStrict();
203
+ Element.prototype.queryStrict = element.queryStrict();
204
+ Element.prototype.queryAsArray = element.queryAsArray();
205
+ Element.prototype.queryAsSet = element.queryAsSet();
206
+ Element.prototype.queryAsMap = element.queryAsMap();
207
+ Element.prototype.waitChild = element.waitChild();
208
+ URLSearchParams.prototype.getStrict = urlSearchParams.getStrict();
209
+ URLSearchParams.prototype.getAsInteger = urlSearchParams.getAsInteger();
210
+ URLSearchParams.prototype.getAsIntegerStrict = urlSearchParams.getAsIntegerStrict();
272
211
 
273
212
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tb-dev/prototype-dom",
3
- "version": "6.1.0",
3
+ "version": "6.1.1",
4
4
  "description": "Adds prototype methods to DOM objects",
5
5
  "packageManager": "pnpm@8.15.4",
6
6
  "license": "MIT",
@@ -24,7 +24,7 @@
24
24
  },
25
25
  "devDependencies": {
26
26
  "@rollup/plugin-typescript": "^11.1.6",
27
- "@tb-dev/eslint-config": "^2.0.3",
27
+ "@tb-dev/eslint-config": "^2.1.1",
28
28
  "@vitest/ui": "^1.3.1",
29
29
  "eslint": "^8.57.0",
30
30
  "husky": "^9.0.11",
@@ -32,7 +32,7 @@
32
32
  "prettier": "^3.2.5",
33
33
  "rollup": "^4.12.0",
34
34
  "tslib": "^2.6.2",
35
- "typedoc": "^0.25.8",
35
+ "typedoc": "^0.25.9",
36
36
  "typedoc-plugin-mdn-links": "^3.1.17",
37
37
  "typescript": "5.3.3",
38
38
  "vitest": "^1.3.1"
@@ -43,7 +43,7 @@
43
43
  "main": "./dist/index.js",
44
44
  "types": "./dist/index.d.ts",
45
45
  "scripts": {
46
- "build": "rollup --config rollup.config.js",
46
+ "build": "rollup --config rollup.config.js && node scripts/delete-artifacts.mjs",
47
47
  "docs": "typedoc --plugin typedoc-plugin-mdn-links",
48
48
  "format": "prettier . --write",
49
49
  "format-check": "prettier . --check",