@oscarpalmer/toretto 0.49.0 → 0.49.1

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/dist/aria.d.mts CHANGED
@@ -72,7 +72,7 @@ declare function setAria(element: Element, attributes: AriaAttributeItem[]): voi
72
72
  * @param element Element for _ARIA_ attributes
73
73
  * @param attributes _ARIA_ attributes to set
74
74
  */
75
- declare function setAria(element: Element, attributes: { [Key in AnyAriaAttribute]?: Key extends AnyAriaBooleanAttribute ? boolean | string : string; }): void;
75
+ declare function setAria(element: Element, attributes: Record<string, unknown>): void;
76
76
  /**
77
77
  * Set the role of an element
78
78
  *
package/dist/aria.mjs CHANGED
@@ -47,7 +47,7 @@ function updateAriaAttribute(element, key, value) {
47
47
  const name = getName(key);
48
48
  let actual = value;
49
49
  if (ariaBooleanAttributesSet.has(name) && typeof value === "string" && EXPRESSION_BOOLEAN.test(value)) actual = value.toLowerCase() === "true";
50
- updateElementValue(element, name, actual, element.setAttribute, element.removeAttribute, false, false);
50
+ updateElementValue(element, name, actual, element.setAttribute, element.removeAttribute, false);
51
51
  }
52
52
  const ATTRIBUTE_ARIA_PREFIX = "aria-";
53
53
  const EXPRESSION_ARIA_PREFIX = /^aria-/i;
package/dist/data.mjs CHANGED
@@ -6,21 +6,21 @@ import { camelCase, kebabCase } from "@oscarpalmer/atoms/string/case";
6
6
  function getData(element, keys, parseValues) {
7
7
  if (!(element instanceof Element)) return;
8
8
  const noParse = parseValues === false;
9
- if (typeof keys === "string") {
10
- const value = element.dataset[camelCase(keys)];
11
- if (value === void 0) return;
12
- return noParse ? value : parse(value);
13
- }
9
+ if (typeof keys === "string") return getDataValue(element, keys, noParse);
14
10
  const { length } = keys;
15
11
  const data = {};
16
12
  for (let index = 0; index < length; index += 1) {
17
13
  const key = keys[index];
18
- const value = element.dataset[camelCase(key)];
19
- if (value == null) data[key] = void 0;
20
- else data[key] = noParse ? value : parse(value);
14
+ data[key] = getDataValue(element, key, noParse);
21
15
  }
22
16
  return data;
23
17
  }
18
+ function getDataValue(element, key, noParse) {
19
+ const value = element.dataset[camelCase(key)];
20
+ if (value == null) return;
21
+ if (noParse) return value;
22
+ return parse(value) ?? value;
23
+ }
24
24
  function getName(original) {
25
25
  return `${ATTRIBUTE_DATA_PREFIX}${kebabCase(original.replace(EXPRESSION_DATA_PREFIX, ""))}`;
26
26
  }
@@ -28,7 +28,7 @@ function setData(element, first, second) {
28
28
  setElementValues(element, first, second, null, updateDataAttribute);
29
29
  }
30
30
  function updateDataAttribute(element, key, value) {
31
- updateElementValue(element, getName(key), value, element.setAttribute, element.removeAttribute, false, true);
31
+ updateElementValue(element, getName(key), value, element.setAttribute, element.removeAttribute, true);
32
32
  }
33
33
  const ATTRIBUTE_DATA_PREFIX = "data-";
34
34
  //#endregion
package/dist/index.d.mts CHANGED
@@ -168,7 +168,7 @@ declare function setAria(element: Element, attributes: AriaAttributeItem[]): voi
168
168
  * @param element Element for _ARIA_ attributes
169
169
  * @param attributes _ARIA_ attributes to set
170
170
  */
