@thednp/shorty 2.0.0-alpha5 → 2.0.0-alpha6
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/README.md +3 -2
- package/dist/shorty.cjs +1 -1
- package/dist/shorty.cjs.map +1 -1
- package/dist/shorty.d.ts +40 -40
- package/dist/shorty.js +1 -1
- package/dist/shorty.js.map +1 -1
- package/dist/shorty.mjs +278 -277
- package/dist/shorty.mjs.map +1 -1
- package/package.json +3 -3
- package/src/event/one.ts +1 -1
- package/src/get/getElementTransitionDelay.ts +3 -1
- package/src/get/getElementTransitionDuration.ts +3 -1
- package/src/index.ts +2 -0
- package/src/interface/customElement.ts +1 -1
- package/src/is/isArray.ts +1 -1
- package/src/is/isCanvas.ts +4 -2
- package/src/is/isCustomElement.ts +3 -2
- package/src/is/isDocument.ts +3 -1
- package/src/is/isElement.ts +4 -2
- package/src/is/isElementsArray.ts +1 -1
- package/src/is/isFunction.ts +1 -1
- package/src/is/isHTMLCollection.ts +4 -2
- package/src/is/isHTMLElement.ts +4 -2
- package/src/is/isHTMLImageElement.ts +4 -2
- package/src/is/isMap.ts +4 -2
- package/src/is/isMedia.ts +4 -3
- package/src/is/isNode.ts +9 -3
- package/src/is/isNodeList.ts +4 -2
- package/src/is/isNumber.ts +1 -1
- package/src/is/isObject.ts +2 -1
- package/src/is/isSVGElement.ts +4 -2
- package/src/is/isShadowRoot.ts +4 -2
- package/src/is/isString.ts +1 -1
- package/src/is/isTableElement.ts +4 -4
- package/src/is/isWeakMap.ts +4 -2
- package/src/is/isWindow.ts +4 -2
- package/src/misc/createElement.ts +1 -3
- package/src/misc/createElementNS.ts +1 -1
- package/src/misc/data.ts +14 -22
- package/src/misc/hasOwn.ts +17 -0
- package/src/selectors/querySelector.ts +6 -5
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thednp/shorty",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.0alpha6",
|
|
4
4
|
"description": "TypeScript shorties for the web",
|
|
5
5
|
"source": "./src/index.ts",
|
|
6
6
|
"main": "./dist/shorty.js",
|
|
7
7
|
"types": "./dist/shorty.d.ts",
|
|
8
8
|
"module": "./dist/shorty.mjs",
|
|
9
|
-
"exports": {
|
|
9
|
+
"*exports": {
|
|
10
10
|
".": {
|
|
11
11
|
"import": "./dist/shorty.mjs",
|
|
12
12
|
"require": "./dist/shorty.cjs",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"@types/istanbul-lib-instrument": "^1.7.4",
|
|
56
56
|
"@typescript-eslint/eslint-plugin": "^5.35.1",
|
|
57
57
|
"@typescript-eslint/parser": "^5.35.1",
|
|
58
|
-
"cypress": "^
|
|
58
|
+
"cypress": "^11.2.0",
|
|
59
59
|
"dts-bundle-generator": "^6.12.0",
|
|
60
60
|
"esbuild": "^0.14.30",
|
|
61
61
|
"eslint": "^8.23.0",
|
package/src/event/one.ts
CHANGED
|
@@ -14,7 +14,7 @@ const one = (
|
|
|
14
14
|
/** Wrap the listener for easy on -> off */
|
|
15
15
|
const handlerWrapper = (e: Event): void => {
|
|
16
16
|
/* istanbul ignore else */
|
|
17
|
-
if (e.target === element) {
|
|
17
|
+
if (e.target === element || e.currentTarget === element) {
|
|
18
18
|
listener.apply(element, [e]);
|
|
19
19
|
off(element, eventName, handlerWrapper, options);
|
|
20
20
|
}
|
|
@@ -14,7 +14,9 @@ const getElementTransitionDelay = (element: HTMLElement): number => {
|
|
|
14
14
|
const delayValue = getElementStyle(element, transitionDelay);
|
|
15
15
|
const delayScale = delayValue.includes('ms') ? /* istanbul ignore next */ 1 : 1000;
|
|
16
16
|
const duration =
|
|
17
|
-
propertyValue && propertyValue !== 'none'
|
|
17
|
+
propertyValue && propertyValue !== 'none'
|
|
18
|
+
? parseFloat(delayValue) * delayScale
|
|
19
|
+
: /* istanbul ignore next */ 0;
|
|
18
20
|
|
|
19
21
|
return !Number.isNaN(duration) ? duration : /* istanbul ignore next */ 0;
|
|
20
22
|
};
|
|
@@ -14,7 +14,9 @@ const getElementTransitionDuration = (element: HTMLElement): number => {
|
|
|
14
14
|
const durationValue = getElementStyle(element, transitionDuration);
|
|
15
15
|
const durationScale = durationValue.includes('ms') ? /* istanbul ignore next */ 1 : 1000;
|
|
16
16
|
const duration =
|
|
17
|
-
propertyValue && propertyValue !== 'none'
|
|
17
|
+
propertyValue && propertyValue !== 'none'
|
|
18
|
+
? parseFloat(durationValue) * durationScale
|
|
19
|
+
: /* istanbul ignore next */ 0;
|
|
18
20
|
|
|
19
21
|
return !Number.isNaN(duration) ? duration : /* istanbul ignore next */ 0;
|
|
20
22
|
};
|
package/src/index.ts
CHANGED
|
@@ -171,6 +171,7 @@ import Float32ArrayFrom from './misc/Float32ArrayFrom';
|
|
|
171
171
|
import Float64ArrayFrom from './misc/Float64ArrayFrom';
|
|
172
172
|
import focus from './misc/focus';
|
|
173
173
|
import noop from './misc/noop';
|
|
174
|
+
import hasOwn from './misc/hasOwn';
|
|
174
175
|
import normalizeOptions from './misc/normalizeOptions';
|
|
175
176
|
import normalizeValue from './misc/normalizeValue';
|
|
176
177
|
import ObjectAssign from './misc/ObjectAssign';
|
|
@@ -425,6 +426,7 @@ const SHORTY = {
|
|
|
425
426
|
getElementsByClassName,
|
|
426
427
|
getElementsByTagName,
|
|
427
428
|
matches,
|
|
429
|
+
hasOwn,
|
|
428
430
|
normalizeValue,
|
|
429
431
|
normalizeOptions,
|
|
430
432
|
reflow,
|
package/src/is/isArray.ts
CHANGED
package/src/is/isCanvas.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import isNode from './isNode';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Checks if an element is an `HTMLCanvasElement` or `<canvas>`.
|
|
3
5
|
*
|
|
@@ -5,7 +7,7 @@
|
|
|
5
7
|
* @returns the query result
|
|
6
8
|
*/
|
|
7
9
|
|
|
8
|
-
const isCanvas = (element?:
|
|
9
|
-
(element && element.nodeName === 'CANVAS') || false;
|
|
10
|
+
const isCanvas = (element?: unknown): element is HTMLCanvasElement =>
|
|
11
|
+
(isNode(element) && element.nodeName === 'CANVAS') || false;
|
|
10
12
|
|
|
11
13
|
export default isCanvas;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import isHTMLElement from './isHTMLElement';
|
|
1
2
|
import { CustomElement } from '../interface/customElement';
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -6,7 +7,7 @@ import { CustomElement } from '../interface/customElement';
|
|
|
6
7
|
* @param element the target object
|
|
7
8
|
* @returns the query result
|
|
8
9
|
*/
|
|
9
|
-
const isCustomElement = <T extends CustomElement>(element?:
|
|
10
|
-
(element && !!(element as
|
|
10
|
+
const isCustomElement = <T extends CustomElement>(element?: unknown): element is T =>
|
|
11
|
+
(isHTMLElement(element) && !!(element as T).shadowRoot) || false;
|
|
11
12
|
|
|
12
13
|
export default isCustomElement;
|
package/src/is/isDocument.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import isNode from './isNode';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Checks if an object is a `Document`.
|
|
3
5
|
*
|
|
@@ -6,6 +8,6 @@
|
|
|
6
8
|
* @param obj the target object
|
|
7
9
|
* @returns the query result
|
|
8
10
|
*/
|
|
9
|
-
const isDocument = (obj?:
|
|
11
|
+
const isDocument = (obj?: unknown): obj is Document => (isNode(obj) && obj.nodeType === 9) || false;
|
|
10
12
|
|
|
11
13
|
export default isDocument;
|
package/src/is/isElement.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import isNode from './isNode';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Checks if an object is an `Element`.
|
|
3
5
|
*
|
|
@@ -19,7 +21,7 @@
|
|
|
19
21
|
* @param element the target object
|
|
20
22
|
* @returns the query result
|
|
21
23
|
*/
|
|
22
|
-
const isElement = (element?:
|
|
23
|
-
(element && [1, 2, 3, 4, 5, 6, 7, 8].some(x => element.nodeType === x)) || false;
|
|
24
|
+
const isElement = (element?: unknown): element is Element =>
|
|
25
|
+
(isNode(element) && [1, 2, 3, 4, 5, 6, 7, 8].some(x => element.nodeType === x)) || false;
|
|
24
26
|
|
|
25
27
|
export default isElement;
|
|
@@ -7,7 +7,7 @@ import isArray from './isArray';
|
|
|
7
7
|
* @param obj the target object
|
|
8
8
|
* @returns the query result
|
|
9
9
|
*/
|
|
10
|
-
const isElementsArray = (obj?:
|
|
10
|
+
const isElementsArray = (obj?: unknown): obj is HTMLElement[] =>
|
|
11
11
|
(isArray(obj) && obj.every(isHTMLElement)) || false;
|
|
12
12
|
|
|
13
13
|
export default isElementsArray;
|
package/src/is/isFunction.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @param fn the target object
|
|
5
5
|
* @returns the query result
|
|
6
6
|
*/
|
|
7
|
-
const isFunction =
|
|
7
|
+
const isFunction = (fn?: unknown): fn is (...arg0: any[]) => any =>
|
|
8
8
|
typeof fn === 'function' || false;
|
|
9
9
|
|
|
10
10
|
export default isFunction;
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import isObject from './isObject';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Checks if an object is an `HTMLCollection`.
|
|
3
5
|
*
|
|
4
6
|
* @param obj the target object
|
|
5
7
|
* @returns the query result
|
|
6
8
|
*/
|
|
7
|
-
const isHTMLCollection = (obj?:
|
|
8
|
-
(obj && obj.constructor.name === 'HTMLCollection') || false;
|
|
9
|
+
const isHTMLCollection = (obj?: unknown): obj is HTMLCollection =>
|
|
10
|
+
(isObject(obj) && obj.constructor.name === 'HTMLCollection') || false;
|
|
9
11
|
|
|
10
12
|
export default isHTMLCollection;
|
package/src/is/isHTMLElement.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import isNode from './isNode';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Checks if an element is an `HTMLElement`.
|
|
3
5
|
*
|
|
@@ -6,6 +8,6 @@
|
|
|
6
8
|
* @param element the target object
|
|
7
9
|
* @returns the query result
|
|
8
10
|
*/
|
|
9
|
-
const isHTMLElement = (element?:
|
|
10
|
-
(element && element.nodeType === 1) || false;
|
|
11
|
+
const isHTMLElement = (element?: unknown): element is HTMLElement =>
|
|
12
|
+
(isNode(element) && element.nodeType === 1) || false;
|
|
11
13
|
export default isHTMLElement;
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import isHTMLElement from './isHTMLElement';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Check if a target element is an `<img>`.
|
|
3
5
|
*
|
|
4
6
|
* @param element the target element
|
|
5
7
|
* @returns the query result
|
|
6
8
|
*/
|
|
7
|
-
const isHTMLImageElement = (element?:
|
|
8
|
-
(element && element.tagName === 'IMG') || false;
|
|
9
|
+
const isHTMLImageElement = (element?: unknown): element is HTMLImageElement =>
|
|
10
|
+
(isHTMLElement(element) && element.tagName === 'IMG') || false;
|
|
9
11
|
|
|
10
12
|
export default isHTMLImageElement;
|
package/src/is/isMap.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import isObject from './isObject';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Checks if an element is a `Map`.
|
|
3
5
|
*
|
|
4
6
|
* @param obj the target object
|
|
5
7
|
* @returns the query result
|
|
6
8
|
*/
|
|
7
|
-
const isMap =
|
|
8
|
-
(obj && obj.constructor.name === 'Map') || false;
|
|
9
|
+
const isMap = (obj?: unknown): obj is Map<any, any> =>
|
|
10
|
+
(isObject(obj) && obj.constructor.name === 'Map') || false;
|
|
9
11
|
export default isMap;
|
package/src/is/isMedia.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import isNode from './isNode';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Checks if an element is an `<svg>` (or any type of SVG element),
|
|
3
5
|
* `<img>`, `<video>` or `<canvas>`.
|
|
@@ -9,10 +11,9 @@
|
|
|
9
11
|
*/
|
|
10
12
|
|
|
11
13
|
const isMedia = (
|
|
12
|
-
element?:
|
|
14
|
+
element?: unknown,
|
|
13
15
|
): element is SVGElement | HTMLImageElement | HTMLVideoElement | HTMLCanvasElement =>
|
|
14
|
-
(element &&
|
|
15
|
-
element.nodeType === 1 &&
|
|
16
|
+
(isNode(element) &&
|
|
16
17
|
['SVG', 'Image', 'Video', 'Canvas'].some(s => element.constructor.name.includes(s))) ||
|
|
17
18
|
false;
|
|
18
19
|
|
package/src/is/isNode.ts
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
|
+
import isObject from './isObject';
|
|
2
|
+
|
|
3
|
+
type NodeObject = object & { nodeType: number };
|
|
4
|
+
|
|
1
5
|
/**
|
|
2
6
|
* Checks if an object is a `Node`.
|
|
3
7
|
*
|
|
4
8
|
* @param node the target object
|
|
5
9
|
* @returns the query result
|
|
6
10
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
11
|
+
const isNode = (node?: unknown): node is Node =>
|
|
12
|
+
(isObject(node) &&
|
|
13
|
+
typeof (node as NodeObject).nodeType === 'number' &&
|
|
14
|
+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].some(x => (node as NodeObject).nodeType === x)) ||
|
|
15
|
+
false;
|
|
10
16
|
|
|
11
17
|
export default isNode;
|
package/src/is/isNodeList.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import isObject from './isObject';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Checks if an object is a `NodeList`.
|
|
3
5
|
* => equivalent to `object instanceof NodeList`
|
|
@@ -5,7 +7,7 @@
|
|
|
5
7
|
* @param obj the target object
|
|
6
8
|
* @returns the query result
|
|
7
9
|
*/
|
|
8
|
-
const isNodeList = (obj?:
|
|
9
|
-
(obj && obj.constructor.name === 'NodeList') || false;
|
|
10
|
+
const isNodeList = (obj?: unknown): obj is NodeList =>
|
|
11
|
+
(isObject(obj) && obj.constructor.name === 'NodeList') || false;
|
|
10
12
|
|
|
11
13
|
export default isNodeList;
|
package/src/is/isNumber.ts
CHANGED
package/src/is/isObject.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* @param obj the target object
|
|
5
5
|
* @returns the query result
|
|
6
6
|
*/
|
|
7
|
-
const isObject = (obj?:
|
|
7
|
+
const isObject = (obj?: unknown): obj is object =>
|
|
8
|
+
(obj !== null && obj !== undefined && typeof obj === 'object') || false;
|
|
8
9
|
|
|
9
10
|
export default isObject;
|
package/src/is/isSVGElement.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import isNode from './isNode';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Check if an element is an `<svg>` or any other SVG element,
|
|
3
5
|
* an equivalent to `SOMETHING instanceof SVGElement`.
|
|
@@ -5,7 +7,7 @@
|
|
|
5
7
|
* @param element the target element
|
|
6
8
|
* @returns the query result
|
|
7
9
|
*/
|
|
8
|
-
const isSVGElement = (element?:
|
|
9
|
-
(element && element.constructor.name.includes('SVG')) || false;
|
|
10
|
+
const isSVGElement = (element?: unknown): element is SVGElement =>
|
|
11
|
+
(isNode(element) && element.constructor.name.includes('SVG')) || false;
|
|
10
12
|
|
|
11
13
|
export default isSVGElement;
|
package/src/is/isShadowRoot.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import isNode from './isNode';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Check if target is a `ShadowRoot`.
|
|
3
5
|
*
|
|
4
6
|
* @param element target
|
|
5
7
|
* @returns the query result
|
|
6
8
|
*/
|
|
7
|
-
const isShadowRoot = (element?:
|
|
8
|
-
(element && element.constructor.name === 'ShadowRoot') || false;
|
|
9
|
+
const isShadowRoot = (element?: unknown): element is ShadowRoot =>
|
|
10
|
+
(isNode(element) && element.constructor.name === 'ShadowRoot') || false;
|
|
9
11
|
|
|
10
12
|
export default isShadowRoot;
|
package/src/is/isString.ts
CHANGED
package/src/is/isTableElement.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import isNode from './isNode';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Check if a target element is a `<table>`, `<td>` or `<th>`.
|
|
3
5
|
* This specific check is important for determining
|
|
@@ -6,9 +8,7 @@
|
|
|
6
8
|
* @param element the target element
|
|
7
9
|
* @returns the query result
|
|
8
10
|
*/
|
|
9
|
-
const isTableElement = (
|
|
10
|
-
element
|
|
11
|
-
): element is HTMLTableElement | HTMLTableCellElement =>
|
|
12
|
-
(element && ['TABLE', 'TD', 'TH'].includes(element.tagName)) || false;
|
|
11
|
+
const isTableElement = (element?: unknown): element is HTMLTableElement | HTMLTableCellElement =>
|
|
12
|
+
(isNode(element) && ['TABLE', 'TD', 'TH'].includes(element.nodeName)) || false;
|
|
13
13
|
|
|
14
14
|
export default isTableElement;
|
package/src/is/isWeakMap.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import isObject from './isObject';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Checks if an element is a `WeakMap`.
|
|
3
5
|
*
|
|
4
6
|
* @param obj the target object
|
|
5
7
|
* @returns the query result
|
|
6
8
|
*/
|
|
7
|
-
const isWeakMap =
|
|
8
|
-
(obj && obj.constructor.name === 'WeakMap') || false;
|
|
9
|
+
const isWeakMap = (obj?: unknown): obj is WeakMap<any, any> =>
|
|
10
|
+
(isObject(obj) && obj.constructor.name === 'WeakMap') || false;
|
|
9
11
|
export default isWeakMap;
|
package/src/is/isWindow.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import isObject from './isObject';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Check if a target object is `Window`.
|
|
3
5
|
* => equivalent to `object instanceof Window`
|
|
@@ -5,7 +7,7 @@
|
|
|
5
7
|
* @param obj the target object
|
|
6
8
|
* @returns the query result
|
|
7
9
|
*/
|
|
8
|
-
const isWindow = (obj?:
|
|
9
|
-
(obj && obj.constructor.name === 'Window') || false;
|
|
10
|
+
const isWindow = (obj?: unknown): obj is Window =>
|
|
11
|
+
(isObject(obj) && obj.constructor.name === 'Window') || false;
|
|
10
12
|
|
|
11
13
|
export default isWindow;
|
|
@@ -14,9 +14,7 @@ import ObjectEntries from './ObjectEntries';
|
|
|
14
14
|
* @param param `tagName` or object
|
|
15
15
|
* @return a new `HTMLElement`
|
|
16
16
|
*/
|
|
17
|
-
const createElement = (
|
|
18
|
-
param?: string | Exclude<HTMLElement, ((...args: any[]) => any) | object>,
|
|
19
|
-
): HTMLElement | undefined => {
|
|
17
|
+
const createElement = (param?: string | Partial<HTMLElement>): HTMLElement | undefined => {
|
|
20
18
|
if (!param) return undefined;
|
|
21
19
|
|
|
22
20
|
if (isString(param)) {
|
|
@@ -17,7 +17,7 @@ import isString from '../is/isString';
|
|
|
17
17
|
*/
|
|
18
18
|
const createElementNS = (
|
|
19
19
|
ns: string,
|
|
20
|
-
param?: string |
|
|
20
|
+
param?: string | Partial<HTMLElement>,
|
|
21
21
|
): HTMLElement | undefined => {
|
|
22
22
|
if (!ns || !param) return undefined;
|
|
23
23
|
|
package/src/misc/data.ts
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import isHTMLElement from '../is/isHTMLElement';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
...args: ConstructorParameters<T>
|
|
5
|
-
) => InstanceType<T>;
|
|
6
|
-
|
|
7
|
-
const componentData = new Map<MConstructor<any>, Map<HTMLElement, InstanceType<any>>>();
|
|
3
|
+
const componentData = new Map<string, Map<HTMLElement, any>>();
|
|
8
4
|
|
|
9
5
|
/**
|
|
10
6
|
* An interface for web components background data.
|
|
@@ -19,19 +15,15 @@ const Data = {
|
|
|
19
15
|
* @param component the component's name or a unique key
|
|
20
16
|
* @param instance the component instance
|
|
21
17
|
*/
|
|
22
|
-
set: <T
|
|
23
|
-
element: HTMLElement,
|
|
24
|
-
component: T,
|
|
25
|
-
instance: InstanceType<T>,
|
|
26
|
-
): void => {
|
|
18
|
+
set: <T>(element: HTMLElement, component: string, instance: T): void => {
|
|
27
19
|
if (!isHTMLElement(element)) return;
|
|
28
20
|
|
|
29
21
|
/* istanbul ignore else */
|
|
30
22
|
if (!componentData.has(component)) {
|
|
31
|
-
componentData.set(component, new Map<HTMLElement,
|
|
23
|
+
componentData.set(component, new Map<HTMLElement, T>());
|
|
32
24
|
}
|
|
33
25
|
|
|
34
|
-
const instanceMap = componentData.get(component) as Map<HTMLElement,
|
|
26
|
+
const instanceMap = componentData.get(component) as Map<HTMLElement, T>;
|
|
35
27
|
// not undefined, but defined right above
|
|
36
28
|
instanceMap.set(element, instance);
|
|
37
29
|
},
|
|
@@ -42,10 +34,8 @@ const Data = {
|
|
|
42
34
|
* @param component the component's name or a unique key
|
|
43
35
|
* @returns all the component instances
|
|
44
36
|
*/
|
|
45
|
-
getAllFor: <T
|
|
46
|
-
component
|
|
47
|
-
): Map<HTMLElement, InstanceType<T>> | null => {
|
|
48
|
-
const instanceMap = componentData.get(component) as Map<HTMLElement, InstanceType<T>>;
|
|
37
|
+
getAllFor: <T>(component: string): Map<HTMLElement, T> | null => {
|
|
38
|
+
const instanceMap = componentData.get(component) as Map<HTMLElement, T>;
|
|
49
39
|
|
|
50
40
|
return instanceMap || null;
|
|
51
41
|
},
|
|
@@ -57,9 +47,11 @@ const Data = {
|
|
|
57
47
|
* @param component the component's name or a unique key
|
|
58
48
|
* @returns the instance
|
|
59
49
|
*/
|
|
60
|
-
get: <T
|
|
50
|
+
get: <T>(element: HTMLElement, component: string): T | null => {
|
|
61
51
|
if (!isHTMLElement(element) || !component) return null;
|
|
62
52
|
const instanceMap = Data.getAllFor<T>(component);
|
|
53
|
+
// const instanceMap = componentData.get(component) as Map<HTMLElement, InstanceType<T>>;
|
|
54
|
+
|
|
63
55
|
const instance = element && instanceMap && instanceMap.get(element);
|
|
64
56
|
|
|
65
57
|
// return (instance as T) || null;
|
|
@@ -72,8 +64,10 @@ const Data = {
|
|
|
72
64
|
* @param element target element
|
|
73
65
|
* @param component the component's name or a unique key
|
|
74
66
|
*/
|
|
75
|
-
remove: <T
|
|
67
|
+
remove: <T>(element: HTMLElement, component: string): void => {
|
|
76
68
|
const instanceMap = Data.getAllFor<T>(component);
|
|
69
|
+
// const instanceMap = componentData.get(component) as Map<HTMLElement, InstanceType<T>>;
|
|
70
|
+
|
|
77
71
|
if (!instanceMap || !isHTMLElement(element)) return;
|
|
78
72
|
|
|
79
73
|
instanceMap.delete(element);
|
|
@@ -88,9 +82,7 @@ const Data = {
|
|
|
88
82
|
/**
|
|
89
83
|
* An alias for `Data.get()`.
|
|
90
84
|
*/
|
|
91
|
-
export const getInstance = <T
|
|
92
|
-
target
|
|
93
|
-
component: T,
|
|
94
|
-
): InstanceType<T> | null => Data.get<T>(target, component);
|
|
85
|
+
export const getInstance = <T>(target: HTMLElement, component: string): T | null =>
|
|
86
|
+
Data.get(target, component);
|
|
95
87
|
|
|
96
88
|
export default Data;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import isObject from '../is/isObject';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A shortcut to `Object.hasOwn()` static method to work
|
|
5
|
+
* with regular `Object` elements.
|
|
6
|
+
*
|
|
7
|
+
* @see https://fettblog.eu/typescript-hasownproperty/
|
|
8
|
+
* @param obj the target object
|
|
9
|
+
* @param prop the property to check
|
|
10
|
+
* @returns the query result
|
|
11
|
+
*/
|
|
12
|
+
const hasOwn = <T extends object, K extends PropertyKey>(
|
|
13
|
+
obj: T,
|
|
14
|
+
prop: K,
|
|
15
|
+
): obj is T & Record<K, unknown> => isObject(obj) && (Object.hasOwn(obj, prop) || prop in obj);
|
|
16
|
+
|
|
17
|
+
export default hasOwn;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import getDocument from '../get/getDocument';
|
|
2
2
|
import isNode from '../is/isNode';
|
|
3
|
+
import isHTMLElement from '../is/isHTMLElement';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Utility to check if target is typeof `HTMLElement`, `Element`, `Node`
|
|
@@ -9,13 +10,13 @@ import isNode from '../is/isNode';
|
|
|
9
10
|
* @param parent optional node to look into
|
|
10
11
|
* @return the `HTMLElement` or `querySelector` result
|
|
11
12
|
*/
|
|
12
|
-
const querySelector = (selector:
|
|
13
|
-
if (
|
|
14
|
-
return selector
|
|
13
|
+
const querySelector = (selector: HTMLElement | string, parent?: ParentNode): HTMLElement | null => {
|
|
14
|
+
if (isHTMLElement(selector)) {
|
|
15
|
+
return selector;
|
|
15
16
|
}
|
|
16
|
-
const lookUp =
|
|
17
|
+
const lookUp = isNode(parent) ? parent : getDocument();
|
|
17
18
|
|
|
18
|
-
return lookUp.querySelector(selector
|
|
19
|
+
return lookUp.querySelector(selector);
|
|
19
20
|
};
|
|
20
21
|
|
|
21
22
|
export default querySelector;
|