@oscarpalmer/toretto 0.31.0 → 0.33.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.
@@ -1,28 +1,57 @@
1
- import type {PlainObject} from '@oscarpalmer/atoms';
2
- import {isNullableOrWhitespace, isPlainObject} from '@oscarpalmer/atoms/is';
1
+ import {isNullableOrWhitespace} from '@oscarpalmer/atoms/is';
3
2
  import {isHTMLOrSVGElement} from '../is';
3
+ import {isAttribute} from './attribute';
4
4
 
5
- export function setElementValues(
5
+ export function setElementValue(
6
6
  element: Element,
7
- first: PlainObject | string,
7
+ first: unknown,
8
8
  second: unknown,
9
+ third: unknown,
9
10
  callback: (element: Element, key: string, value: unknown) => void,
10
11
  ): void {
11
12
  if (!isHTMLOrSVGElement(element)) {
12
13
  return;
13
14
  }
14
15
 
15
- if (isPlainObject(first)) {
16
- const entries = Object.entries(first);
17
- const {length} = entries;
16
+ if (typeof first === 'string') {
17
+ setElementValues(element, first, second, third, callback);
18
+ } else if (isAttribute(first)) {
19
+ setElementValues(element, first.name, first.value, third, callback);
20
+ }
21
+ }
22
+
23
+ export function setElementValues(
24
+ element: Element,
25
+ first: unknown,
26
+ second: unknown,
27
+ third: unknown,
28
+ callback: (element: Element, key: string, value: unknown, dispatch?: unknown) => void,
29
+ ): void {
30
+ if (!isHTMLOrSVGElement(element)) {
31
+ return;
32
+ }
33
+
34
+ if (typeof first === 'string') {
35
+ callback(element, first, second, third);
36
+
37
+ return;
38
+ }
39
+
40
+ const isArray = Array.isArray(first);
41
+
42
+ if (!isArray && !(typeof first === 'object' && first !== null)) {
43
+ return;
44
+ }
45
+
46
+ const entries = isArray ? first : Object.entries(first).map(([name, value]) => ({name, value}));
47
+ const {length} = entries;
18
48
 
19
- for (let index = 0; index < length; index += 1) {
20
- const [key, value] = entries[index];
49
+ for (let index = 0; index < length; index += 1) {
50
+ const entry = entries[index];
21
51
 
22
- callback(element, key, value);
52
+ if (typeof entry === 'object' && typeof entry?.name === 'string') {
53
+ callback(element, entry.name, entry.value, third);
23
54
  }
24
- } else if (typeof first === 'string') {
25
- callback(element, first, second);
26
55
  }
27
56
  }
28
57
 
@@ -32,9 +61,10 @@ export function updateElementValue(
32
61
  value: unknown,
33
62
  set: (key: string, value: string) => void,
34
63
  remove: (key: string) => void,
64
+ isBoolean: boolean,
35
65
  json: boolean,
36
66
  ): void {
37
- if (isNullableOrWhitespace(value)) {
67
+ if (isBoolean ? value == null : isNullableOrWhitespace(value)) {
38
68
  remove.call(element, key);
39
69
  } else {
40
70
  set.call(element, key, json ? JSON.stringify(value) : String(value));
package/src/models.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * Attribute for an element
3
3
  */
4
- export type Attribute<Value = unknown> = {
4
+ export type Attribute = {
5
5
  name: string;
6
- value: Value;
6
+ value: unknown;
7
7
  };
8
8
 
9
9
  /**
@@ -19,11 +19,6 @@ export type EventPosition = {
19
19
  y: number;
20
20
  };
21
21
 
22
- /**
23
- * Property for an element
24
- */
25
- export type Property = Attribute<unknown>;
26
-
27
22
  /**
28
23
  * Event listener that can be removed
29
24
  */
package/src/style.ts CHANGED
@@ -97,7 +97,7 @@ export function setStyle(
97
97
  property: keyof CSSStyleDeclaration,
98
98
  value?: string,
99
99
  ): void {
100
- setElementValues(element, property as string, value, updateStyleProperty);
100
+ setElementValues(element, property as string, value, null, updateStyleProperty);
101
101
  }
102
102
 
103
103
  /**
@@ -106,7 +106,7 @@ export function setStyle(
106
106
  * @param styles Styles to set
107
107
  */
108
108
  export function setStyles(element: Element, styles: Partial<CSSStyleDeclaration>): void {
109
- setElementValues(element, styles as never, null, updateStyleProperty);
109
+ setElementValues(element, styles as never, null, null, updateStyleProperty);
110
110
  }
111
111
 
112
112
  /**
@@ -169,6 +169,7 @@ function updateStyleProperty(element: Element, key: string, value: unknown): voi
169
169
  (this as HTMLElement).style[property as never] = '';
170
170
  },
171
171
  false,
172
+ false,
172
173
  );
173
174
  }
174
175
 
@@ -1,4 +1,15 @@
1
- import type { Attribute, Property } from '../models';
1
+ import type { Attribute } from '../models';
2
+ type DispatchedAttribute = 'checked' | 'open' | 'value';
3
+ /**
4
+ * Set an attribute on an element
5
+ *
6
+ * _(Or remove it, if value is `null` or `undefined`)_
7
+ * @param element Element for attribute
8
+ * @param name Attribute name
9
+ * @param value Attribute value
10
+ * @param dispatch Dispatch event for attribute? _(defaults to `true`)_
11
+ */
12
+ export declare function setAttribute<Name extends DispatchedAttribute>(element: Element, name: Name, value?: unknown, dispatch?: boolean): void;
2
13
  /**
3
14
  * Set an attribute on an element
4
15
  *
@@ -14,54 +25,25 @@ export declare function setAttribute(element: Element, name: string, value?: unk
14
25
  * _(Or remove it, if value is `null` or `undefined`)_
15
26
  * @param element Element for attribute
16
27
  * @param attribute Attribute to set
28
+ * @param dispatch Dispatch event for attribute? _(defaults to `true`)_
17
29
  */
18
- export declare function setAttribute(element: Element, attribute: Attr | Attribute): void;
30
+ export declare function setAttribute(element: Element, attribute: Attr | Attribute, dispatch?: boolean): void;
19
31
  /**
20
32
  * Set one or more attributes on an element
21
33
  *
22
34
  * _(Or remove them, if their value is `null` or `undefined`)_
23
35
  * @param element Element for attributes
24
36
  * @param attributes Attributes to set
37
+ * @param dispatch Dispatch events for relevant attributes? _(defaults to `true`)_
25
38
  */
26
- export declare function setAttributes(element: Element, attributes: Array<Attr | Attribute>): void;
39
+ export declare function setAttributes(element: Element, attributes: Array<Attr | Attribute>, dispatch?: boolean): void;
27
40
  /**
28
41
  * Set one or more attributes on an element
29
42
  *
30
43
  * _(Or remove them, if their value is `null` or `undefined`)_
31
44
  * @param element Element for attributes
32
45
  * @param attributes Attributes to set
46
+ * @param dispatch Dispatch events for relevant attributes? _(defaults to `true`)_
33
47
  */
34
- export declare function setAttributes(element: Element, attributes: Record<string, unknown>): void;
35
- /**
36
- * Set a property on an element
37
- *
38
- * _(Or remove it, if value is not an empty string or does not match the name)_
39
- * @param element Element for property
40
- * @param name Property name
41
- * @param value Property value
42
- */
43
- export declare function setProperty(element: Element, name: string, value: boolean | string): void;
44
- /**
45
- * Set a property on an element
46
- *
47
- * _(Or remove it, if value is not an empty string or does not match the name)_
48
- * @param element Element for property
49
- * @param property Property to set
50
- */
51
- export declare function setProperty(element: Element, property: Property): void;
52
- /**
53
- * Set one or more properties on an element
54
- *
55
- * _(Or remove them, if their value is not an empty string or does not match the name)_
56
- * @param element Element for properties
57
- * @param properties Properties to set
58
- */
59
- export declare function setProperties(element: Element, properties: Property[]): void;
60
- /**
61
- * Set one or more properties on an element
62
- *
63
- * _(Or remove them, if their value is not an empty string or does not match the name)_
64
- * @param element Element for properties
65
- * @param properties Properties to set
66
- */
67
- export declare function setProperties(element: Element, properties: Record<string, unknown>): void;
48
+ export declare function setAttributes(element: Element, attributes: Record<string, unknown>, dispatch?: boolean): void;
49
+ export {};
@@ -4,4 +4,4 @@ export type EventTargetWithListeners = EventTarget & Partial<{
4
4
  }>;
5
5
  export declare function addDelegatedListener(target: EventTargetWithListeners, type: string, name: string, listener: EventListener | CustomEventListener, passive: boolean): RemovableEventListener;
6
6
  export declare function getDelegatedName(target: EventTarget, type: string, options: AddEventListenerOptions): string | undefined;
7
- export declare function removeDelegatedListener(target: EventTargetWithListeners, type: string, name: string, listener: EventListener | CustomEventListener, passive: boolean): boolean;
7
+ export declare function removeDelegatedListener(target: EventTargetWithListeners, name: string, listener: EventListener | CustomEventListener): boolean;
@@ -23,4 +23,4 @@ export declare function findElements(selector: Selector, context?: Selector | nu
23
23
  */
24
24
  export declare function getElementUnderPointer(skipIgnore?: boolean): Element | null;
25
25
  export { findElement as $, findElements as $$ };
26
- export { findAncestor, findRelatives } from './relative';
26
+ export { findAncestor, findRelatives, getDistance } from './relative';
@@ -11,11 +11,17 @@ export declare function findAncestor(origin: Element, selector: string | ((eleme
11
11
  /**
12
12
  * Finds the closest elements to the origin element that matches the selector
13
13
  *
14
- * - Traverses up, down, and sideways in the _DOM_-tree
15
- * - _(If you only want to traverse up, use {@link findAncestor})_
14
+ * Traverses up, down, and sideways in the _DOM_-tree. _(If you only want to traverse up, use {@link findAncestor})_
16
15
  * @param origin Element to start from
17
16
  * @param selector Selector to match
18
17
  * @param context Context to search within
19
18
  * @returns Found elements
20
19
  */
21
20
  export declare function findRelatives(origin: Element, selector: string, context?: Document | Element): Element[];
21
+ /**
22
+ * Get the distance between two elements _(i.e., the amount of nodes of between them)_
23
+ * @param origin Origin element
24
+ * @param target Target element
25
+ * @returns Distance between elements, or `-1` if distance cannot be calculated
26
+ */
27
+ export declare function getDistance(origin: Element, target: Element): number;
package/types/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import supportsTouch from './touch';
2
- export { isBadAttribute, isBooleanAttribute, isEmptyNonBooleanAttribute, isInvalidBooleanAttribute, } from './attribute';
2
+ export { isBadAttribute, isBooleanAttribute, isEmptyNonBooleanAttribute, isInvalidBooleanAttribute, } from './attribute/index';
3
3
  export * from './data';
4
4
  export * from './event/index';
5
5
  export * from './find/index';
@@ -1,11 +1,10 @@
1
- import type { Attribute, Property } from '../models';
1
+ import type { Attribute } from '../models';
2
+ export declare function isAttribute(value: unknown): value is Attr | Attribute;
2
3
  export declare function _isBadAttribute(first: unknown, second: unknown, decode: boolean): boolean;
3
4
  export declare function _isBooleanAttribute(first: unknown, decode: boolean): boolean;
4
5
  export declare function _isEmptyNonBooleanAttribute(first: unknown, second: unknown, decode: boolean): boolean;
5
6
  export declare function _isInvalidBooleanAttribute(first: unknown, second: unknown, decode: boolean): boolean;
6
- export declare function isProperty(value: unknown): value is Property;
7
- export declare function updateValue(element: Element, first: unknown, second: unknown): void;
8
- export declare function updateValues(element: Element, values: Attribute<unknown>[] | Record<string, unknown>): void;
7
+ export declare function updateAttribute(element: Element, name: string, value: unknown, dispatch?: unknown): void;
9
8
  /**
10
9
  * List of boolean attributes
11
10
  */
@@ -1,3 +1,3 @@
1
- import type { PlainObject } from '@oscarpalmer/atoms';
2
- export declare function setElementValues(element: Element, first: PlainObject | string, second: unknown, callback: (element: Element, key: string, value: unknown) => void): void;
3
- export declare function updateElementValue(element: Element, key: string, value: unknown, set: (key: string, value: string) => void, remove: (key: string) => void, json: boolean): void;
1
+ export declare function setElementValue(element: Element, first: unknown, second: unknown, third: unknown, callback: (element: Element, key: string, value: unknown) => void): void;
2
+ export declare function setElementValues(element: Element, first: unknown, second: unknown, third: unknown, callback: (element: Element, key: string, value: unknown, dispatch?: unknown) => void): void;
3
+ export declare function updateElementValue(element: Element, key: string, value: unknown, set: (key: string, value: string) => void, remove: (key: string) => void, isBoolean: boolean, json: boolean): void;
package/types/models.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * Attribute for an element
3
3
  */
4
- export type Attribute<Value = unknown> = {
4
+ export type Attribute = {
5
5
  name: string;
6
- value: Value;
6
+ value: unknown;
7
7
  };
8
8
  /**
9
9
  * Event listener for custom events
@@ -16,10 +16,6 @@ export type EventPosition = {
16
16
  x: number;
17
17
  y: number;
18
18
  };
19
- /**
20
- * Property for an element
21
- */
22
- export type Property = Attribute<unknown>;
23
19
  /**
24
20
  * Event listener that can be removed
25
21
  */