@thednp/shorty 2.0.0-alpha4 → 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.
Files changed (86) hide show
  1. package/README.md +8 -3
  2. package/dist/shorty.cjs +1 -1
  3. package/dist/shorty.cjs.map +1 -1
  4. package/dist/shorty.d.ts +59 -53
  5. package/dist/shorty.js +1 -1
  6. package/dist/shorty.js.map +1 -1
  7. package/dist/shorty.mjs +305 -306
  8. package/dist/shorty.mjs.map +1 -1
  9. package/package.json +12 -6
  10. package/src/attr/getAttribute.ts +3 -1
  11. package/src/attr/getAttributeNS.ts +3 -1
  12. package/src/attr/hasAttribute.ts +1 -0
  13. package/src/attr/hasAttributeNS.ts +3 -1
  14. package/src/attr/removeAttribute.ts +1 -0
  15. package/src/attr/removeAttributeNS.ts +3 -1
  16. package/src/attr/setAttribute.ts +3 -1
  17. package/src/attr/setAttributeNS.ts +1 -0
  18. package/src/boolean/isMobile.ts +1 -1
  19. package/src/boolean/support3DTransform.ts +1 -1
  20. package/src/boolean/supportAnimation.ts +1 -1
  21. package/src/boolean/supportPassive.ts +1 -1
  22. package/src/boolean/supportTouch.ts +2 -1
  23. package/src/boolean/supportTransform.ts +1 -1
  24. package/src/boolean/supportTransition.ts +1 -1
  25. package/src/event/off.ts +5 -3
  26. package/src/event/on.ts +5 -3
  27. package/src/event/one.ts +7 -2
  28. package/src/get/getBoundingClientRect.ts +5 -1
  29. package/src/get/getDocument.ts +4 -3
  30. package/src/get/getDocumentBody.ts +1 -1
  31. package/src/get/getElementAnimationDelay.ts +2 -1
  32. package/src/get/getElementAnimationDuration.ts +2 -1
  33. package/src/get/getElementTransitionDelay.ts +4 -1
  34. package/src/get/getElementTransitionDuration.ts +4 -1
  35. package/src/get/getParentNode.ts +2 -1
  36. package/src/get/getRectRelativeToOffsetParent.ts +1 -0
  37. package/src/get/getUID.ts +2 -2
  38. package/src/get/getWindow.ts +1 -0
  39. package/src/index.ts +12 -2
  40. package/src/interface/customElement.ts +1 -1
  41. package/src/interface/originalEvent.ts +2 -1
  42. package/src/is/isArray.ts +1 -1
  43. package/src/is/isCanvas.ts +4 -2
  44. package/src/is/isCustomElement.ts +3 -2
  45. package/src/is/isDocument.ts +4 -1
  46. package/src/is/isElement.ts +5 -2
  47. package/src/is/isElementsArray.ts +2 -1
  48. package/src/is/isFunction.ts +2 -1
  49. package/src/is/isHTMLCollection.ts +4 -2
  50. package/src/is/isHTMLElement.ts +5 -1
  51. package/src/is/isHTMLImageElement.ts +5 -2
  52. package/src/is/isJSON.ts +1 -1
  53. package/src/is/isMap.ts +4 -1
  54. package/src/is/isMedia.ts +8 -4
  55. package/src/is/isNode.ts +9 -3
  56. package/src/is/isNodeList.ts +4 -1
  57. package/src/is/isNumber.ts +1 -1
  58. package/src/is/isObject.ts +2 -1
  59. package/src/is/isRTL.ts +1 -0
  60. package/src/is/isSVGElement.ts +4 -2
  61. package/src/is/isScaledElement.ts +5 -2
  62. package/src/is/isShadowRoot.ts +4 -2
  63. package/src/is/isString.ts +1 -1
  64. package/src/is/isTableElement.ts +4 -2
  65. package/src/is/isWeakMap.ts +4 -1
  66. package/src/is/isWindow.ts +4 -1
  67. package/src/misc/Float32ArrayFrom.ts +1 -0
  68. package/src/misc/Float64ArrayFrom.ts +1 -0
  69. package/src/misc/ObjectAssign.ts +35 -3
  70. package/src/misc/ObjectEntries.ts +3 -1
  71. package/src/misc/ObjectKeys.ts +2 -1
  72. package/src/misc/ObjectValues.ts +3 -1
  73. package/src/misc/{OriginalEvent.ts → createCustomEvent.ts} +9 -5
  74. package/src/misc/createElement.ts +7 -6
  75. package/src/misc/createElementNS.ts +9 -5
  76. package/src/misc/data.ts +22 -15
  77. package/src/misc/distinct.ts +1 -0
  78. package/src/misc/hasOwn.ts +17 -0
  79. package/src/misc/normalizeValue.ts +9 -7
  80. package/src/misc/setElementStyle.ts +3 -2
  81. package/src/misc/timer.ts +16 -13
  82. package/src/selectors/getCustomElements.ts +1 -0
  83. package/src/selectors/getElementsByClassName.ts +7 -2
  84. package/src/selectors/getElementsByTagName.ts +4 -1
  85. package/src/selectors/querySelector.ts +5 -4
  86. package/src/strings/mouseHoverEvents.ts +3 -1