171
- declare function setAria(element: Element, attributes: { [Key in AnyAriaAttribute]?: Key extends AnyAriaBooleanAttribute ? boolean | string : string; }): void;
171
+ declare function setAria(element: Element, attributes: Record<string, unknown>): void;
172
172
  /**
173
173
  * Set the role of an element
174
174
  *
package/dist/index.mjs CHANGED
@@ -528,7 +528,7 @@ function updateAttribute(element, name, value, dispatch) {
528
528
  const isBoolean = booleanAttributesSet.has(lowerCaseName);
529
529
  const next = isBoolean ? value === true || typeof value === "string" && (value === "" || value.toLowerCase() === lowerCaseName) : value == null ? "" : value;
530
530
  if (isBoolean || dispatchedAttributes.has(name)) updateProperty(element, name, next, dispatch);
531
- updateElementValue(element, name, isBoolean ? next ? "" : null : value, element.setAttribute, element.removeAttribute, isBoolean, false);
531
+ updateElementValue(element, name, isBoolean ? next ? "" : null : value, element.setAttribute, element.removeAttribute, false);
532
532
  }
533
533
  const EXPRESSION_CLOBBERED_NAME = /^(id|name)$/i;
534
534
  const EXPRESSION_DATA_OR_SCRIPT = /^(?:data|\w+script):/i;
@@ -577,6 +577,10 @@ const formElement = document.createElement("form");
577
577
  let textArea;
578
578
  //#endregion
579
579
  //#region src/internal/element-value.ts
580
+ function getValueForAttribute(value, json) {
581
+ if (typeof value === "string") return value;
582
+ return json ? JSON.stringify(value) : getString(value);
583
+ }
580
584
  function ignoreSetAttribute(element, name) {
581
585
  if (element instanceof HTMLTextAreaElement && name === "value") return true;
582
586
  return false;
@@ -608,9 +612,9 @@ function setElementValues(element, first, second, third, callback, style) {
608
612
  if (typeof entry === "object" && typeof entry?.name === "string") callback(element, normalizeKey(entry.name, style), entry.value, dispatch);
609
613
  }
610
614
  }
611
- function updateElementValue(element, key, value, set, remove, isBoolean, json) {
615
+ function updateElementValue(element, key, value, set, remove, json) {
612
616
  if (value == null) remove.call(element, key);
613
- else if (!ignoreSetAttribute(element, key)) set.call(element, key, json ? JSON.stringify(value) : getString(value));
617
+ else if (!ignoreSetAttribute(element, key)) set.call(element, key, getValueForAttribute(value, json));
614
618
  }
615
619
  const CSS_VARIABLE_PREFIX$1 = "--";
616
620
  //#endregion
@@ -662,7 +666,7 @@ function updateAriaAttribute(element, key, value) {
662
666
  const name = getName$1(key);
663
667
  let actual = value;
664
668
  if (ariaBooleanAttributesSet.has(name) && typeof value === "string" && EXPRESSION_BOOLEAN.test(value)) actual = value.toLowerCase() === "true";
665
- updateElementValue(element, name, actual, element.setAttribute, element.removeAttribute, false, false);
669
+ updateElementValue(element, name, actual, element.setAttribute, element.removeAttribute, false);
666
670
  }
667
671
  const ATTRIBUTE_ARIA_PREFIX = "aria-";
668
672
  const EXPRESSION_ARIA_PREFIX = /^aria-/i;
@@ -758,21 +762,21 @@ function isInvalidBooleanAttribute(first, second) {
758
762
  function getData(element, keys, parseValues) {
759
763
  if (!(element instanceof Element)) return;
760
764
  const noParse = parseValues === false;
761
- if (typeof keys === "string") {
762
- const value = element.dataset[camelCase(keys)];
763
- if (value === void 0) return;
764
- return noParse ? value : parse(value);
765
- }
765
+ if (typeof keys === "string") return getDataValue(element, keys, noParse);
766
766
  const { length } = keys;
767
767
  const data = {};
768
768
  for (let index = 0; index < length; index += 1) {
769
769
  const key = keys[index];
770
- const value = element.dataset[camelCase(key)];
771
- if (value == null) data[key] = void 0;
772
- else data[key] = noParse ? value : parse(value);
770
+ data[key] = getDataValue(element, key, noParse);
773
771
  }
774
772
  return data;
775
773
  }
774
+ function getDataValue(element, key, noParse) {
775
+ const value = element.dataset[camelCase(key)];
776
+ if (value == null) return;
777
+ if (noParse) return value;
778
+ return parse(value) ?? value;
779
+ }
776
780
  function getName(original) {
777
781
  return `${ATTRIBUTE_DATA_PREFIX}${kebabCase(original.replace(EXPRESSION_DATA_PREFIX, ""))}`;
778
782
  }
@@ -780,7 +784,7 @@ function setData(element, first, second) {
780
784
  setElementValues(element, first, second, null, updateDataAttribute);
781
785
  }
782
786
  function updateDataAttribute(element, key, value) {
783
- updateElementValue(element, getName(key), value, element.setAttribute, element.removeAttribute, false, true);
787
+ updateElementValue(element, getName(key), value, element.setAttribute, element.removeAttribute, true);
784
788
  }
785
789
  const ATTRIBUTE_DATA_PREFIX = "data-";
786
790
  //#endregion
@@ -942,7 +946,7 @@ function updateStyleProperty(element, key, value) {
942
946
  if (name.startsWith(VARIABLE_PREFIX)) this.style.removeProperty(name);
943
947
  else this.style[name] = "";
944
948
  if (this.getAttribute(ATTRIBUTE_STYLE) === "") this.removeAttribute(ATTRIBUTE_STYLE);
945
- }, false, false);
949
+ }, false);
946
950
  }
947
951
  const ATTRIBUTE_STYLE = "style";
948
952
  const DIRECTION_LTR = "ltr";
@@ -56,7 +56,7 @@ function updateAttribute(element, name, value, dispatch) {
56
56
  const isBoolean = booleanAttributesSet.has(lowerCaseName);
57
57
  const next = isBoolean ? value === true || typeof value === "string" && (value === "" || value.toLowerCase() === lowerCaseName) : value == null ? "" : value;
58
58
  if (isBoolean || dispatchedAttributes.has(name)) updateProperty(element, name, next, dispatch);
59
- updateElementValue(element, name, isBoolean ? next ? "" : null : value, element.setAttribute, element.removeAttribute, isBoolean, false);
59
+ updateElementValue(element, name, isBoolean ? next ? "" : null : value, element.setAttribute, element.removeAttribute, false);
60
60
  }
61
61
  const EXPRESSION_CLOBBERED_NAME = /^(id|name)$/i;
62
62
  const EXPRESSION_DATA_OR_SCRIPT = /^(?:data|\w+script):/i;
@@ -1,6 +1,6 @@
1
1
  //#region src/internal/element-value.d.ts
2
2
  declare function setElementValue(element: Element, first: unknown, second: unknown, third: unknown, callback: (element: Element, key: string, value: unknown, dispatch: boolean) => void, style?: boolean): void;
3
3
  declare function setElementValues(element: Element, first: unknown, second: unknown, third: unknown, callback: (element: Element, key: string, value: unknown, dispatch: boolean) => void, style?: boolean): void;
4
- declare function updateElementValue(element: Element, key: string, value: unknown, set: (key: string, value: string) => void, remove: (key: string) => void, isBoolean: boolean, json: boolean): void;
4
+ declare function updateElementValue(element: Element, key: string, value: unknown, set: (key: string, value: string) => void, remove: (key: string) => void, json: boolean): void;
5
5
  //#endregion
6
6
  export { setElementValue, setElementValues, updateElementValue };
@@ -2,6 +2,10 @@ import { isAttribute } from "./attribute.mjs";
2
2
  import { getString } from "@oscarpalmer/atoms/string";
3
3
  import { kebabCase } from "@oscarpalmer/atoms/string/case";
4
4
  //#region src/internal/element-value.ts
5
+ function getValueForAttribute(value, json) {
6
+ if (typeof value === "string") return value;
7
+ return json ? JSON.stringify(value) : getString(value);
8
+ }
5
9
  function ignoreSetAttribute(element, name) {
6
10
  if (element instanceof HTMLTextAreaElement && name === "value") return true;
7
11
  return false;
@@ -33,9 +37,9 @@ function setElementValues(element, first, second, third, callback, style) {
33
37
  if (typeof entry === "object" && typeof entry?.name === "string") callback(element, normalizeKey(entry.name, style), entry.value, dispatch);
34
38
  }
35
39
  }
36
- function updateElementValue(element, key, value, set, remove, isBoolean, json) {
40
+ function updateElementValue(element, key, value, set, remove, json) {
37
41
  if (value == null) remove.call(element, key);
38
- else if (!ignoreSetAttribute(element, key)) set.call(element, key, json ? JSON.stringify(value) : getString(value));
42
+ else if (!ignoreSetAttribute(element, key)) set.call(element, key, getValueForAttribute(value, json));
39
43
  }
40
44
  const CSS_VARIABLE_PREFIX = "--";
41
45
  //#endregion
package/dist/style.mjs CHANGED
@@ -100,7 +100,7 @@ function updateStyleProperty(element, key, value) {
100
100
  if (name.startsWith(VARIABLE_PREFIX)) this.style.removeProperty(name);
101
101
  else this.style[name] = "";
102
102
  if (this.getAttribute(ATTRIBUTE_STYLE) === "") this.removeAttribute(ATTRIBUTE_STYLE);
103
- }, false, false);
103
+ }, false);
104
104
  }
105
105
  const ATTRIBUTE_STYLE = "style";
106
106
  const DIRECTION_LTR = "ltr";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oscarpalmer/toretto",
3
- "version": "0.49.0",
3
+ "version": "0.49.1",
4
4
  "description": "A collection of badass DOM utilities.",
5
5
  "keywords": [
6
6
  "dom",
package/src/aria.ts CHANGED
@@ -149,12 +149,7 @@ export function setAria(element: Element, attributes: AriaAttributeItem[]): void
149
149
  * @param element Element for _ARIA_ attributes
150
150
  * @param attributes _ARIA_ attributes to set
151
151
  */
