@oscarpalmer/toretto 0.3.0 → 0.4.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,60 @@ 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
+ }
86
150
  export {
151
+ setData,
87
152
  setAttributes,
88
153
  setAttribute,
89
154
  isInvalidBooleanAttribute,
90
155
  isEmptyNonBooleanAttribute,
91
156
  isBooleanAttribute,
92
157
  isBadAttribute,
158
+ getData,
93
159
  booleanAttributes
94
160
  };
package/dist/index.mjs CHANGED
@@ -1,2 +1,3 @@
1
1
  // src/index.ts
2
2
  export * from "./attribute";
3
+ export * from "./data";
@@ -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/package.json CHANGED
@@ -56,5 +56,5 @@
56
56
  },
57
57
  "type": "module",
58
58
  "types": "types/index.d.cts",
59
- "version": "0.3.0"
59
+ "version": "0.4.0"
60
60
  }
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 as an object
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,2 @@
1
1
  export * from './attribute';
2
+ export * from './data';
@@ -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
+ }
@@ -0,0 +1,17 @@
1
+ import type { PlainObject } from '@oscarpalmer/atoms/models';
2
+ /**
3
+ * Get data values from an element as an object
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,53 @@ 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 as an object
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;
44
92
 
45
93
  export {};
package/types/index.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from './attribute';
2
+ export * from './data';
@@ -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;