@oscarpalmer/toretto 0.4.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/index.js CHANGED
@@ -147,7 +147,35 @@ function setData(element, first, second) {
147
147
  function updateDataAttribute(element, key, value2) {
148
148
  updateElementValue(element, `data-${key}`, value2, element.setAttribute, element.removeAttribute, true);
149
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
+ }
150
176
  export {
177
+ setStyles,
178
+ setStyle,
151
179
  setData,
152
180
  setAttributes,
153
181
  setAttribute,
@@ -155,6 +183,8 @@ export {
155
183
  isEmptyNonBooleanAttribute,
156
184
  isBooleanAttribute,
157
185
  isBadAttribute,
186
+ getStyles,
187
+ getStyle,
158
188
  getData,
159
189
  booleanAttributes
160
190
  };
package/dist/index.mjs CHANGED
@@ -1,3 +1,4 @@
1
1
  // src/index.ts
2
2
  export * from "./attribute";
3
3
  export * from "./data";
4
+ export * from "./style";
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.4.0"
71
+ "version": "0.5.0"
60
72
  }
package/src/data.ts CHANGED
@@ -3,7 +3,7 @@ import {parse} from '@oscarpalmer/atoms/string';
3
3
  import {setElementValues, updateElementValue} from './internal/element-value';
4
4
 
5
5
  /**
6
- * Get data values from an element as an object
6
+ * Get data values from an element
7
7
  */
8
8
  export function getData<Value extends PlainObject>(
9
9
  element: HTMLElement,
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './attribute';
2
2
  export * from './data';
3
+ export * from './style';
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
+ }
package/types/data.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { PlainObject } from '@oscarpalmer/atoms/models';
2
2
  /**
3
- * Get data values from an element as an object
3
+ * Get data values from an element
4
4
  */
5
5
  export declare function getData<Value extends PlainObject>(element: HTMLElement, keys: string[]): Value;
6
6
  /**
package/types/index.d.cts CHANGED
@@ -74,7 +74,7 @@ isObject('hello');
74
74
  export type UnknownRecord = Record<PropertyKey, unknown>;
75
75
  export type PlainObject = UnknownRecord;
76
76
  /**
77
- * Get data values from an element as an object
77
+ * Get data values from an element
78
78
  */
79
79
  export declare function getData<Value extends PlainObject>(element: HTMLElement, keys: string[]): Value;
80
80
  /**
@@ -89,5 +89,21 @@ export declare function setData(element: HTMLElement, data: PlainObject): void;
89
89
  * Set a data value on an element
90
90
  */
91
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;
92
108
 
93
109
  export {};
package/types/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './attribute';
2
2
  export * from './data';
3
+ export * from './style';
@@ -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;