package/src/is/isJSON.ts CHANGED
@@ -6,7 +6,7 @@ import isString from './isString';
6
6
  * @param str the target string
7
7
  * @returns the query result
8
8
  */
9
- const isJSON = (str?: any): boolean => {
9
+ const isJSON = (str?: string): boolean => {
10
10
  if (!isString(str)) return false;
11
11
 
12
12
  try {
package/src/is/isMap.ts CHANGED
@@ -1,8 +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 = (obj?: any): obj is Map<any, any> => (obj && obj.constructor.name === 'Map') || false;
9
+ const isMap = (obj?: unknown): obj is Map<any, any> =>
10
+ (isObject(obj) && obj.constructor.name === 'Map') || false;
8
11
  export default isMap;
package/src/is/isMedia.ts CHANGED
@@ -1,16 +1,20 @@
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>`.
4
6
  *
5
7
  * *Tooltip* / *Popover* works different with media elements.
8
+ *
6
9
  * @param element the target element
7
10
  * @returns the query result
8
11
  */
9
12
 
10
- const isMedia = (element?: any): element is SVGElement | HTMLImageElement | HTMLVideoElement | HTMLCanvasElement =>
11
- (element &&
12
- element.nodeType === 1 &&
13
- ['SVG', 'Image', 'Video', 'Canvas'].some((s) => element.constructor.name.includes(s))) ||
13
+ const isMedia = (
14
+ element?: unknown,
15
+ ): element is SVGElement | HTMLImageElement | HTMLVideoElement | HTMLCanvasElement =>
16
+ (isNode(element) &&
17
+ ['SVG', 'Image', 'Video', 'Canvas'].some(s => element.constructor.name.includes(s))) ||
14
18
  false;
15
19
 
16
20
  export default isMedia;
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
- const isNode = (node?: any): node is Node =>
9
- (node && [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].some((x) => node.nodeType === x)) || false;
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;
@@ -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,6 +7,7 @@
5
7
  * @param obj the target object
6
8
  * @returns the query result
7
9
  */
8
- const isNodeList = (obj?: any): obj is NodeList => (obj && obj.constructor.name === 'NodeList') || false;
10
+ const isNodeList = (obj?: unknown): obj is NodeList =>
11
+ (isObject(obj) && obj.constructor.name === 'NodeList') || false;
9
12
 
10
13
  export default isNodeList;
@@ -4,6 +4,6 @@
4
4
  * @param num input value
5
5
  * @returns the query result
6
6
  */
7
- const isNumber = (num?: any): num is number => typeof num === 'number' || false;
7
+ const isNumber = (num?: unknown): num is number => typeof num === 'number' || false;
8
8
 
9
9
  export default isNumber;
@@ -4,6 +4,7 @@
4
4
  * @param obj the target object
5
5
  * @returns the query result
6
6
  */
7
- const isObject = (obj?: any): obj is object => typeof obj === 'object' || false;
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/isRTL.ts CHANGED
@@ -2,6 +2,7 @@ import getDocumentElement from '../get/getDocumentElement';
2
2
 
3
3
  /**
4
4
  * Checks if a page is Right To Left.
5
+ *
5
6
  * @param node the target
6
7
  * @returns the query result
7
8
  */
@@ -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?: any): element is SVGElement =>
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;
@@ -3,14 +3,17 @@ import getBoundingClientRect from '../get/getBoundingClientRect';
3
3
 
4
4
  /**
5
5
  * Checks if a target `HTMLElement` is affected by scale.
6
+ *
6
7
  * @see https://github.com/floating-ui/floating-ui
7
8
  *
8
9
  * @param element target
9
10
  * @returns the query result
10
11
  */
11
- export default function isScaledElement(element?: HTMLElement): boolean {
12
+ const isScaledElement = (element?: HTMLElement): boolean => {
12
13
  if (!isHTMLElement(element)) return false;
13
14
  const { width, height } = getBoundingClientRect(element);
14
15
  const { offsetWidth, offsetHeight } = element;
15
16
  return Math.round(width) !== offsetWidth || Math.round(height) !== offsetHeight;
16
- }
17
+ };
18
+
19
+ export default isScaledElement;
@@ -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?: any): element is ShadowRoot =>
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;
@@ -4,6 +4,6 @@
4
4
  * @param str input value
5
5
  * @returns the query result
6
6
  */
7
- const isString = (str?: any): str is string => typeof str === 'string' || false;
7
+ const isString = (str?: unknown): str is string => typeof str === 'string' || false;
8
8
 
9
9
  export default isString;
@@ -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,7 +8,7 @@
6
8
  * @param element the target element
7
9
  * @returns the query result
8
10
  */
9
- const isTableElement = (element?: any): element is HTMLTableElement | HTMLTableCellElement =>
10
- (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;
11
13
 
12
14
  export default isTableElement;
@@ -1,8 +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 = (obj?: any): obj is WeakMap<any, any> => (obj && obj.constructor.name === 'WeakMap') || false;
9
+ const isWeakMap = (obj?: unknown): obj is WeakMap<any, any> =>
10
+ (isObject(obj) && obj.constructor.name === 'WeakMap') || false;
8
11
  export default isWeakMap;
@@ -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,6 +7,7 @@
5
7
  * @param obj the target object
6
8
  * @returns the query result
7
9
  */
8
- const isWindow = (obj?: any): obj is Window => (obj && obj.constructor.name === 'Window') || false;
10
+ const isWindow = (obj?: unknown): obj is Window =>
11
+ (isObject(obj) && obj.constructor.name === 'Window') || false;
9
12
 
10
13
  export default isWindow;
@@ -1,5 +1,6 @@
1
1
  /**
2
2
  * Shortcut for `Float32Array.from()` static method.
3
+ *
3
4
  * @param arr array-like iterable object
4
5
  * @returns a new Float32Array
5
6
  */
@@ -1,5 +1,6 @@
1
1
  /**
2
2
  * Shortcut for `Float64Array.from()` static method.
3
+ *
3
4
  * @param arr array-like iterable object
4
5
  * @returns a new Float64Array
5
6
  */
@@ -1,11 +1,43 @@
1
+ /**
2
+ * Overloads for Object.assign.
3
+ *
4
+ * @see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/object-assign/index.d.ts
5
+ */
6
+ declare function ObjectAssignTyped<T, U>(target: T, source: U): T & U;
7
+ declare function ObjectAssignTyped<T, U, V>(target: T, source1: U, source2: V): T & U & V;
8
+ declare function ObjectAssignTyped<T, U, V, W>(
9
+ target: T,
10
+ source1: U,
11
+ source2: V,
12
+ source3: W,
13
+ ): T & U & V & W;
14
+ declare function ObjectAssignTyped<T, U, V, W, Q>(
15
+ target: T,
16
+ source1: U,
17
+ source2: V,
18
+ source3: W,
19
+ source4: Q,
20
+ ): T & U & V & W & Q;
21
+ declare function ObjectAssignTyped<T, U, V, W, Q, R>(
22
+ target: T,
23
+ source1: U,
24
+ source2: V,
25
+ source3: W,
26
+ source4: Q,
27
+ source5: R,
28
+ ): T & U & V & W & Q & R;
29
+ declare function ObjectAssignTyped(target: any, ...sources: any[]): any;
30
+
1
31
  /**
2
32
  * Shortcut for `Object.assign()` static method.
33
+ *
3
34
  * @param obj a target object
4
35
  * @param source source object(s)
5
36
  * @see https://github.com/devinrhode2/ObjectTyped/blob/master/src/index.ts
6
37
  */
7
-
8
- const ObjectAssign = <A extends {}, B>(obj: A, ...source: B[]): B extends any[] ? any : A & B =>
9
- Object.assign(obj, ...source);
38
+ const ObjectAssign: typeof ObjectAssignTyped = <A extends Record<string, any>, B>(
39
+ obj: A,
40
+ ...source: B[]
41
+ ): B extends Record<string, any>[] ? any : A & B => Object.assign(obj, ...source);
10
42
 
11
43
  export default ObjectAssign;
@@ -1,9 +1,11 @@
1
1
  /**
2
2
  * Shortcut for `Object.entries()` static method.
3
+ *
3
4
  * @param obj a target object
4
5
  * @returns the entries of an object in an array format [key, value][]
5
6
  * @see https://github.com/devinrhode2/ObjectTyped/blob/master/src/index.ts
6
7
  */
7
- const ObjectEntries = <O extends {}>(obj: O) => Object.entries(obj) as [keyof O, O[keyof O]][];
8
+ const ObjectEntries = <O extends Record<string, any>>(obj: O) =>
9
+ Object.entries(obj) as [keyof O, O[keyof O]][];
8
10
 
9
11
  export default ObjectEntries;
@@ -1,9 +1,10 @@
1
1
  /**
2
2
  * Shortcut for `Object.keys()` static method.
3
+ *
3
4
  * @param obj a target object
4
5
  * @returns an array with object keys
5
6
  * @see https://github.com/devinrhode2/ObjectTyped/blob/master/src/index.ts
6
7
  */
7
- const ObjectKeys = <O extends {}>(obj: O) => Object.keys(obj) as (keyof O)[];
8
+ const ObjectKeys = <O extends Record<string, any>>(obj: O) => Object.keys(obj) as (keyof O)[];
8
9
 
9
10
  export default ObjectKeys;
@@ -1,9 +1,11 @@
1
1
  /**
2
2
  * Shortcut for `Object.values()` static method.
3
+ *
3
4
  * @param obj a target object
4
5
  * @returns an array with the object values
5
6
  * @see https://github.com/devinrhode2/ObjectTyped/blob/master/src/index.ts
6
7
  */
7
- const ObjectValues = <O extends {}>(obj: O) => Object.values(obj) as (O[keyof O])[];
8
+ const ObjectValues = <O extends Record<string, unknown>>(obj: O): O[keyof O][] =>
9
+ Object.values(obj) as O[keyof O][];
8
10
 
9
11
  export default ObjectValues;
@@ -4,15 +4,19 @@ import ObjectAssign from './ObjectAssign';
4
4
 
5
5
  /**
6
6
  * Returns a namespaced `CustomEvent` specific to each component.
7
- * @param EventType Event.type
7
+ *
8
+ * @param eventType Event.type
8
9
  * @param config Event.options | Event.properties
9
10
  * @returns a new namespaced event
10
11
  */
11
- const OriginalEvent = <T>(EventType: string, config?: CustomEventInit<T>): OriginalEvent<T> => {
12
- const OriginalCustomEvent = new CustomEvent<T>(EventType, {
12
+ const createCustomEvent = <T extends OriginalEvent>(
13
+ eventType: string,
14
+ config?: CustomEventInit<any>,
15
+ ): T => {
16
+ const OriginalCustomEvent = new CustomEvent<any>(eventType, {
13
17
  cancelable: true,
14
18
  bubbles: true,
15
- } as CustomEventInit<T>);
19
+ } as CustomEventInit<any>) as T;
16
20
 
17
21
  /* istanbul ignore else */
18
22
  if (isObject(config)) {
@@ -21,4 +25,4 @@ const OriginalEvent = <T>(EventType: string, config?: CustomEventInit<T>): Origi
21
25
  return OriginalCustomEvent;
22
26
  };
23
27
 
24
- export default OriginalEvent;
28
+ export default createCustomEvent;
@@ -8,29 +8,30 @@ import ObjectEntries from './ObjectEntries';
8
8
  * which allows you to create a new `HTMLElement` for a given `tagName`
9
9
  * or based on an object with specific non-readonly attributes with string values:
10
10
  * `id`, `className`, `textContent`, `style`, etc.
11
+ *
11
12
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement
12
13
  *
13
14
  * @param param `tagName` or object
14
- * @return a new `HTMLElement` or `Element`
15
+ * @return a new `HTMLElement`
15
16
  */
16
17
  const createElement = (param?: string | Partial<HTMLElement>): HTMLElement | undefined => {
17
18
  if (!param) return undefined;
18
19
 
19
- if (typeof param === 'string') {
20
+ if (isString(param)) {
20
21
  return getDocument().createElement(param);
21
22
  }
22
23
 
23
24
  const { tagName } = param;
24
- const newElement = createElement(tagName);
25
+ const newElement = createElement(tagName as string);
25
26
 
26
27
  if (!newElement) return undefined;
27
28
 
28
- const attr = { ...param };
29
+ const attr = { ...(param as Record<string, unknown>) };
29
30
  delete attr.tagName;
30
31
 
31
32
  ObjectEntries(attr).forEach(([key, value]) => {
32
- if (isString(value)) {
33
- setAttribute(newElement, key, value);
33
+ if (isString(key as string) && isString(value as string)) {
34
+ setAttribute(newElement, key as string, value as string);
34
35
  }
35
36
  });
36
37
  return newElement;
@@ -8,16 +8,20 @@ import isString from '../is/isString';
8
8
  * which allows you to create a new `HTMLElement` for a given `tagName`
9
9
  * or based on an object with specific non-readonly attributes with string values:
10
10
  * `id`, `className`, `textContent`, `style`, etc.
11
+ *
11
12
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS
12
13
  *
13
14
  * @param ns `namespaceURI` to associate with the new `HTMLElement`
14
15
  * @param param `tagName` or object
15
16
  * @return a new `HTMLElement`
16
17
  */
17
- const createElementNS = (ns: string, param?: string | Partial<HTMLElement>): HTMLElement | undefined => {
18
+ const createElementNS = (
19
+ ns: string,
20
+ param?: string | Partial<HTMLElement>,
21
+ ): HTMLElement | undefined => {
18
22
  if (!ns || !param) return undefined;
19
23
 
20
- if (typeof param === 'string') {
24
+ if (isString(param)) {
21
25
  return getDocument().createElementNS(ns, param) as HTMLElement;
22
26
  }
23
27
 
@@ -26,12 +30,12 @@ const createElementNS = (ns: string, param?: string | Partial<HTMLElement>): HTM
26
30
 
27
31
  if (!newElement) return undefined;
28
32
 
29
- const attr = { ...param };
33
+ const attr = { ...(param as Record<string, unknown>) };
30
34
  delete attr.tagName;
31
35
 
32
36
  ObjectEntries(attr).forEach(([key, value]) => {
33
- if (isString(value)) {
34
- setAttribute(newElement, key, value);
37
+ if (isString(key as string) && isString(value as string)) {
38
+ setAttribute(newElement, key as string, value as string);
35
39
  }
36
40
  });
37
41
 
package/src/misc/data.ts CHANGED
@@ -1,18 +1,16 @@
1
1
  import isHTMLElement from '../is/isHTMLElement';
2
2
 
3
- type ComponentData = Record<string, any>;
4
- type ElementComponentMap = Map<HTMLElement, ComponentData>;
5
- type ComponentDataMap = Map<string, ElementComponentMap>;
6
-
7
- const componentData: ComponentDataMap = new Map();
3
+ const componentData = new Map<string, Map<HTMLElement, any>>();
8
4
 
9
5
  /**
10
6
  * An interface for web components background data.
7
+ *
11
8
  * @see https://github.com/thednp/bootstrap.native/blob/master/src/components/base-component.js
12
9
  */
13
10
  const Data = {
14
11
  /**
15
12
  * Sets web components data.
13
+ *
16
14
  * @param element target element
17
15
  * @param component the component's name or a unique key
18
16
  * @param instance the component instance
@@ -22,46 +20,54 @@ const Data = {
22
20
 
23
21
  /* istanbul ignore else */
24
22
  if (!componentData.has(component)) {
25
- componentData.set(component, new Map());
23
+ componentData.set(component, new Map<HTMLElement, T>());
26
24
  }
27
25
 
28
- const instanceMap = componentData.get(component);
26
+ const instanceMap = componentData.get(component) as Map<HTMLElement, T>;
29
27
  // not undefined, but defined right above
30
- (instanceMap as ElementComponentMap).set(element, instance);
28
+ instanceMap.set(element, instance);
31
29
  },
32
30
 
33
31
  /**
34
32
  * Returns all instances for specified component.
33
+ *
35
34
  * @param component the component's name or a unique key
36
35
  * @returns all the component instances
37
36
  */
38
- getAllFor: (component: string): ElementComponentMap | null => {
39
- const instanceMap = componentData.get(component);
37
+ getAllFor: <T>(component: string): Map<HTMLElement, T> | null => {
38
+ const instanceMap = componentData.get(component) as Map<HTMLElement, T>;
40
39
 
41
40
  return instanceMap || null;
42
41
  },
43
42
 
44
43
  /**
45
44
  * Returns the instance associated with the target.
45
+ *
46
46
  * @param element target element
47
47
  * @param component the component's name or a unique key
48
48
  * @returns the instance
49
49
  */
50
50
  get: <T>(element: HTMLElement, component: string): T | null => {
51
51
  if (!isHTMLElement(element) || !component) return null;
52
- const instanceMap = Data.getAllFor(component);
52
+ const instanceMap = Data.getAllFor<T>(component);
53
+ // const instanceMap = componentData.get(component) as Map<HTMLElement, InstanceType<T>>;
54
+
53
55
  const instance = element && instanceMap && instanceMap.get(element);
54
56
 
55
- return (instance as T) || null;
57
+ // return (instance as T) || null;
58
+ return instance || null;
56
59
  },
57
60
 
58
61
  /**
59
62
  * Removes web components data.
63
+ *
60
64
  * @param element target element
61
65
  * @param component the component's name or a unique key
62
66
  */
63
- remove: (element: HTMLElement, component: string): void => {
64
- const instanceMap = Data.getAllFor(component);
67
+ remove: <T>(element: HTMLElement, component: string): void => {
68
+ const instanceMap = Data.getAllFor<T>(component);
69
+ // const instanceMap = componentData.get(component) as Map<HTMLElement, InstanceType<T>>;
70
+
65
71
  if (!instanceMap || !isHTMLElement(element)) return;
66
72
 
67
73
  instanceMap.delete(element);
@@ -76,6 +82,7 @@ const Data = {
76
82
  /**
77
83
  * An alias for `Data.get()`.
78
84
  */
79
- export const getInstance = <T>(target: HTMLElement, component: string): T | null => Data.get<T>(target, component);
85
+ export const getInstance = <T>(target: HTMLElement, component: string): T | null =>
86
+ Data.get(target, component);
80
87
 
81
88
  export default Data;
@@ -1,5 +1,6 @@
1
1
  /**
2
2
  * JavaScript `Array` distinct.
3
+ *
3
4
  * @see https://codeburst.io/javascript-array-distinct-5edc93501dc4
4
5
  *
5
6
  * @example
@@ -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,28 +1,30 @@
1
+ type NormalValue = boolean | number | string | ((...args: any[]) => any) | null;
2
+
1
3
  /**
2
4
  * Utility to normalize component options
3
5
  *
4
6
  * @param value the input value
5
7
  * @return the normalized value
6
8
  */
7
- const normalizeValue = (value?: any): any => {
8
- if (['true', true].includes(value)) {
9
+ const normalizeValue = (value?: boolean | number | string): NormalValue => {
10
+ if (['true', true].includes(value as boolean)) {
9
11
  return true;
10
12
  }
11
13
 
12
- if (['false', false].includes(value)) {
14
+ if (['false', false].includes(value as boolean)) {
13
15
  return false;
14
16
  }
15
17
 
16
- if (['null', '', null].includes(value)) {
18
+ if (['null', '', null, undefined].includes(value as string | undefined)) {
17
19
  return null;
18
20
  }
19
21
 
20
- if (value !== '' && !Number.isNaN(+value)) {
21
- return +value;
22
+ if (value !== '' && !Number.isNaN(+(value as string))) {
23
+ return +(value as string);
22
24
  }
23
25
 
24
26
  // string / function / HTMLElement / object / undefined
25
- return value;
27
+ return value as NormalValue;
26
28
  };
27
29
 
28
30
  export default normalizeValue;
@@ -5,13 +5,14 @@ import isString from '../is/isString';
5
5
 
6
6
  /**
7
7
  * Shortcut for multiple uses of `HTMLElement.style.propertyName` method.
8
+ *
8
9
  * @param element target element
9
10
  * @param styles attribute value
10
11
  */
11
12
  const setElementStyle = (element: HTMLElement, styles: Partial<CSS4Declaration>): void => {
12
13
  ObjectEntries(styles).forEach(([key, value]) => {
13
- if (value && isString(key) && key.includes('--')) {
14
- element.style.setProperty(key, value);
14
+ if (value && isString(key as string) && (key as string).includes('--')) {
15
+ element.style.setProperty(key as string, value);
15
16
  } else {
16
17
  const propObject: Partial<CSS4Declaration> = {};
17
18
  propObject[key] = value;