@rickard.antonsson/ts-utility 2.2.1 → 2.2.3
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.
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { Fn } from '../../types';
|
|
2
|
+
/**
|
|
3
|
+
* Create htmlelement with direct possibility of accessing the element and adding childnodes.
|
|
4
|
+
*
|
|
5
|
+
* example: createElement('div', e => (e.textContent = 'Sometext'), [createElement('a{link text}[href=link-url]')])
|
|
6
|
+
* @param basicEmmet div.class1.class-2#my-id[attribute1 attribute2=somevalue]
|
|
7
|
+
* @param listOrFn optional - childList or function which gets the element as input
|
|
8
|
+
* @param list optional - childList
|
|
9
|
+
* @returns HtmlElement
|
|
10
|
+
*/
|
|
2
11
|
export declare function createElement<S extends keyof HTMLElementTagNameMap | `${keyof HTMLElementTagNameMap}${SplittingChars}${string}`>(basicEmmet: S, listOrFn?: (HTMLElement | string | (() => HTMLElement | string))[] | Fn<[HTMLElementTagNameMap[ExtractHtmlTagName<S>]], void>, list?: (HTMLElement | string | (() => HTMLElement | string))[]): HTMLElementTagNameMap[ExtractHtmlTagName<S>];
|
|
3
|
-
type SplittingChars = '.' | '{' | '#';
|
|
12
|
+
type SplittingChars = '.' | '{' | '#' | '[';
|
|
4
13
|
type ExtractHtmlTagName<T extends string> = T extends keyof HTMLElementTagNameMap ? T : T extends `${infer Tag}${SplittingChars}${string}` ? Tag extends keyof HTMLElementTagNameMap ? Tag : never : never;
|
|
5
14
|
export {};
|
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createElement = createElement;
|
|
4
|
+
/**
|
|
5
|
+
* Create htmlelement with direct possibility of accessing the element and adding childnodes.
|
|
6
|
+
*
|
|
7
|
+
* example: createElement('div', e => (e.textContent = 'Sometext'), [createElement('a{link text}[href=link-url]')])
|
|
8
|
+
* @param basicEmmet div.class1.class-2#my-id[attribute1 attribute2=somevalue]
|
|
9
|
+
* @param listOrFn optional - childList or function which gets the element as input
|
|
10
|
+
* @param list optional - childList
|
|
11
|
+
* @returns HtmlElement
|
|
12
|
+
*/
|
|
4
13
|
function createElement(basicEmmet, listOrFn, list) {
|
|
5
14
|
const elm = emmetCreateElement(basicEmmet);
|
|
6
15
|
if (!elm)
|
|
@@ -15,20 +24,6 @@ function createElement(basicEmmet, listOrFn, list) {
|
|
|
15
24
|
elm.append(...list.map(i => (typeof i === 'function' ? i() : i)));
|
|
16
25
|
return elm;
|
|
17
26
|
}
|
|
18
|
-
function emmetCreateElement2(str) {
|
|
19
|
-
const matches = str.match(/([a-z-]+)(#[a-z1-9-]+)?((\.[a-z1-9-]+)*)(\{.+\})?/);
|
|
20
|
-
if (!matches)
|
|
21
|
-
return;
|
|
22
|
-
const [, tagName, id, classnames, , textContent] = matches;
|
|
23
|
-
const element = document.createElement(tagName);
|
|
24
|
-
if (id)
|
|
25
|
-
element.id = id.substring(1);
|
|
26
|
-
if (classnames)
|
|
27
|
-
element.className = classnames.substring(1).split('.').join(' ');
|
|
28
|
-
if (textContent)
|
|
29
|
-
element.textContent = textContent.substring(1, textContent.length - 1);
|
|
30
|
-
return element;
|
|
31
|
-
}
|
|
32
27
|
function emmetCreateElement(input) {
|
|
33
28
|
var _a, _b, _c, _d, _e;
|
|
34
29
|
const [, tagname, , modifier] = (_a = input.match(/^([a-z-]+)(:([a-z-]+))?/)) !== null && _a !== void 0 ? _a : [];
|
|
@@ -41,7 +36,7 @@ function emmetCreateElement(input) {
|
|
|
41
36
|
const [, text] = (_c = input.match(/{(.*)}/)) !== null && _c !== void 0 ? _c : [];
|
|
42
37
|
if (text)
|
|
43
38
|
element.textContent = text;
|
|
44
|
-
const classnames = [...input.matchAll(/\.([a-z-0-9]*)/g)];
|
|
39
|
+
const classnames = [...input.matchAll(/\.([a-z-0-9:]*)/g)];
|
|
45
40
|
if (classnames.length)
|
|
46
41
|
element.className = classnames.map(cn => cn[1]).join(' ');
|
|
47
42
|
const [, allAttributes] = (_d = input.match(/\[(.*?)\]/)) !== null && _d !== void 0 ? _d : [];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-element.js","sourceRoot":"","sources":["../../../src/functions/other/create-element.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"create-element.js","sourceRoot":"","sources":["../../../src/functions/other/create-element.ts"],"names":[],"mappings":";;AAWA,sCAsBC;AA/BD;;;;;;;;GAQG;AACH,SAAgB,aAAa,CAK3B,UAAa,EACb,QAE4D,EAC5D,IAA8D;IAE9D,MAAM,GAAG,GAAG,kBAAkB,CAAC,UAAU,CAAC,CAAC;IAC3C,IAAI,CAAC,GAAG;QAAE,MAAM,IAAI,KAAK,CAAC,mBAAmB,UAAU,EAAE,CAAC,CAAC;IAC3D,IAAI,QAAQ,KAAK,SAAS,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;QAChD,IAAI,OAAO,QAAQ,KAAK,UAAU;YAAE,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;;YAE3D,GAAG,CAAC,MAAM,CACR,GAAG,CAAC,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAClE,CAAC;IACN,CAAC;IACD,IAAI,IAAI;QAAE,GAAG,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC5E,OAAO,GAAG,CAAC;AACb,CAAC;AAED,SAAS,kBAAkB,CAAmB,KAAQ;;IACpD,M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AI,EAAE,AAAD,EAAG,MAAM,EAAE,QAAQ,CAAC,EAAE,EAAE,WAC/B,OAAA,CAAE,OAAe,CAAC,IAAI,CAAC,GAAG,MAAA,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,QAAQ,mCAAI,IAAI,CAAC,CAAA,EAAA,CACxD,CAAC;IAEF,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,SAAS,qBAAqB,CAAC,GAAgB,EAAE,SAAiB;;IAChE,IAAI,GAAG,YAAY,gBAAgB,EAAE,CAAC;QACpC,MAAM,MAAM,GAA8B;YACxC,CAAC,EAAE,QAAQ;YACX,CAAC,EAAE,UAAU;YACb,CAAC,EAAE,MAAM;YACT,CAAC,EAAE,QAAQ;YACX,CAAC,EAAE,OAAO;YACV,CAAC,EAAE,OAAO;YACV,CAAC,EAAE,QAAQ;YACX,CAAC,EAAE,MAAM;YACT,CAAC,EAAE,UAAU;SACd,CAAC;QAEF,GAAG,CAAC,IAAI,GAAG,MAAA,MAAM,CAAC,SAAS,CAAC,mCAAI,SAAS,CAAC;IAC5C,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rickard.antonsson/ts-utility",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.3",
|
|
4
4
|
"description": "My favorite small helper functions",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -9,16 +9,6 @@
|
|
|
9
9
|
],
|
|
10
10
|
"license": "Apache-2.0",
|
|
11
11
|
"keywords": [],
|
|
12
|
-
"scripts": {
|
|
13
|
-
"test": "jest",
|
|
14
|
-
"lint": "gts lint",
|
|
15
|
-
"clean": "gts clean",
|
|
16
|
-
"compile": "tsc",
|
|
17
|
-
"fix": "gts fix",
|
|
18
|
-
"prepare": "pnpm run compile",
|
|
19
|
-
"pretest": "pnpm run compile",
|
|
20
|
-
"posttest": "pnpm run lint"
|
|
21
|
-
},
|
|
22
12
|
"devDependencies": {
|
|
23
13
|
"@babel/core": "^7.16.0",
|
|
24
14
|
"@babel/preset-env": "^7.23.3",
|
|
@@ -34,5 +24,14 @@
|
|
|
34
24
|
},
|
|
35
25
|
"dependencies": {
|
|
36
26
|
"@rickard.antonsson/ts-utility": "file:"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"test": "jest",
|
|
30
|
+
"lint": "gts lint",
|
|
31
|
+
"clean": "gts clean",
|
|
32
|
+
"compile": "tsc",
|
|
33
|
+
"fix": "gts fix",
|
|
34
|
+
"pretest": "pnpm run compile",
|
|
35
|
+
"posttest": "pnpm run lint"
|
|
37
36
|
}
|
|
38
|
-
}
|
|
37
|
+
}
|