@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/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@thednp/shorty",
3
- "version": "2.0.0alpha5",
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": "^10.6.0",
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' ? parseFloat(delayValue) * delayScale : 0;
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' ? parseFloat(durationValue) * durationScale : 0;
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,
@@ -4,5 +4,5 @@ export interface CustomElement extends HTMLElement {
4
4
  disconnectedCallback?: () => void;
5
5
  adoptedCallback?: () => void;
6
6
  attributeChangedCallback?: () => void;
7
- [key: string]: any;
7
+ // [key: PropertyKey]: any;
8
8
  }
package/src/is/isArray.ts CHANGED
@@ -4,6 +4,6 @@
4
4
  * @param obj array-like iterable object
5
5
  * @returns the query result
6
6
  */
7
- const isArray = (obj?: any): obj is any[] => Array.isArray(obj) || false;
7
+ const isArray = (obj?: unknown): obj is any[] => Array.isArray(obj) || false;
8
8
 
9
9
  export default isArray;
@@ -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?: Node): element is HTMLCanvasElement =>
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?: T | Node): element is T =>
10
- (element && !!(element as CustomElement).shadowRoot) || false;
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;
@@ -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?: Node | Document): obj is Document => (obj && obj.nodeType === 9) || false;
11
+ const isDocument = (obj?: unknown): obj is Document => (isNode(obj) && obj.nodeType === 9) || false;
10
12
 
11
13
  export default isDocument;
@@ -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?: Element | Node): element is 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?: any): obj is HTMLElement[] =>
10
+ const isElementsArray = (obj?: unknown): obj is HTMLElement[] =>
11
11
  (isArray(obj) && obj.every(isHTMLElement)) || false;
12
12
 
13
13
  export default isElementsArray;
@@ -4,7 +4,7 @@
4
4
  * @param fn the target object
5
5
  * @returns the query result
6
6
  */
7
- const isFunction = <T extends (...arg0: any[]) => any>(fn?: T): fn is T =>
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?: HTMLCollection): obj is HTMLCollection =>
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;
@@ -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?: HTMLElement | Node): element is HTMLElement =>
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?: HTMLImageElement): element is HTMLImageElement =>
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 = <T extends Map<any, any>>(obj?: T): obj is T =>
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?: SVGElement | HTMLImageElement | HTMLVideoElement | HTMLCanvasElement,
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
- const isNode = (node?: Node): 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,7 +7,7 @@
5
7
  * @param obj the target object
6
8
  * @returns the query result
7
9
  */
8
- const isNodeList = (obj?: NodeList): obj is NodeList =>
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;
@@ -4,6 +4,6 @@
4
4
  * @param num input value
5
5
  * @returns the query result
6
6
  */
7
- const isNumber = (num?: number): 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?: object): 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;
@@ -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?: SVGElement): 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;
@@ -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?: Node | ShadowRoot): 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?: string): 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,9 +8,7 @@
6
8
  * @param element the target element
7
9
  * @returns the query result
8
10
  */
9
- const isTableElement = (
10
- element?: HTMLTableElement | HTMLTableCellElement,
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;
@@ -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 = <T extends WeakMap<any, unknown>>(obj?: T): obj is T =>
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;
@@ -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?: Node | Window): obj is Window =>
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 | Exclude<HTMLElement, ((...args: any[]) => any) | object>,
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
- type MConstructor<T extends new (...args: any[]) => any> = new (
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 extends MConstructor<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, InstanceType<T>>());
23
+ componentData.set(component, new Map<HTMLElement, T>());
32
24
  }
33
25
 
34
- const instanceMap = componentData.get(component) as Map<HTMLElement, InstanceType<T>>;
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 extends MConstructor<T>>(
46
- component: T,
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 extends MConstructor<T>>(element: HTMLElement, component: T): InstanceType<T> | null => {
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 extends MConstructor<T>>(element: HTMLElement, component: T): void => {
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 extends MConstructor<T>>(
92
- target: HTMLElement,
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: Node | string, parent?: ParentNode): HTMLElement | null => {
13
- if (isNode(selector as Node)) {
14
- return selector as HTMLElement;
13
+ const querySelector = (selector: HTMLElement | string, parent?: ParentNode): HTMLElement | null => {
14
+ if (isHTMLElement(selector)) {
15
+ return selector;
15
16
  }
16
- const lookUp = parent && isNode(parent) ? parent : getDocument();
17
+ const lookUp = isNode(parent) ? parent : getDocument();
17
18
 
18
- return lookUp.querySelector(selector as string);
19
+ return lookUp.querySelector(selector);
19
20
  };
20
21
 
21
22
  export default querySelector;