@oscarpalmer/atoms 0.12.0 → 0.13.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.
@@ -18,11 +18,7 @@ var _getValidElements = function(type, parent, filters) {
18
18
  }
19
19
  const indiced = [];
20
20
  const zeroed = [];
21
- const { length } = items;
22
- let position = Number(length);
23
- while (position--) {
24
- const index = length - position - 1;
25
- const item = items[index];
21
+ for (const item of items) {
26
22
  if (item.tabIndex === 0) {
27
23
  zeroed.push(item.element);
28
24
  } else {
@@ -18,11 +18,7 @@ var _getValidElements = function(type, parent, filters) {
18
18
  }
19
19
  const indiced = [];
20
20
  const zeroed = [];
21
- const { length } = items;
22
- let position = Number(length);
23
- while (position--) {
24
- const index = length - position - 1;
25
- const item = items[index];
21
+ for (const item of items) {
26
22
  if (item.tabIndex === 0) {
27
23
  zeroed.push(item.element);
28
24
  } else {
package/dist/js/index.js CHANGED
@@ -1,81 +1,70 @@
1
- // src/js/string.ts
2
- function createUuid() {
3
- return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, (substring) => (substring ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> substring / 4).toString(16));
4
- }
5
- function getString(value) {
6
- return typeof value === "string" ? value : typeof value?.toString === "function" ? value.toString() : String(value);
7
- }
8
- function isNullableOrWhitespace(value) {
9
- return value == null || getString(value).trim().length === 0;
10
- }
11
-
12
- // src/js/value.ts
13
- var _getValue = function(data, key) {
14
- if (typeof data !== "object" || data === null || /^(__proto__|constructor|prototype)$/i.test(key)) {
15
- return;
1
+ // src/js/element/index.ts
2
+ function findElements(selector, context) {
3
+ const contexts = context === undefined ? [document] : findElements(context);
4
+ const elements = [];
5
+ if (typeof selector === "string") {
6
+ for (const context2 of contexts) {
7
+ elements.push(...Array.from(context2.querySelectorAll(selector) ?? []));
8
+ }
9
+ return elements;
16
10
  }
17
- if (data instanceof Map) {
18
- return data.get(key);
11
+ const nodes = Array.isArray(selector) || selector instanceof NodeList ? selector : [selector];
12
+ for (const node of nodes) {
13
+ if (node instanceof Element && contexts.some((context2) => context2.contains(node))) {
14
+ elements.push(node);
15
+ }
19
16
  }
20
- return data[key];
21
- };
22
- var _setValue = function(data, key, value) {
23
- if (typeof data !== "object" || data === null || /^(__proto__|constructor|prototype)$/i.test(key)) {
17
+ return elements;
18
+ }
19
+ function findParentElement(origin, selector) {
20
+ if (origin == null || selector == null) {
24
21
  return;
25
22
  }
26
- if (data instanceof Map) {
27
- data.set(key, value);
28
- } else {
29
- data[key] = value;
23
+ function matches(element) {
24
+ return typeof selector === "string" ? element.matches?.(selector) ?? false : typeof selector === "function" ? selector(element) : false;
30
25
  }
31
- };
32
- function getValue(data, key) {
33
- if (typeof data !== "object" || data === null || isNullableOrWhitespace(key)) {
34
- return;
26
+ if (matches(origin)) {
27
+ return origin;
35
28
  }
36
- const parts = getString(key).split(".").reverse();
37
- let position = parts.length;
38
- let value = data;
39
- while (position--) {
40
- value = _getValue(value, parts[position]);
41
- if (value == null) {
42
- break;
29
+ let parent = origin.parentElement;
30
+ while (parent != null && !matches(parent)) {
31
+ if (parent === document.body) {
32
+ return;
43
33
  }
34
+ parent = parent.parentElement;
44
35
  }
45
- return value;
46
- }
47
- function isArrayOrObject(value) {
48
- return /^(array|object)$/i.test(value?.constructor?.name);
49
- }
50
- function isNullable(value) {
51
- return value == null;
36
+ return parent ?? undefined;
52
37
  }
53
- function isObject(value) {
54
- return value?.constructor?.name === "Object";
38
+ function getElementUnderPointer(skipIgnore) {
39
+ const elements = Array.from(document.querySelectorAll(":hover")).filter((element) => {
40
+ if (/^head$/i.test(element.tagName)) {
41
+ return false;
42
+ }
43
+ const style = getComputedStyle(element);
44
+ return typeof skipIgnore === "boolean" && skipIgnore || style.pointerEvents !== "none" && style.visibility !== "hidden";
45
+ });
46
+ return elements[elements.length - 1];
55
47
  }
56
- function setValue(data, key, value) {
57
- if (typeof data !== "object" || data === null || isNullableOrWhitespace(key)) {
58
- return data;
48
+ function getTextDirection(element) {
49
+ const direction = element.getAttribute("dir");
50
+ if (direction !== null && /^(ltr|rtl)$/i.test(direction)) {
51
+ return direction.toLowerCase();
59
52
  }
60
- const parts = getString(key).split(".").reverse();
61
- let position = parts.length;
62
- let target = data;
63
- while (position--) {
64
- const key2 = parts[position];
65
- if (position === 0) {
66
- _setValue(target, key2, value);
67
- break;
68
- }
69
- let next = _getValue(target, key2);
70
- if (typeof next !== "object" || next === null) {
71
- next = /^\d+$/.test(parts[position - 1]) ? [] : {};
72
- target[key2] = next;
73
- }
74
- target = next;
53
+ return getComputedStyle?.(element)?.direction === "rtl" ? "rtl" : "ltr";
54
+ }
55
+ // src/js/event.ts
56
+ function getEventPosition(event) {
57
+ let x;
58
+ let y;
59
+ if (event instanceof MouseEvent) {
60
+ x = event.clientX;
61
+ y = event.clientY;
62
+ } else if (event instanceof TouchEvent) {
63
+ x = event.touches[0]?.clientX;
64
+ y = event.touches[0]?.clientY;
75
65
  }
76
- return data;
66
+ return typeof x === "number" && typeof y === "number" ? { x, y } : undefined;
77
67
  }
78
-
79
68
  // src/js/number.ts
80
69
  function clampNumber(value, min, max) {
81
70
  return Math.min(Math.max(getNumber(value), getNumber(min)), getNumber(max));
@@ -107,7 +96,16 @@ function getNumber(value) {
107
96
  }
108
97
  return +(/^0x[0-9a-f]+$/i.test(trimmed) ? trimmed : trimmed.replace(/_/g, ""));
109
98
  }
110
-
99
+ // src/js/string.ts
100
+ function createUuid() {
101
+ return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, (substring) => (substring ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> substring / 4).toString(16));
102
+ }
103
+ function getString(value) {
104
+ return typeof value === "string" ? value : typeof value?.toString === "function" ? value.toString() : String(value);
105
+ }
106
+ function isNullableOrWhitespace(value) {
107
+ return value == null || getString(value).trim().length === 0;
108
+ }
111
109
  // src/js/timer.ts
112
110
  function repeat(callback, options) {
113
111
  const count = typeof options?.count === "number" ? options.count : Infinity;
@@ -191,74 +189,68 @@ class Timer {
191
189
  return work("stop", this, this.state, this.options);
192
190
  }
193
191
  }
194
-
195
- // src/js/element/index.ts
196
- function findElements(selector, context) {
197
- const contexts = context === undefined ? [document] : findElements(context);
198
- const elements = [];
199
- if (typeof selector === "string") {
200
- for (const context2 of contexts) {
201
- elements.push(...Array.from(context2.querySelectorAll(selector) ?? []));
202
- }
203
- return elements;
192
+ // src/js/value.ts
193
+ var _getValue = function(data, key) {
194
+ if (typeof data !== "object" || data === null || /^(__proto__|constructor|prototype)$/i.test(key)) {
195
+ return;
204
196
  }
205
- const nodes = Array.isArray(selector) || selector instanceof NodeList ? selector : [selector];
206
- for (const node of nodes) {
207
- if (node instanceof Element && contexts.some((context2) => context2.contains(node))) {
208
- elements.push(node);
209
- }
197
+ if (data instanceof Map) {
198
+ return data.get(key);
210
199
  }
211
- return elements;
212
- }
213
- function findParentElement(origin, selector) {
214
- if (origin == null || selector == null) {
200
+ return data[key];
201
+ };
202
+ var _setValue = function(data, key, value) {
203
+ if (typeof data !== "object" || data === null || /^(__proto__|constructor|prototype)$/i.test(key)) {
215
204
  return;
216
205
  }
217
- function matches(element) {
218
- return typeof selector === "string" ? element.matches?.(selector) ?? false : typeof selector === "function" ? selector(element) : false;
206
+ if (data instanceof Map) {
207
+ data.set(key, value);
208
+ } else {
209
+ data[key] = value;
219
210
  }
220
- if (matches(origin)) {
221
- return origin;
211
+ };
212
+ function getValue(data, key) {
213
+ if (typeof data !== "object" || data === null || isNullableOrWhitespace(key)) {
214
+ return;
222
215
  }
223
- let parent = origin.parentElement;
224
- while (parent != null && !matches(parent)) {
225
- if (parent === document.body) {
226
- return;
216
+ const parts = getString(key).split(".");
217
+ let value = data;
218
+ for (const part of parts) {
219
+ value = _getValue(value, part);
220
+ if (value == null) {
221
+ break;
227
222
  }
228
- parent = parent.parentElement;
229
223
  }
230
- return parent ?? undefined;
224
+ return value;
231
225
  }
232
- function getElementUnderPointer(skipIgnore) {
233
- const elements = Array.from(document.querySelectorAll(":hover")).filter((element) => {
234
- if (/^head$/i.test(element.tagName)) {
235
- return false;
236
- }
237
- const style = getComputedStyle(element);
238
- return typeof skipIgnore === "boolean" && skipIgnore || style.pointerEvents !== "none" && style.visibility !== "hidden";
239
- });
240
- return elements[elements.length - 1];
226
+ function isArrayOrObject(value) {
227
+ return /^(array|object)$/i.test(value?.constructor?.name);
241
228
  }
242
- function getTextDirection(element) {
243
- const direction = element.getAttribute("dir");
244
- if (direction !== null && /^(ltr|rtl)$/i.test(direction)) {
245
- return direction.toLowerCase();
246
- }
247
- return getComputedStyle?.(element)?.direction === "rtl" ? "rtl" : "ltr";
229
+ function isNullable(value) {
230
+ return value == null;
248
231
  }
249
-
250
- // src/js/event.ts
251
- function getEventPosition(event) {
252
- let x;
253
- let y;
254
- if (event instanceof MouseEvent) {
255
- x = event.clientX;
256
- y = event.clientY;
257
- } else if (event instanceof TouchEvent) {
258
- x = event.touches[0]?.clientX;
259
- y = event.touches[0]?.clientY;
232
+ function isObject(value) {
233
+ return /^object$/i.test(value?.constructor?.name);
234
+ }
235
+ function setValue(data, key, value) {
236
+ if (typeof data !== "object" || data === null || isNullableOrWhitespace(key)) {
237
+ return data;
260
238
  }
261
- return typeof x === "number" && typeof y === "number" ? { x, y } : undefined;
239
+ const parts = getString(key).split(".");
240
+ let target = data;
241
+ for (const part of parts) {
242
+ if (parts.indexOf(part) === parts.length - 1) {
243
+ _setValue(target, part, value);
244
+ break;
245
+ }
246
+ let next = _getValue(target, part);
247
+ if (typeof next !== "object" || next === null) {
248
+ next = /^\d+$/.test(part) ? [] : {};
249
+ target[part] = next;
250
+ }
251
+ target = next;
252
+ }
253
+ return data;
262
254
  }
263
255
  export {
264
256
  wait,
@@ -277,5 +269,6 @@ export {
277
269
  findParentElement,
278
270
  findElements,
279
271
  createUuid,
280
- clampNumber
272
+ clampNumber,
273
+ Timer
281
274
  };
package/dist/js/timer.js CHANGED
@@ -83,5 +83,6 @@ class Timer {
83
83
  }
84
84
  export {
85
85
  wait,
86
- repeat
86
+ repeat,
87
+ Timer
87
88
  };
package/dist/js/timer.mjs CHANGED
@@ -83,5 +83,6 @@ class Timer {
83
83
  }
84
84
  export {
85
85
  wait,
86
- repeat
86
+ repeat,
87
+ Timer
87
88
  };
package/dist/js/value.js CHANGED
@@ -1,7 +1,4 @@
1
1
  // src/js/string.ts
2
- function createUuid() {
3
- return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, (substring) => (substring ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> substring / 4).toString(16));
4
- }
5
2
  function getString(value) {
6
3
  return typeof value === "string" ? value : typeof value?.toString === "function" ? value.toString() : String(value);
7
4
  }
@@ -33,11 +30,10 @@ function getValue(data, key) {
33
30
  if (typeof data !== "object" || data === null || isNullableOrWhitespace(key)) {
34
31
  return;
35
32
  }
36
- const parts = getString(key).split(".").reverse();
37
- let position = parts.length;
33
+ const parts = getString(key).split(".");
38
34
  let value = data;
39
- while (position--) {
40
- value = _getValue(value, parts[position]);
35
+ for (const part of parts) {
36
+ value = _getValue(value, part);
41
37
  if (value == null) {
42
38
  break;
43
39
  }
@@ -51,25 +47,23 @@ function isNullable(value) {
51
47
  return value == null;
52
48
  }
53
49
  function isObject(value) {
54
- return value?.constructor?.name === "Object";
50
+ return /^object$/i.test(value?.constructor?.name);
55
51
  }
56
52
  function setValue(data, key, value) {
57
53
  if (typeof data !== "object" || data === null || isNullableOrWhitespace(key)) {
58
54
  return data;
59
55
  }
60
- const parts = getString(key).split(".").reverse();
61
- let position = parts.length;
56
+ const parts = getString(key).split(".");
62
57
  let target = data;
63
- while (position--) {
64
- const key2 = parts[position];
65
- if (position === 0) {
66
- _setValue(target, key2, value);
58
+ for (const part of parts) {
59
+ if (parts.indexOf(part) === parts.length - 1) {
60
+ _setValue(target, part, value);
67
61
  break;
68
62
  }
69
- let next = _getValue(target, key2);
63
+ let next = _getValue(target, part);
70
64
  if (typeof next !== "object" || next === null) {
71
- next = /^\d+$/.test(parts[position - 1]) ? [] : {};
72
- target[key2] = next;
65
+ next = /^\d+$/.test(part) ? [] : {};
66
+ target[part] = next;
73
67
  }
74
68
  target = next;
75
69
  }
package/dist/js/value.mjs CHANGED
@@ -23,11 +23,10 @@ function getValue(data, key) {
23
23
  if (typeof data !== "object" || data === null || isNullableOrWhitespace(key)) {
24
24
  return;
25
25
  }
26
- const parts = getString(key).split(".").reverse();
27
- let position = parts.length;
26
+ const parts = getString(key).split(".");
28
27
  let value = data;
29
- while (position--) {
30
- value = _getValue(value, parts[position]);
28
+ for (const part of parts) {
29
+ value = _getValue(value, part);
31
30
  if (value == null) {
32
31
  break;
33
32
  }
@@ -41,25 +40,23 @@ function isNullable(value) {
41
40
  return value == null;
42
41
  }
43
42
  function isObject(value) {
44
- return value?.constructor?.name === "Object";
43
+ return /^object$/i.test(value?.constructor?.name);
45
44
  }
46
45
  function setValue(data, key, value) {
47
46
  if (typeof data !== "object" || data === null || isNullableOrWhitespace(key)) {
48
47
  return data;
49
48
  }
50
- const parts = getString(key).split(".").reverse();
51
- let position = parts.length;
49
+ const parts = getString(key).split(".");
52
50
  let target = data;
53
- while (position--) {
54
- const key2 = parts[position];
55
- if (position === 0) {
56
- _setValue(target, key2, value);
51
+ for (const part of parts) {
52
+ if (parts.indexOf(part) === parts.length - 1) {
53
+ _setValue(target, part, value);
57
54
  break;
58
55
  }
59
- let next = _getValue(target, key2);
56
+ let next = _getValue(target, part);
60
57
  if (typeof next !== "object" || next === null) {
61
- next = /^\d+$/.test(parts[position - 1]) ? [] : {};
62
- target[key2] = next;
58
+ next = /^\d+$/.test(part) ? [] : {};
59
+ target[part] = next;
63
60
  }
64
61
  target = next;
65
62
  }
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "@biomejs/biome": "^1.5",
9
9
  "@happy-dom/global-registrator": "^13.3",
10
10
  "bun": "^1.0",
11
- "sass": "^1.70",
11
+ "sass": "^1.71",
12
12
  "typescript": "^5.3"
13
13
  },
14
14
  "exports": {
@@ -83,10 +83,9 @@
83
83
  "url": "git+https://github.com/oscarpalmer/atoms.git"
84
84
  },
85
85
  "scripts": {
86
- "build": "bun run build:css && bun run build:js && bun run build:mjs && bun run types",
86
+ "build": "bun run build:css && bun run build:js && bun run types",
87
87
  "build:css": "bunx sass ./src/css:./dist/css --no-source-map",
88
- "build:js": "bunx bun ./.bun.ts",
89
- "build:mjs": "bunx bun ./.bun.ts --mjs",
88
+ "build:js": "bunx bun ./.bun.ts && bunx bun ./.bun.ts --mjs",
90
89
  "test": "bun test --preload ./test/_preload.ts --coverage",
91
90
  "types": "bunx tsc -p ./tsconfig.json",
92
91
  "watch:css": "bunx sass ./src/css:./dist/css --no-source-map --watch",
@@ -94,5 +93,5 @@
94
93
  },
95
94
  "type": "module",
96
95
  "types": "./types/index.d.ts",
97
- "version": "0.12.0"
96
+ "version": "0.13.0"
98
97
  }
@@ -87,14 +87,7 @@ function _getValidElements(
87
87
  const indiced: Array<Array<FocusableElement>> = [];
88
88
  const zeroed: Array<FocusableElement> = [];
89
89
 
90
- const {length} = items;
91
-
92
- let position = Number(length);
93
-
94
- while (position--) {
95
- const index = length - position - 1;
96
- const item = items[index];
97
-
90
+ for (const item of items) {
98
91
  if (item.tabIndex === 0) {
99
92
  zeroed.push(item.element);
100
93
  } else {
package/src/js/number.ts CHANGED
@@ -9,6 +9,7 @@ export function clampNumber(value: number, min: number, max: number): number {
9
9
  /**
10
10
  * - Gets the number value from an unknown value
11
11
  * - Returns `NaN` if the value is `undefined`, `null`, or cannot be parsed
12
+ * - Based on Lodash :-)
12
13
  */
13
14
  export function getNumber(value: unknown): number {
14
15
  if (typeof value === 'number') {
@@ -45,5 +46,7 @@ export function getNumber(value: unknown): number {
45
46
  return parseInt(trimmed.slice(2), isBinary ? 2 : 8);
46
47
  }
47
48
 
48
- return +(/^0x[0-9a-f]+$/i.test(trimmed) ? trimmed : trimmed.replace(/_/g, ''));
49
+ return +(/^0x[0-9a-f]+$/i.test(trimmed)
50
+ ? trimmed
51
+ : trimmed.replace(/_/g, ''));
49
52
  }
package/src/js/timer.ts CHANGED
@@ -42,7 +42,7 @@ type WorkType = 'restart' | 'start' | 'stop';
42
42
  /**
43
43
  * A timer that can be started, stopped, and restarted as neeeded
44
44
  */
45
- class Timer {
45
+ export class Timer {
46
46
  private declare readonly state: TimerState;
47
47
  private declare readonly options: TimerOptions;
48
48
 
package/src/js/value.ts CHANGED
@@ -57,13 +57,12 @@ export function getValue(data: ValueObject, key: Key): unknown {
57
57
  return undefined;
58
58
  }
59
59
 
60
- const parts = getString(key).split('.').reverse();
60
+ const parts = getString(key).split('.');
61
61
 
62
- let position = parts.length;
63
62
  let value = data;
64
63
 
65
- while (position--) {
66
- value = _getValue(value, parts[position]) as ValueObject;
64
+ for (const part of parts) {
65
+ value = _getValue(value, part) as ValueObject;
67
66
 
68
67
  if (value == null) {
69
68
  break;
@@ -91,7 +90,7 @@ export function isNullable(value: unknown): value is undefined | null {
91
90
  * Is the value a generic object?
92
91
  */
93
92
  export function isObject(value: unknown): value is GenericObject {
94
- return (value as GenericObject)?.constructor?.name === 'Object';
93
+ return /^object$/i.test((value as GenericObject)?.constructor?.name);
95
94
  }
96
95
 
97
96
  /**
@@ -113,26 +112,23 @@ export function setValue<Model extends ValueObject>(
113
112
  return data;
114
113
  }
115
114
 
116
- const parts = getString(key).split('.').reverse();
115
+ const parts = getString(key).split('.');
117
116
 
118
- let position = parts.length;
119
117
  let target: ValueObject = data;
120
118
 
121
- while (position--) {
122
- const key = parts[position] as never;
123
-
124
- if (position === 0) {
125
- _setValue(target, key, value);
119
+ for (const part of parts) {
120
+ if (parts.indexOf(part) === parts.length - 1) {
121
+ _setValue(target, part, value);
126
122
 
127
123
  break;
128
124
  }
129
125
 
130
- let next = _getValue(target, key);
126
+ let next = _getValue(target, part);
131
127
 
132
128
  if (typeof next !== 'object' || next === null) {
133
- next = /^\d+$/.test(parts[position - 1]) ? [] : {};
129
+ next = /^\d+$/.test(part) ? [] : {};
134
130
 
135
- target[key] = next as never;
131
+ target[part as never] = next as never;
136
132
  }
137
133
 
138
134
  target = next as ValueObject;
package/types/number.d.ts CHANGED
@@ -5,5 +5,6 @@ export declare function clampNumber(value: number, min: number, max: number): nu
5
5
  /**
6
6
  * - Gets the number value from an unknown value
7
7
  * - Returns `NaN` if the value is `undefined`, `null`, or cannot be parsed
8
+ * - Based on Lodash :-)
8
9
  */
9
10
  export declare function getNumber(value: unknown): number;
package/types/timer.d.ts CHANGED
@@ -25,7 +25,7 @@ type Options = {
25
25
  /**
26
26
  * A timer that can be started, stopped, and restarted as neeeded
27
27
  */
28
- declare class Timer {
28
+ export declare class Timer {
29
29
  private readonly state;
30
30
  private readonly options;
31
31
  /**