@tb-dev/prototype-dom 5.2.2
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/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +175 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Andrew Ferreira
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
interface ArrayConstructor {
|
|
3
|
+
fromElements: <T extends Element[] | NodeList | string, V>(source: T, valueSelector: (element: Element) => V) => V[];
|
|
4
|
+
}
|
|
5
|
+
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>;
|
|
10
|
+
}
|
|
11
|
+
interface Element {
|
|
12
|
+
getAttributeStrict: <T extends string>(qualifiedName: string) => T;
|
|
13
|
+
getAttributeAsFloatStrict: (qualifiedName: string, allowNegative?: boolean) => number;
|
|
14
|
+
getAttributeAsIntStrict: (qualifiedName: string, radix?: number, allowNegative?: boolean) => number;
|
|
15
|
+
getTextContentStrict: <T extends string>() => T;
|
|
16
|
+
parseIntStrict: (radix?: number, allowNegative?: boolean) => number;
|
|
17
|
+
parseFloatStrict: (allowNegative?: boolean) => number;
|
|
18
|
+
queryAsArray: <T = Element>(selector: string, valueSelector?: (element: Element) => T) => T[];
|
|
19
|
+
queryAsSet: <T = Element>(selector: string, valueSelector?: (element: Element) => T) => Set<T>;
|
|
20
|
+
queryStrict: <T extends Element>(selector: string) => T;
|
|
21
|
+
queryAsMap: <T extends Element, K, V = T>(selector: string, keySelector: (element: T) => K, valueSelector?: (element: T) => V) => Map<K, V>;
|
|
22
|
+
}
|
|
23
|
+
interface URLSearchParams {
|
|
24
|
+
getStrict: <T extends string>(name: string) => T;
|
|
25
|
+
getAsInteger: (name: string, radix?: number) => number | null;
|
|
26
|
+
getAsIntegerStrict: (name: string, radix?: number) => number;
|
|
27
|
+
}
|
|
28
|
+
interface MapConstructor {
|
|
29
|
+
fromElements: <T extends Element[] | NodeList | string, K, V>(source: T, keySelector: (element: Element) => K, valueSelector: (element: Element) => V) => Map<K, V>;
|
|
30
|
+
}
|
|
31
|
+
interface SetConstructor {
|
|
32
|
+
fromElements: <T extends Element[] | NodeList | string, K>(source: T, valueSelector: (element: Element) => K) => Set<K>;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
(function () {
|
|
2
|
+
'use strict';
|
|
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
|
+
Element.prototype.getAttributeStrict = function (attribute) {
|
|
31
|
+
const value = this.getAttribute(attribute)?.trim();
|
|
32
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
33
|
+
throw new Error(`attribute "${attribute}" not found`);
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
};
|
|
37
|
+
Element.prototype.getAttributeAsFloatStrict = function (attribute) {
|
|
38
|
+
const value = this.getAttributeStrict(attribute);
|
|
39
|
+
const parsed = Number.parseFloat(value);
|
|
40
|
+
if (!Number.isFinite(parsed))
|
|
41
|
+
throw new Error(`could not parse attribute "${attribute}" as float`);
|
|
42
|
+
return parsed;
|
|
43
|
+
};
|
|
44
|
+
Element.prototype.getAttributeAsIntStrict = function (attribute, radix = 10) {
|
|
45
|
+
const value = this.getAttributeStrict(attribute);
|
|
46
|
+
const parsed = Number.parseInt(value.replace(/\D/g, ''), radix);
|
|
47
|
+
if (!Number.isInteger(parsed)) {
|
|
48
|
+
throw new TypeError(`could not parse attribute "${attribute}" as integer`);
|
|
49
|
+
}
|
|
50
|
+
return parsed;
|
|
51
|
+
};
|
|
52
|
+
Element.prototype.getTextContentStrict = function () {
|
|
53
|
+
const content = this.textContent?.trim();
|
|
54
|
+
if (typeof content !== 'string' || content.length === 0) {
|
|
55
|
+
throw new Error('element has no text content');
|
|
56
|
+
}
|
|
57
|
+
return content;
|
|
58
|
+
};
|
|
59
|
+
Element.prototype.parseIntStrict = function (radix = 10) {
|
|
60
|
+
const content = this.getTextContentStrict();
|
|
61
|
+
const parsed = Number.parseInt(content.replace(/\D/g, ''), radix);
|
|
62
|
+
if (!Number.isInteger(parsed)) {
|
|
63
|
+
throw new TypeError('could not parse text content as integer');
|
|
64
|
+
}
|
|
65
|
+
return parsed;
|
|
66
|
+
};
|
|
67
|
+
Element.prototype.parseFloatStrict = function () {
|
|
68
|
+
const content = this.getTextContentStrict();
|
|
69
|
+
const parsed = Number.parseFloat(content);
|
|
70
|
+
if (!Number.isFinite(parsed)) {
|
|
71
|
+
throw new TypeError('could not parse text content as float');
|
|
72
|
+
}
|
|
73
|
+
return parsed;
|
|
74
|
+
};
|
|
75
|
+
Element.prototype.queryStrict = function (selector) {
|
|
76
|
+
const element = this.querySelector(selector);
|
|
77
|
+
if (!element)
|
|
78
|
+
throw new Error(`no element found for selector "${selector}"`);
|
|
79
|
+
return element;
|
|
80
|
+
};
|
|
81
|
+
Element.prototype.queryAsArray = function (selector, valueSelector) {
|
|
82
|
+
valueSelector ??= (element) => element;
|
|
83
|
+
const elements = this.querySelectorAll(selector);
|
|
84
|
+
return Array.from(elements, valueSelector);
|
|
85
|
+
};
|
|
86
|
+
Element.prototype.queryAsSet = function (selector, valueSelector) {
|
|
87
|
+
valueSelector ??= (element) => element;
|
|
88
|
+
const elements = this.queryAsArray(selector, valueSelector);
|
|
89
|
+
return new Set(elements);
|
|
90
|
+
};
|
|
91
|
+
Element.prototype.queryAsMap = function (selector, keySelector, valueSelector = (el) => el) {
|
|
92
|
+
const elements = this.queryAsArray(selector);
|
|
93
|
+
const map = new Map();
|
|
94
|
+
for (const element of elements) {
|
|
95
|
+
const key = keySelector(element);
|
|
96
|
+
const value = valueSelector(element);
|
|
97
|
+
map.set(key, value);
|
|
98
|
+
}
|
|
99
|
+
return map;
|
|
100
|
+
};
|
|
101
|
+
URLSearchParams.prototype.getStrict = function (name) {
|
|
102
|
+
const item = this.get(name);
|
|
103
|
+
if (item === null) {
|
|
104
|
+
throw new Error(`key "${name}" not found in URL search parameters`);
|
|
105
|
+
}
|
|
106
|
+
return item;
|
|
107
|
+
};
|
|
108
|
+
URLSearchParams.prototype.getAsInteger = function (name, radix = 10) {
|
|
109
|
+
const item = this.get(name);
|
|
110
|
+
if (item === null)
|
|
111
|
+
return item;
|
|
112
|
+
return Number.parseInt(item, radix);
|
|
113
|
+
};
|
|
114
|
+
URLSearchParams.prototype.getAsIntegerStrict = function (name, radix = 10) {
|
|
115
|
+
const item = this.getStrict(name);
|
|
116
|
+
const parsed = Number.parseInt(item, radix);
|
|
117
|
+
if (!Number.isInteger(parsed)) {
|
|
118
|
+
throw new TypeError(`could not parse "${item}" as an integer`);
|
|
119
|
+
}
|
|
120
|
+
return parsed;
|
|
121
|
+
};
|
|
122
|
+
Array.fromElements = function (source, valueSelector) {
|
|
123
|
+
isValidElementSource(source);
|
|
124
|
+
const elements = parseElementSource(source);
|
|
125
|
+
return Array.from(elements, valueSelector);
|
|
126
|
+
};
|
|
127
|
+
Map.fromElements = function (source, keySelector, valueSelector) {
|
|
128
|
+
isValidElementSource(source);
|
|
129
|
+
const elements = parseElementSource(source);
|
|
130
|
+
const map = new Map();
|
|
131
|
+
for (const element of elements) {
|
|
132
|
+
if (!(element instanceof Element)) {
|
|
133
|
+
throw new TypeError('item in source array is not an element');
|
|
134
|
+
}
|
|
135
|
+
const key = keySelector(element);
|
|
136
|
+
const value = valueSelector(element);
|
|
137
|
+
map.set(key, value);
|
|
138
|
+
}
|
|
139
|
+
return map;
|
|
140
|
+
};
|
|
141
|
+
Set.fromElements = function (source, valueSelector) {
|
|
142
|
+
isValidElementSource(source);
|
|
143
|
+
const elements = parseElementSource(source);
|
|
144
|
+
const set = new Set();
|
|
145
|
+
for (const element of elements) {
|
|
146
|
+
if (!(element instanceof Element)) {
|
|
147
|
+
throw new TypeError('item in source array is not an element');
|
|
148
|
+
}
|
|
149
|
+
const value = valueSelector(element);
|
|
150
|
+
set.add(value);
|
|
151
|
+
}
|
|
152
|
+
return set;
|
|
153
|
+
};
|
|
154
|
+
function isValidElementSource(source) {
|
|
155
|
+
if (!Array.isArray(source) &&
|
|
156
|
+
!(source instanceof NodeList) &&
|
|
157
|
+
(typeof source !== 'string' || source.length === 0)) {
|
|
158
|
+
throw new TypeError('source must be an array, a NodeList or a string');
|
|
159
|
+
}
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
function parseElementSource(source) {
|
|
163
|
+
if (typeof source === 'string') {
|
|
164
|
+
return document.queryAsArray(source);
|
|
165
|
+
}
|
|
166
|
+
else if (Array.isArray(source)) {
|
|
167
|
+
return source;
|
|
168
|
+
}
|
|
169
|
+
else if (source instanceof NodeList) {
|
|
170
|
+
return Array.from(source);
|
|
171
|
+
}
|
|
172
|
+
throw new TypeError('source must be an array, a NodeList or a string');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
})();
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tb-dev/prototype-dom",
|
|
3
|
+
"version": "5.2.2",
|
|
4
|
+
"description": "Adds prototype methods to DOM objects",
|
|
5
|
+
"packageManager": "pnpm@8.15.4",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"author": {
|
|
9
|
+
"name": "Andrew Ferreira",
|
|
10
|
+
"email": "andrew.shien2@gmail.com",
|
|
11
|
+
"url": "https://github.com/ferreira-tb"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://tb.dev.br/prototype-dom",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/ferreira-tb/prototype-dom.git"
|
|
17
|
+
},
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/ferreira-tb/prototype-dom/issues",
|
|
20
|
+
"email": "andrew.shien2@gmail.com"
|
|
21
|
+
},
|
|
22
|
+
"lint-staged": {
|
|
23
|
+
"*.{?(c|m)@(j|t)s,css,vue,md,json}": "prettier --write"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "rollup --config rollup.config.js",
|
|
27
|
+
"docs": "typedoc --plugin typedoc-plugin-mdn-links",
|
|
28
|
+
"format": "prettier . --write",
|
|
29
|
+
"format-check": "prettier . --check",
|
|
30
|
+
"prepare": "husky",
|
|
31
|
+
"release": "pnpm run build && pnpm publish",
|
|
32
|
+
"type-check": "tsc --noEmit"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
36
|
+
"@tb-dev/eslint-config": "^2.0.3",
|
|
37
|
+
"@types/node": "^20.11.20",
|
|
38
|
+
"@vitest/ui": "^1.3.1",
|
|
39
|
+
"eslint": "^8.57.0",
|
|
40
|
+
"husky": "^9.0.11",
|
|
41
|
+
"lint-staged": "^15.2.2",
|
|
42
|
+
"prettier": "^3.2.5",
|
|
43
|
+
"rollup": "^4.12.0",
|
|
44
|
+
"tslib": "^2.6.2",
|
|
45
|
+
"typedoc": "^0.25.8",
|
|
46
|
+
"typedoc-plugin-mdn-links": "^3.1.17",
|
|
47
|
+
"typescript": "5.3.3",
|
|
48
|
+
"vitest": "^1.3.1"
|
|
49
|
+
},
|
|
50
|
+
"files": [
|
|
51
|
+
"dist/**/*"
|
|
52
|
+
],
|
|
53
|
+
"main": "./dist/index.js",
|
|
54
|
+
"types": "./dist/index.d.ts"
|
|
55
|
+
}
|