@oscarpalmer/toretto 0.3.0 → 0.5.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.
package/dist/attribute.js CHANGED
@@ -10,6 +10,15 @@ function getString(value2) {
10
10
  const asString = valueOff?.toString?.() ?? String(valueOff);
11
11
  return asString.startsWith("[object ") ? JSON.stringify(value2) : asString;
12
12
  }
13
+ // node_modules/@oscarpalmer/atoms/dist/js/is.mjs
14
+ function isPlainObject(value2) {
15
+ if (typeof value2 !== "object" || value2 === null) {
16
+ return false;
17
+ }
18
+ const prototype = Object.getPrototypeOf(value2);
19
+ return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value2) && !(Symbol.iterator in value2);
20
+ }
21
+
13
22
  // src/attribute.ts
14
23
  function isBadAttribute(attribute) {
15
24
  return onPrefix.test(attribute.name) || sourcePrefix.test(attribute.name) && valuePrefix.test(attribute.value);
@@ -28,7 +37,7 @@ function isInvalidBooleanAttribute(attribute) {
28
37
  return !(normalised.length === 0 || normalised === attribute.name || attribute.name === "hidden" && normalised === "until-found");
29
38
  }
30
39
  function setAttribute(element, first, second) {
31
- if (typeof first === "object" && typeof first?.name === "string") {
40
+ if (isPlainObject(first) && typeof first?.name === "string") {
32
41
  setAttributeValue(element, first.name, first.value);
33
42
  } else if (typeof first === "string") {
34
43
  setAttributeValue(element, first, second);
@@ -1,4 +1,5 @@
1
1
  // src/attribute.ts
2
+ import {isPlainObject} from "@oscarpalmer/atoms/is";
2
3
  import {getString} from "@oscarpalmer/atoms/string";
3
4
  function isBadAttribute(attribute) {
4
5
  return onPrefix.test(attribute.name) || sourcePrefix.test(attribute.name) && valuePrefix.test(attribute.value);
@@ -17,7 +18,7 @@ function isInvalidBooleanAttribute(attribute) {
17
18
  return !(normalised.length === 0 || normalised === attribute.name || attribute.name === "hidden" && normalised === "until-found");
18
19
  }
19
20
  function setAttribute(element, first, second) {
20
- if (typeof first === "object" && typeof first?.name === "string") {
21
+ if (isPlainObject(first) && typeof first?.name === "string") {
21
22
  setAttributeValue(element, first.name, first.value);
22
23
  } else if (typeof first === "string") {
23
24
  setAttributeValue(element, first, second);
package/dist/data.js ADDED
@@ -0,0 +1,79 @@
1
+ // node_modules/@oscarpalmer/atoms/dist/js/is.mjs
2
+ function isNullableOrWhitespace(value) {
3
+ return value == null || /^\s*$/.test(getString(value));
4
+ }
5
+ function isPlainObject(value) {
6
+ if (typeof value !== "object" || value === null) {
7
+ return false;
8
+ }
9
+ const prototype = Object.getPrototypeOf(value);
10
+ return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value) && !(Symbol.iterator in value);
11
+ }
12
+ // node_modules/@oscarpalmer/atoms/dist/js/string/index.mjs
13
+ function getString(value2) {
14
+ if (typeof value2 === "string") {
15
+ return value2;
16
+ }
17
+ if (typeof value2 !== "object" || value2 == null) {
18
+ return String(value2);
19
+ }
20
+ const valueOff = value2.valueOf?.() ?? value2;
21
+ const asString = valueOff?.toString?.() ?? String(valueOff);
22
+ return asString.startsWith("[object ") ? JSON.stringify(value2) : asString;
23
+ }
24
+ function parse(value2, reviver) {
25
+ try {
26
+ return JSON.parse(value2, reviver);
27
+ } catch {
28
+ }
29
+ }
30
+ // src/internal/element-value.ts
31
+ function setElementValues(element, first, second, callback) {
32
+ if (isPlainObject(first)) {
33
+ const entries = Object.entries(first);
34
+ const { length } = entries;
35
+ for (let index = 0;index < length; index += 1) {
36
+ const [key, value2] = entries[index];
37
+ callback(element, key, value2);
38
+ }
39
+ } else if (first != null) {
40
+ callback(element, first, second);
41
+ }
42
+ }
43
+ function updateElementValue(element, key, value2, set3, remove, json) {
44
+ if (isNullableOrWhitespace(value2)) {
45
+ remove.call(element, key);
46
+ } else {
47
+ set3.call(element, key, json ? JSON.stringify(value2) : String(value2));
48
+ }
49
+ }
50
+
51
+ // src/data.ts
52
+ function getData(element, keys) {
53
+ if (typeof keys === "string") {
54
+ return getDataValue(element, keys);
55
+ }
56
+ const { length } = keys;
57
+ const data = {};
58
+ for (let index = 0;index < length; index += 1) {
59
+ const key = keys[index];
60
+ data[key] = getDataValue(element, key);
61
+ }
62
+ return data;
63
+ }
64
+ function getDataValue(element, key) {
65
+ const value2 = element.dataset[key];
66
+ if (value2 != null) {
67
+ return parse(value2);
68
+ }
69
+ }
70
+ function setData(element, first, second) {
71
+ setElementValues(element, first, second, updateDataAttribute);
72
+ }
73
+ function updateDataAttribute(element, key, value2) {
74
+ updateElementValue(element, `data-${key}`, value2, element.setAttribute, element.removeAttribute, true);
75
+ }
76
+ export {
77
+ setData,
78
+ getData
79
+ };
package/dist/data.mjs ADDED
@@ -0,0 +1,31 @@
1
+ // src/data.ts
2
+ import {parse} from "@oscarpalmer/atoms/string";
3
+ import {setElementValues, updateElementValue} from "./internal/element-value";
4
+ function getData(element, keys) {
5
+ if (typeof keys === "string") {
6
+ return getDataValue(element, keys);
7
+ }
8
+ const { length } = keys;
9
+ const data = {};
10
+ for (let index = 0;index < length; index += 1) {
11
+ const key = keys[index];
12
+ data[key] = getDataValue(element, key);
13
+ }
14
+ return data;
15
+ }
16
+ function getDataValue(element, key) {
17
+ const value = element.dataset[key];
18
+ if (value != null) {
19
+ return parse(value);
20
+ }
21
+ }
22
+ function setData(element, first, second) {
23
+ setElementValues(element, first, second, updateDataAttribute);
24
+ }
25
+ function updateDataAttribute(element, key, value) {
26
+ updateElementValue(element, `data-${key}`, value, element.setAttribute, element.removeAttribute, true);
27
+ }
28
+ export {
29
+ setData,
30
+ getData
31
+ };
package/dist/index.js CHANGED
@@ -10,6 +10,24 @@ function getString(value2) {
10
10
  const asString = valueOff?.toString?.() ?? String(valueOff);
11
11
  return asString.startsWith("[object ") ? JSON.stringify(value2) : asString;
12
12
  }
13
+ function parse(value2, reviver) {
14
+ try {
15
+ return JSON.parse(value2, reviver);
16
+ } catch {
17
+ }
18
+ }
19
+ // node_modules/@oscarpalmer/atoms/dist/js/is.mjs
20
+ function isNullableOrWhitespace(value2) {
21
+ return value2 == null || /^\s*$/.test(getString(value2));
22
+ }
23
+ function isPlainObject(value2) {
24
+ if (typeof value2 !== "object" || value2 === null) {
25
+ return false;
26
+ }
27
+ const prototype = Object.getPrototypeOf(value2);
28
+ return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value2) && !(Symbol.iterator in value2);
29
+ }
30
+
13
31
  // src/attribute.ts
14
32
  function isBadAttribute(attribute) {
15
33
  return onPrefix.test(attribute.name) || sourcePrefix.test(attribute.name) && valuePrefix.test(attribute.value);
@@ -28,7 +46,7 @@ function isInvalidBooleanAttribute(attribute) {
28
46
  return !(normalised.length === 0 || normalised === attribute.name || attribute.name === "hidden" && normalised === "until-found");
29
47
  }
30
48
  function setAttribute(element, first, second) {
31
- if (typeof first === "object" && typeof first?.name === "string") {
49
+ if (isPlainObject(first) && typeof first?.name === "string") {
32
50
  setAttributeValue(element, first.name, first.value);
33
51
  } else if (typeof first === "string") {
34
52
  setAttributeValue(element, first, second);
@@ -83,12 +101,90 @@ var booleanAttributes = Object.freeze([
83
101
  var onPrefix = /^on/i;
84
102
  var sourcePrefix = /^(href|src|xlink:href)$/i;
85
103
  var valuePrefix = /(data:text\/html|javascript:)/i;
104
+ // src/internal/element-value.ts
105
+ function setElementValues(element, first, second, callback) {
106
+ if (isPlainObject(first)) {
107
+ const entries = Object.entries(first);
108
+ const { length } = entries;
109
+ for (let index = 0;index < length; index += 1) {
110
+ const [key, value2] = entries[index];
111
+ callback(element, key, value2);
112
+ }
113
+ } else if (first != null) {
114
+ callback(element, first, second);
115
+ }
116
+ }
117
+ function updateElementValue(element, key, value2, set3, remove, json) {
118
+ if (isNullableOrWhitespace(value2)) {
119
+ remove.call(element, key);
120
+ } else {
121
+ set3.call(element, key, json ? JSON.stringify(value2) : String(value2));
122
+ }
123
+ }
124
+
125
+ // src/data.ts
126
+ function getData(element, keys) {
127
+ if (typeof keys === "string") {
128
+ return getDataValue(element, keys);
129
+ }
130
+ const { length } = keys;
131
+ const data = {};
132
+ for (let index = 0;index < length; index += 1) {
133
+ const key = keys[index];
134
+ data[key] = getDataValue(element, key);
135
+ }
136
+ return data;
137
+ }
138
+ function getDataValue(element, key) {
139
+ const value2 = element.dataset[key];
140
+ if (value2 != null) {
141
+ return parse(value2);
142
+ }
143
+ }
144
+ function setData(element, first, second) {
145
+ setElementValues(element, first, second, updateDataAttribute);
146
+ }
147
+ function updateDataAttribute(element, key, value2) {
148
+ updateElementValue(element, `data-${key}`, value2, element.setAttribute, element.removeAttribute, true);
149
+ }
150
+ // src/style.ts
151
+ function getStyle(element, property) {
152
+ return element.style[property];
153
+ }
154
+ function getStyles(element, properties) {
155
+ const styles = {};
156
+ const { length } = properties;
157
+ for (let index = 0;index < length; index += 1) {
158
+ const property = properties[index];
159
+ styles[property] = element.style[property];
160
+ }
161
+ return styles;
162
+ }
163
+ function setStyle(element, property, value2) {
164
+ setElementValues(element, property, value2, updateStyleProperty);
165
+ }
166
+ function setStyles(element, styles) {
167
+ setElementValues(element, styles, null, updateStyleProperty);
168
+ }
169
+ function updateStyleProperty(element, key, value2) {
170
+ updateElementValue(element, key, value2, function(property, value3) {
171
+ this.style[property] = value3;
172
+ }, function(property) {
173
+ this.style[property] = "";
174
+ }, false);
175
+ }
86
176
  export {
177
+ setStyles,
178
+ setStyle,
179
+ setData,
87
180
  setAttributes,
88
181
  setAttribute,
89
182
  isInvalidBooleanAttribute,
90
183
  isEmptyNonBooleanAttribute,
91
184
  isBooleanAttribute,
92
185
  isBadAttribute,
186
+ getStyles,
187
+ getStyle,
188
+ getData,
93
189
  booleanAttributes
94
190
  };
package/dist/index.mjs CHANGED
@@ -1,2 +1,4 @@
1
1
  // src/index.ts
2
2
  export * from "./attribute";
3
+ export * from "./data";
4
+ export * from "./style";
@@ -0,0 +1,48 @@
1
+ // node_modules/@oscarpalmer/atoms/dist/js/string/index.mjs
2
+ function getString(value2) {
3
+ if (typeof value2 === "string") {
4
+ return value2;
5
+ }
6
+ if (typeof value2 !== "object" || value2 == null) {
7
+ return String(value2);
8
+ }
9
+ const valueOff = value2.valueOf?.() ?? value2;
10
+ const asString = valueOff?.toString?.() ?? String(valueOff);
11
+ return asString.startsWith("[object ") ? JSON.stringify(value2) : asString;
12
+ }
13
+ // node_modules/@oscarpalmer/atoms/dist/js/is.mjs
14
+ function isNullableOrWhitespace(value2) {
15
+ return value2 == null || /^\s*$/.test(getString(value2));
16
+ }
17
+ function isPlainObject(value2) {
18
+ if (typeof value2 !== "object" || value2 === null) {
19
+ return false;
20
+ }
21
+ const prototype = Object.getPrototypeOf(value2);
22
+ return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value2) && !(Symbol.iterator in value2);
23
+ }
24
+
25
+ // src/internal/element-value.ts
26
+ function setElementValues(element, first, second, callback) {
27
+ if (isPlainObject(first)) {
28
+ const entries = Object.entries(first);
29
+ const { length } = entries;
30
+ for (let index = 0;index < length; index += 1) {
31
+ const [key, value2] = entries[index];
32
+ callback(element, key, value2);
33
+ }
34
+ } else if (first != null) {
35
+ callback(element, first, second);
36
+ }
37
+ }
38
+ function updateElementValue(element, key, value2, set3, remove, json) {
39
+ if (isNullableOrWhitespace(value2)) {
40
+ remove.call(element, key);
41
+ } else {
42
+ set3.call(element, key, json ? JSON.stringify(value2) : String(value2));
43
+ }
44
+ }
45
+ export {
46
+ updateElementValue,
47
+ setElementValues
48
+ };
@@ -0,0 +1,25 @@
1
+ // src/internal/element-value.ts
2
+ import {isNullableOrWhitespace, isPlainObject} from "@oscarpalmer/atoms/is";
3
+ function setElementValues(element, first, second, callback) {
4
+ if (isPlainObject(first)) {
5
+ const entries = Object.entries(first);
6
+ const { length } = entries;
7
+ for (let index = 0;index < length; index += 1) {
8
+ const [key, value] = entries[index];
9
+ callback(element, key, value);
10
+ }
11
+ } else if (first != null) {
12
+ callback(element, first, second);
13
+ }
14
+ }
15
+ function updateElementValue(element, key, value, set, remove, json) {
16
+ if (isNullableOrWhitespace(value)) {
17
+ remove.call(element, key);
18
+ } else {
19
+ set.call(element, key, json ? JSON.stringify(value) : String(value));
20
+ }
21
+ }
22
+ export {
23
+ updateElementValue,
24
+ setElementValues
25
+ };
package/dist/style.js ADDED
@@ -0,0 +1,77 @@
1
+ // node_modules/@oscarpalmer/atoms/dist/js/string/index.mjs
2
+ function getString(value2) {
3
+ if (typeof value2 === "string") {
4
+ return value2;
5
+ }
6
+ if (typeof value2 !== "object" || value2 == null) {
7
+ return String(value2);
8
+ }
9
+ const valueOff = value2.valueOf?.() ?? value2;
10
+ const asString = valueOff?.toString?.() ?? String(valueOff);
11
+ return asString.startsWith("[object ") ? JSON.stringify(value2) : asString;
12
+ }
13
+ // node_modules/@oscarpalmer/atoms/dist/js/is.mjs
14
+ function isNullableOrWhitespace(value2) {
15
+ return value2 == null || /^\s*$/.test(getString(value2));
16
+ }
17
+ function isPlainObject(value2) {
18
+ if (typeof value2 !== "object" || value2 === null) {
19
+ return false;
20
+ }
21
+ const prototype = Object.getPrototypeOf(value2);
22
+ return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value2) && !(Symbol.iterator in value2);
23
+ }
24
+
25
+ // src/internal/element-value.ts
26
+ function setElementValues(element, first, second, callback) {
27
+ if (isPlainObject(first)) {
28
+ const entries = Object.entries(first);
29
+ const { length } = entries;
30
+ for (let index = 0;index < length; index += 1) {
31
+ const [key, value2] = entries[index];
32
+ callback(element, key, value2);
33
+ }
34
+ } else if (first != null) {
35
+ callback(element, first, second);
36
+ }
37
+ }
38
+ function updateElementValue(element, key, value2, set3, remove, json) {
39
+ if (isNullableOrWhitespace(value2)) {
40
+ remove.call(element, key);
41
+ } else {
42
+ set3.call(element, key, json ? JSON.stringify(value2) : String(value2));
43
+ }
44
+ }
45
+
46
+ // src/style.ts
47
+ function getStyle(element, property) {
48
+ return element.style[property];
49
+ }
50
+ function getStyles(element, properties) {
51
+ const styles = {};
52
+ const { length } = properties;
53
+ for (let index = 0;index < length; index += 1) {
54
+ const property = properties[index];
55
+ styles[property] = element.style[property];
56
+ }
57
+ return styles;
58
+ }
59
+ function setStyle(element, property, value2) {
60
+ setElementValues(element, property, value2, updateStyleProperty);
61
+ }
62
+ function setStyles(element, styles) {
63
+ setElementValues(element, styles, null, updateStyleProperty);
64
+ }
65
+ function updateStyleProperty(element, key, value2) {
66
+ updateElementValue(element, key, value2, function(property, value3) {
67
+ this.style[property] = value3;
68
+ }, function(property) {
69
+ this.style[property] = "";
70
+ }, false);
71
+ }
72
+ export {
73
+ setStyles,
74
+ setStyle,
75
+ getStyles,
76
+ getStyle
77
+ };
package/dist/style.mjs ADDED
@@ -0,0 +1,33 @@
1
+ // src/style.ts
2
+ import {setElementValues, updateElementValue} from "./internal/element-value";
3
+ function getStyle(element, property) {
4
+ return element.style[property];
5
+ }
6
+ function getStyles(element, properties) {
7
+ const styles = {};
8
+ const { length } = properties;
9
+ for (let index = 0;index < length; index += 1) {
10
+ const property = properties[index];
11
+ styles[property] = element.style[property];
12
+ }
13
+ return styles;
14
+ }
15
+ function setStyle(element, property, value) {
16
+ setElementValues(element, property, value, updateStyleProperty);
17
+ }
18
+ function setStyles(element, styles) {
19
+ setElementValues(element, styles, null, updateStyleProperty);
20
+ }
21
+ function updateStyleProperty(element, key, value) {
22
+ updateElementValue(element, key, value, function(property, value2) {
23
+ this.style[property] = value2;
24
+ }, function(property) {
25
+ this.style[property] = "";
26
+ }, false);
27
+ }
28
+ export {
29
+ setStyles,
30
+ setStyle,
31
+ getStyles,
32
+ getStyle
33
+ };
package/package.json CHANGED
@@ -32,6 +32,18 @@
32
32
  "bun": "./src/attribute.ts",
33
33
  "import": "./dist/attribute.mjs",
34
34
  "require": "./dist/attribute.js"
35
+ },
36
+ "./data": {
37
+ "types": "./types/data.d.ts",
38
+ "bun": "./src/data.ts",
39
+ "import": "./dist/data.mjs",
40
+ "require": "./dist/data.js"
41
+ },
42
+ "./style": {
43
+ "types": "./types/style.d.ts",
44
+ "bun": "./src/style.ts",
45
+ "import": "./dist/style.mjs",
46
+ "require": "./dist/style.js"
35
47
  }
36
48
  },
37
49
  "files": ["dist", "src", "types"],
@@ -56,5 +68,5 @@
56
68
  },
57
69
  "type": "module",
58
70
  "types": "types/index.d.cts",
59
- "version": "0.3.0"
71
+ "version": "0.5.0"
60
72
  }
package/src/attribute.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import {isPlainObject} from '@oscarpalmer/atoms/is';
1
2
  import {getString} from '@oscarpalmer/atoms/string';
2
3
 
3
4
  type Attribute<Value = unknown> = {
@@ -107,10 +108,7 @@ export function setAttribute(
107
108
  first: unknown,
108
109
  second?: unknown,
109
110
  ): void {
110
- if (
111
- typeof first === 'object' &&
112
- typeof (first as Attribute)?.name === 'string'
113
- ) {
111
+ if (isPlainObject(first) && typeof (first as Attribute)?.name === 'string') {
114
112
  setAttributeValue(
115
113
  element,
116
114
  (first as Attribute).name,
package/src/data.ts ADDED
@@ -0,0 +1,82 @@
1
+ import type {PlainObject} from '@oscarpalmer/atoms/models';
2
+ import {parse} from '@oscarpalmer/atoms/string';
3
+ import {setElementValues, updateElementValue} from './internal/element-value';
4
+
5
+ /**
6
+ * Get data values from an element
7
+ */
8
+ export function getData<Value extends PlainObject>(
9
+ element: HTMLElement,
10
+ keys: string[],
11
+ ): Value;
12
+
13
+ /**
14
+ * Get a data value from an element
15
+ */
16
+ export function getData(element: HTMLElement, key: string): unknown;
17
+
18
+ export function getData(
19
+ element: HTMLElement,
20
+ keys: string | string[],
21
+ ): unknown {
22
+ if (typeof keys === 'string') {
23
+ return getDataValue(element, keys);
24
+ }
25
+
26
+ const {length} = keys;
27
+
28
+ const data: PlainObject = {};
29
+
30
+ for (let index = 0; index < length; index += 1) {
31
+ const key = keys[index];
32
+
33
+ data[key] = getDataValue(element, key);
34
+ }
35
+
36
+ return data;
37
+ }
38
+
39
+ function getDataValue(element: HTMLElement, key: string): unknown {
40
+ const value = element.dataset[key];
41
+
42
+ if (value != null) {
43
+ return parse(value);
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Set data values on an element
49
+ */
50
+ export function setData(element: HTMLElement, data: PlainObject): void;
51
+
52
+ /**
53
+ * Set a data value on an element
54
+ */
55
+ export function setData(
56
+ element: HTMLElement,
57
+ key: string,
58
+ value: unknown,
59
+ ): void;
60
+
61
+ export function setData(
62
+ element: HTMLElement,
63
+ first: PlainObject | string,
64
+ second?: unknown,
65
+ ): void {
66
+ setElementValues(element, first, second, updateDataAttribute);
67
+ }
68
+
69
+ function updateDataAttribute(
70
+ element: HTMLElement,
71
+ key: string,
72
+ value: unknown,
73
+ ): void {
74
+ updateElementValue(
75
+ element,
76
+ `data-${key}`,
77
+ value,
78
+ element.setAttribute,
79
+ element.removeAttribute,
80
+ true,
81
+ );
82
+ }
package/src/index.ts CHANGED
@@ -1 +1,3 @@
1
1
  export * from './attribute';
2
+ export * from './data';
3
+ export * from './style';
@@ -0,0 +1,37 @@
1
+ import {isNullableOrWhitespace, isPlainObject} from '@oscarpalmer/atoms/is';
2
+ import type {PlainObject} from '@oscarpalmer/atoms/models';
3
+
4
+ export function setElementValues(
5
+ element: HTMLElement,
6
+ first: PlainObject | string,
7
+ second: unknown,
8
+ callback: (element: HTMLElement, key: string, value: unknown) => void,
9
+ ): void {
10
+ if (isPlainObject(first)) {
11
+ const entries = Object.entries(first);
12
+ const {length} = entries;
13
+
14
+ for (let index = 0; index < length; index += 1) {
15
+ const [key, value] = entries[index];
16
+
17
+ callback(element, key, value);
18
+ }
19
+ } else if (first != null) {
20
+ callback(element, first, second);
21
+ }
22
+ }
23
+
24
+ export function updateElementValue(
25
+ element: HTMLElement,
26
+ key: string,
27
+ value: unknown,
28
+ set: (key: string, value: string) => void,
29
+ remove: (key: string) => void,
30
+ json: boolean,
31
+ ): void {
32
+ if (isNullableOrWhitespace(value)) {
33
+ remove.call(element, key);
34
+ } else {
35
+ set.call(element, key, json ? JSON.stringify(value) : String(value));
36
+ }
37
+ }
package/src/style.ts ADDED
@@ -0,0 +1,70 @@
1
+ import {setElementValues, updateElementValue} from './internal/element-value';
2
+
3
+ /**
4
+ * Get a style from an element
5
+ */
6
+ export function getStyle(
7
+ element: HTMLElement,
8
+ property: keyof CSSStyleDeclaration,
9
+ ): string {
10
+ return element.style[property as never];
11
+ }
12
+
13
+ /**
14
+ * Get styles from an element
15
+ */
16
+ export function getStyles<Property extends keyof CSSStyleDeclaration>(
17
+ element: HTMLElement,
18
+ properties: Property[],
19
+ ): Pick<CSSStyleDeclaration, Property> {
20
+ const styles = {} as Pick<CSSStyleDeclaration, Property>;
21
+ const {length} = properties;
22
+
23
+ for (let index = 0; index < length; index += 1) {
24
+ const property = properties[index];
25
+
26
+ styles[property] = element.style[property as never] as never;
27
+ }
28
+
29
+ return styles;
30
+ }
31
+
32
+ /**
33
+ * Set a style on an element
34
+ */
35
+ export function setStyle(
36
+ element: HTMLElement,
37
+ property: keyof CSSStyleDeclaration,
38
+ value?: string,
39
+ ): void {
40
+ setElementValues(element, property as string, value, updateStyleProperty);
41
+ }
42
+
43
+ /**
44
+ * Set styles on an element
45
+ */
46
+ export function setStyles(
47
+ element: HTMLElement,
48
+ styles: Partial<CSSStyleDeclaration>,
49
+ ): void {
50
+ setElementValues(element, styles as string, null, updateStyleProperty);
51
+ }
52
+
53
+ function updateStyleProperty(
54
+ element: HTMLElement,
55
+ key: string,
56
+ value: unknown,
57
+ ): void {
58
+ updateElementValue(
59
+ element,
60
+ key,
61
+ value,
62
+ function (this: HTMLElement, property: string, value: string) {
63
+ this.style[property as never] = value;
64
+ },
65
+ function (this: HTMLElement, property: string) {
66
+ this.style[property as never] = '';
67
+ },
68
+ false,
69
+ );
70
+ }
@@ -0,0 +1,17 @@
1
+ import type { PlainObject } from '@oscarpalmer/atoms/models';
2
+ /**
3
+ * Get data values from an element
4
+ */
5
+ export declare function getData<Value extends PlainObject>(element: HTMLElement, keys: string[]): Value;
6
+ /**
7
+ * Get a data value from an element
8
+ */
9
+ export declare function getData(element: HTMLElement, key: string): unknown;
10
+ /**
11
+ * Set data values on an element
12
+ */
13
+ export declare function setData(element: HTMLElement, data: PlainObject): void;
14
+ /**
15
+ * Set a data value on an element
16
+ */
17
+ export declare function setData(element: HTMLElement, key: string, value: unknown): void;
package/types/index.d.cts CHANGED
@@ -41,5 +41,69 @@ export declare function setAttributes(element: Element, attributes: Attribute[])
41
41
  * Set one or more attributes on an element _(or remove them, if their value is `null` or `undefined`)_
42
42
  */
43
43
  export declare function setAttributes(element: Element, attributes: Record<string, unknown>): void;
44
+ /**
45
+ Represents an object with `unknown` value. You probably want this instead of `{}`.
46
+
47
+ Use case: You have an object whose keys and values are unknown to you.
48
+
49
+ @example
50
+ ```
51
+ import type {UnknownRecord} from 'type-fest';
52
+
53
+ function toJson(object: UnknownRecord) {
54
+ return JSON.stringify(object);
55
+ }
56
+
57
+ toJson({hello: 'world'});
58
+ //=> '{"hello":"world"}'
59
+
60
+ function isObject(value: unknown): value is UnknownRecord {
61
+ return typeof value === 'object' && value !== null;
62
+ }
63
+
64
+ isObject({hello: 'world'});
65
+ //=> true
66
+
67
+ isObject('hello');
68
+ //=> false
69
+ ```
70
+
71
+ @category Type
72
+ @category Object
73
+ */
74
+ export type UnknownRecord = Record<PropertyKey, unknown>;
75
+ export type PlainObject = UnknownRecord;
76
+ /**
77
+ * Get data values from an element
78
+ */
79
+ export declare function getData<Value extends PlainObject>(element: HTMLElement, keys: string[]): Value;
80
+ /**
81
+ * Get a data value from an element
82
+ */
83
+ export declare function getData(element: HTMLElement, key: string): unknown;
84
+ /**
85
+ * Set data values on an element
86
+ */
87
+ export declare function setData(element: HTMLElement, data: PlainObject): void;
88
+ /**
89
+ * Set a data value on an element
90
+ */
91
+ export declare function setData(element: HTMLElement, key: string, value: unknown): void;
92
+ /**
93
+ * Get a style from an element
94
+ */
95
+ export declare function getStyle(element: HTMLElement, property: keyof CSSStyleDeclaration): string;
96
+ /**
97
+ * Get styles from an element
98
+ */
99
+ export declare function getStyles<Property extends keyof CSSStyleDeclaration>(element: HTMLElement, properties: Property[]): Pick<CSSStyleDeclaration, Property>;
100
+ /**
101
+ * Set a style on an element
102
+ */
103
+ export declare function setStyle(element: HTMLElement, property: keyof CSSStyleDeclaration, value?: string): void;
104
+ /**
105
+ * Set styles on an element
106
+ */
107
+ export declare function setStyles(element: HTMLElement, styles: Partial<CSSStyleDeclaration>): void;
44
108
 
45
109
  export {};
package/types/index.d.ts CHANGED
@@ -1 +1,3 @@
1
1
  export * from './attribute';
2
+ export * from './data';
3
+ export * from './style';
@@ -0,0 +1,3 @@
1
+ import type { PlainObject } from '@oscarpalmer/atoms/models';
2
+ export declare function setElementValues(element: HTMLElement, first: PlainObject | string, second: unknown, callback: (element: HTMLElement, key: string, value: unknown) => void): void;
3
+ export declare function updateElementValue(element: HTMLElement, key: string, value: unknown, set: (key: string, value: string) => void, remove: (key: string) => void, json: boolean): void;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Get a style from an element
3
+ */
4
+ export declare function getStyle(element: HTMLElement, property: keyof CSSStyleDeclaration): string;
5
+ /**
6
+ * Get styles from an element
7
+ */
8
+ export declare function getStyles<Property extends keyof CSSStyleDeclaration>(element: HTMLElement, properties: Property[]): Pick<CSSStyleDeclaration, Property>;
9
+ /**
10
+ * Set a style on an element
11
+ */
12
+ export declare function setStyle(element: HTMLElement, property: keyof CSSStyleDeclaration, value?: string): void;
13
+ /**
14
+ * Set styles on an element
15
+ */
16
+ export declare function setStyles(element: HTMLElement, styles: Partial<CSSStyleDeclaration>): void;