152
- export function setAria(
153
- element: Element,
154
- attributes: {
155
- [Key in AnyAriaAttribute]?: Key extends AnyAriaBooleanAttribute ? boolean | string : string;
156
- },
157
- ): void;
152
+ export function setAria(element: Element, attributes: Record<string, unknown>): void;
158
153
 
159
154
  export function setAria(element: Element, first: unknown, second?: unknown): void {
160
155
  setElementValues(element, first, second, null, updateAriaAttribute);
@@ -202,7 +197,6 @@ function updateAriaAttribute(element: Element, key: string, value: unknown): voi
202
197
  // oxlint-disable-next-line typescript/unbound-method
203
198
  element.removeAttribute,
204
199
  false,
205
- false,
206
200
  );
207
201
  }
208
202
 
package/src/data.ts CHANGED
@@ -56,13 +56,7 @@ export function getData(element: Element, keys: string | string[], parseValues?:
56
56
  const noParse = parseValues === false;
57
57
 
58
58
  if (typeof keys === 'string') {
59
- const value = (element as HTMLElement).dataset[camelCase(keys)];
60
-
61
- if (value === undefined) {
62
- return;
63
- }
64
-
65
- return noParse ? value : parse(value);
59
+ return getDataValue(element, keys, noParse);
66
60
  }
67
61
 
68
62
  const {length} = keys;
@@ -71,18 +65,27 @@ export function getData(element: Element, keys: string | string[], parseValues?:
71
65
 
72
66
  for (let index = 0; index < length; index += 1) {
73
67
  const key = keys[index];
74
- const value = (element as HTMLElement).dataset[camelCase(key)];
75
68
 
76
- if (value == null) {
77
- data[key] = undefined;
78
- } else {
79
- data[key] = noParse ? value : parse(value);
80
- }
69
+ data[key] = getDataValue(element, key, noParse);
81
70
  }
82
71
 
83
72
  return data;
84
73
  }
85
74
 
75
+ function getDataValue(element: Element, key: string, noParse: boolean): unknown {
76
+ const value = (element as HTMLElement).dataset[camelCase(key)];
77
+
78
+ if (value == null) {
79
+ return;
80
+ }
81
+
82
+ if (noParse) {
83
+ return value;
84
+ }
85
+
86
+ return parse(value) ?? value;
87
+ }
88
+
86
89
  function getName(original: string): string {
87
90
  return `${ATTRIBUTE_DATA_PREFIX}${kebabCase(original.replace(EXPRESSION_DATA_PREFIX, ''))}`;
88
91
  }
@@ -119,7 +122,6 @@ function updateDataAttribute(element: Element, key: string, value: unknown): voi
119
122
  // Using `.call` in `updateElementValue`
120
123
  // oxlint-disable-next-line typescript/unbound-method
121
124
  element.removeAttribute,
122
- false,
123
125
  true,
124
126
  );
