ts-dom-utils 1.0.0
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 +80 -0
- package/dist/chunk-2CYKZHHS.js +32 -0
- package/dist/chunk-MRTNZYZJ.js +8 -0
- package/dist/chunk-QUWBOENH.js +8 -0
- package/dist/createElement.cjs +52 -0
- package/dist/createElement.d.ts +15 -0
- package/dist/createElement.js +6 -0
- package/dist/index.cjs +72 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +14 -0
- package/dist/qs.cjs +28 -0
- package/dist/qs.d.ts +9 -0
- package/dist/qs.js +6 -0
- package/dist/qsa.cjs +28 -0
- package/dist/qsa.d.ts +8 -0
- package/dist/qsa.js +6 -0
- package/package.json +34 -0
- package/tsconfig.json +12 -0
- package/tsup.config.ts +7 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Rafał Całka
|
|
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
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# TS Dom Utils
|
|
2
|
+
A lightweight TypeScript library that provides simple wrappers around common DOM manipulation functions.
|
|
3
|
+
It allows you to write less code.
|
|
4
|
+
Although it's written in TypeScript, it can also be used in JavaScript projects.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```shell
|
|
9
|
+
# npm
|
|
10
|
+
npm install ts-dom-utils
|
|
11
|
+
# or Yarn
|
|
12
|
+
yarn add ts-dom-utils
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
- Strong TypeScript typings for better editor autocompletion and error checking.
|
|
17
|
+
- Compatible with both TypeScript and JavaScript projects.
|
|
18
|
+
- Simple and intuitive API.
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
## Functions
|
|
22
|
+
|
|
23
|
+
### `createElement`
|
|
24
|
+
Creates a new element with the provided options.
|
|
25
|
+
|
|
26
|
+
Example
|
|
27
|
+
```typescript
|
|
28
|
+
import { createElement } from 'ts-dom-utils';
|
|
29
|
+
|
|
30
|
+
const button = createElement('button', {
|
|
31
|
+
id: 'my-button',
|
|
32
|
+
'aria-expandended': 'false',
|
|
33
|
+
class: ['btn', 'btn-primary'],
|
|
34
|
+
text: 'Click me',
|
|
35
|
+
dataset: {
|
|
36
|
+
action: 'open-modal',
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
document.body.appendChild(button);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
| Param | Default | Description |
|
|
44
|
+
|---------|-----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
45
|
+
| tagName | undefined | The tag name of the element type to create. |
|
|
46
|
+
| options | {} | The options to use when creating the element. Options can include any attributes that can be passed to `setAttribute`, with `class`, `dataset`, and `text` as special options for enhancement. |
|
|
47
|
+
| target | document | The Document in which to create the element. |
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
### `qs`
|
|
51
|
+
A wrapper function for `document.querySelector`.
|
|
52
|
+
|
|
53
|
+
Example
|
|
54
|
+
```typescript
|
|
55
|
+
import { qs } from 'ts-dom-utils';
|
|
56
|
+
|
|
57
|
+
const wrapper = qs<HTMLDivElement>('.footer > .buttons');
|
|
58
|
+
const button = qs<HTMLButtonElement>('button', wrapper);
|
|
59
|
+
```
|
|
60
|
+
| Param | Default | Description |
|
|
61
|
+
|----------|------------|-----------------------------------------------------|
|
|
62
|
+
| selector | undefined | The selector to match against. |
|
|
63
|
+
| parent | document | The ParentNode in which to search for the selector. |
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
### `qsa`
|
|
68
|
+
A wrapper function for `document.querySelectorAll`.
|
|
69
|
+
|
|
70
|
+
Example
|
|
71
|
+
```typescript
|
|
72
|
+
import { qsa } from 'ts-dom-utils';
|
|
73
|
+
|
|
74
|
+
const buttons = qsa<HTMLButtonElement>('.btn');
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
| Param | Default | Description |
|
|
78
|
+
|----------|-----------|-----------------------------------------------------|
|
|
79
|
+
| selector | undefined | The selector to match against. |
|
|
80
|
+
| parent | document | The ParentNode in which to search for the selector. |
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// src/createElement.ts
|
|
2
|
+
function createElement(tagName, options = {}, target = document) {
|
|
3
|
+
const element = target.createElement(tagName);
|
|
4
|
+
Object.entries(options).forEach(([key, value]) => {
|
|
5
|
+
if (key === "class") {
|
|
6
|
+
if (Array.isArray(value)) {
|
|
7
|
+
element.classList.add(...value);
|
|
8
|
+
} else {
|
|
9
|
+
element.classList.add(value);
|
|
10
|
+
}
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (key === "dataset" && typeof value === "object" && value !== null) {
|
|
14
|
+
Object.entries(value).forEach(
|
|
15
|
+
([dataKey, dataValue]) => {
|
|
16
|
+
element.dataset[dataKey] = dataValue;
|
|
17
|
+
}
|
|
18
|
+
);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (key === "text") {
|
|
22
|
+
element.textContent = value;
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
element.setAttribute(key, value);
|
|
26
|
+
});
|
|
27
|
+
return element;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export {
|
|
31
|
+
createElement
|
|
32
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/createElement.ts
|
|
21
|
+
var createElement_exports = {};
|
|
22
|
+
__export(createElement_exports, {
|
|
23
|
+
default: () => createElement
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(createElement_exports);
|
|
26
|
+
function createElement(tagName, options = {}, target = document) {
|
|
27
|
+
const element = target.createElement(tagName);
|
|
28
|
+
Object.entries(options).forEach(([key, value]) => {
|
|
29
|
+
if (key === "class") {
|
|
30
|
+
if (Array.isArray(value)) {
|
|
31
|
+
element.classList.add(...value);
|
|
32
|
+
} else {
|
|
33
|
+
element.classList.add(value);
|
|
34
|
+
}
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (key === "dataset" && typeof value === "object" && value !== null) {
|
|
38
|
+
Object.entries(value).forEach(
|
|
39
|
+
([dataKey, dataValue]) => {
|
|
40
|
+
element.dataset[dataKey] = dataValue;
|
|
41
|
+
}
|
|
42
|
+
);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (key === "text") {
|
|
46
|
+
element.textContent = value;
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
element.setAttribute(key, value);
|
|
50
|
+
});
|
|
51
|
+
return element;
|
|
52
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a new element with the provided options.
|
|
3
|
+
* @param tagName - The name of the element type to create.
|
|
4
|
+
* @param options - The options to use when creating the element.
|
|
5
|
+
* @param target - The Document in which to create the element. Defaults to the current document.
|
|
6
|
+
*/
|
|
7
|
+
declare function createElement<K extends keyof HTMLElementTagNameMap>(tagName: K, options?: CreateElementOptions, target?: Document): HTMLElementTagNameMap[K];
|
|
8
|
+
type CreateElementOptions = {
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
class?: string | string[];
|
|
11
|
+
dataset?: Record<string, string>;
|
|
12
|
+
text?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export { CreateElementOptions, createElement as default };
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
createElement: () => createElement,
|
|
24
|
+
qs: () => qs,
|
|
25
|
+
qsa: () => qsa
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(src_exports);
|
|
28
|
+
|
|
29
|
+
// src/createElement.ts
|
|
30
|
+
function createElement(tagName, options = {}, target = document) {
|
|
31
|
+
const element = target.createElement(tagName);
|
|
32
|
+
Object.entries(options).forEach(([key, value]) => {
|
|
33
|
+
if (key === "class") {
|
|
34
|
+
if (Array.isArray(value)) {
|
|
35
|
+
element.classList.add(...value);
|
|
36
|
+
} else {
|
|
37
|
+
element.classList.add(value);
|
|
38
|
+
}
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (key === "dataset" && typeof value === "object" && value !== null) {
|
|
42
|
+
Object.entries(value).forEach(
|
|
43
|
+
([dataKey, dataValue]) => {
|
|
44
|
+
element.dataset[dataKey] = dataValue;
|
|
45
|
+
}
|
|
46
|
+
);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (key === "text") {
|
|
50
|
+
element.textContent = value;
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
element.setAttribute(key, value);
|
|
54
|
+
});
|
|
55
|
+
return element;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// src/qs.ts
|
|
59
|
+
function qs(selector, parent = document) {
|
|
60
|
+
return parent.querySelector(selector);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// src/qsa.ts
|
|
64
|
+
function qsa(selector, parent = document) {
|
|
65
|
+
return parent.querySelectorAll(selector);
|
|
66
|
+
}
|
|
67
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
68
|
+
0 && (module.exports = {
|
|
69
|
+
createElement,
|
|
70
|
+
qs,
|
|
71
|
+
qsa
|
|
72
|
+
});
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/qs.cjs
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/qs.ts
|
|
21
|
+
var qs_exports = {};
|
|
22
|
+
__export(qs_exports, {
|
|
23
|
+
default: () => qs
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(qs_exports);
|
|
26
|
+
function qs(selector, parent = document) {
|
|
27
|
+
return parent.querySelector(selector);
|
|
28
|
+
}
|
package/dist/qs.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QuerySelector wrapper function.
|
|
3
|
+
* @param selector The selector to match against.
|
|
4
|
+
* @param parent The ParentNode in which to search for the selector.
|
|
5
|
+
* @returns The first Element within the document that matches the specified selector, or group of selectors.
|
|
6
|
+
*/
|
|
7
|
+
declare function qs<E extends Element>(selector: string, parent?: ParentNode): E | null;
|
|
8
|
+
|
|
9
|
+
export { qs as default };
|
package/dist/qs.js
ADDED
package/dist/qsa.cjs
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/qsa.ts
|
|
21
|
+
var qsa_exports = {};
|
|
22
|
+
__export(qsa_exports, {
|
|
23
|
+
default: () => qsa
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(qsa_exports);
|
|
26
|
+
function qsa(selector, parent = document) {
|
|
27
|
+
return parent.querySelectorAll(selector);
|
|
28
|
+
}
|
package/dist/qsa.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QuerySelectorAll wrapper function.
|
|
3
|
+
* @param selector - The selector to match against.
|
|
4
|
+
* @param parent - The ParentNode in which to search for the selector. Defaults to the entire document.
|
|
5
|
+
*/
|
|
6
|
+
declare function qsa<E extends Element>(selector: string, parent?: ParentNode): NodeListOf<E>;
|
|
7
|
+
|
|
8
|
+
export { qsa as default };
|
package/dist/qsa.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ts-dom-utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A simple, typeful utility library for DOM manipulation in TypeScript.",
|
|
5
|
+
"main": "dist/index.cjs",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"sideEffects": false,
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsup --dts",
|
|
12
|
+
"lint": "tsc"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/rafaucau/ts-dom-utils.git"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"dom",
|
|
20
|
+
"typescript",
|
|
21
|
+
"utilities"
|
|
22
|
+
],
|
|
23
|
+
"author": "rafaucau",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/rafaucau/ts-dom-utils/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/rafaucau/ts-dom-utils#readme",
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"prettier": "^2.8.8",
|
|
31
|
+
"tsup": "^6.7.0",
|
|
32
|
+
"typescript": "^5.0.4"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/tsconfig.json
ADDED