125
127
  }
@@ -148,7 +148,6 @@ export function updateAttribute(
148
148
  // Using `.call` in `updateElementValue`
149
149
  // oxlint-disable-next-line typescript/unbound-method
150
150
  element.removeAttribute,
151
- isBoolean,
152
151
  false,
153
152
  );
154
153
  }
@@ -4,6 +4,14 @@ import {isAttribute} from './attribute';
4
4
 
5
5
  // #region Functions
6
6
 
7
+ function getValueForAttribute(value: unknown, json: boolean): string | undefined {
8
+ if (typeof value === 'string') {
9
+ return value;
10
+ }
11
+
12
+ return json ? JSON.stringify(value) : getString(value);
13
+ }
14
+
7
15
  function ignoreSetAttribute(element: Element, name: string): boolean {
8
16
  if (element instanceof HTMLTextAreaElement && name === 'value') {
9
17
  return true;
@@ -79,13 +87,12 @@ export function updateElementValue(
79
87
  value: unknown,
80
88
  set: (key: string, value: string) => void,
81
89
  remove: (key: string) => void,
82
- isBoolean: boolean,
83
90
  json: boolean,
84
91
  ): void {
85
92
  if (value == null) {
86
93
  remove.call(element, key);
87
94
  } else if (!ignoreSetAttribute(element, key)) {
88
- set.call(element, key, json ? JSON.stringify(value) : getString(value));
95
+ set.call(element, key, getValueForAttribute(value, json) as string);
89
96
  }
90
97
  }
91
98
 
package/src/style.ts CHANGED
@@ -201,7 +201,6 @@ function updateStyleProperty(element: Element, key: string, value: unknown): voi
201
201
  }
202
202
  },
203
203
  false,
204
- false,
205
204
  );
206
205
  }
207
206