@oscarpalmer/toretto 0.35.0 → 0.36.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.
@@ -1,12 +1,13 @@
1
1
  function findAncestor(origin, selector) {
2
- if (!(origin instanceof Element) || selector == null) return null;
2
+ const element = getElement(origin);
3
+ if (element == null || selector == null) return null;
3
4
  if (typeof selector === "string") {
4
- if (origin.matches?.(selector)) return origin;
5
- return origin.closest(selector);
5
+ if (element.matches?.(selector)) return element;
6
+ return element.closest(selector);
6
7
  }
7
8
  if (typeof selector !== "function") return null;
8
- if (selector(origin)) return origin;
9
- let parent = origin.parentElement;
9
+ if (selector(element)) return element;
10
+ let parent = element.parentElement;
10
11
  while (parent != null && !selector(parent)) {
11
12
  if (parent === document.body) return null;
12
13
  parent = parent.parentElement;
@@ -45,6 +46,10 @@ function getDistance(origin, target) {
45
46
  const preceding = comparison & Node.DOCUMENT_POSITION_PRECEDING;
46
47
  return traverse(preceding ? origin : target, preceding ? target : origin) ?? -1;
47
48
  }
49
+ function getElement(origin) {
50
+ if (origin instanceof Element) return origin;
51
+ return origin instanceof Event && origin.target instanceof Element ? origin.target : void 0;
52
+ }
48
53
  function traverse(from, to) {
49
54
  let current = from;
50
55
  let distance = 0;
@@ -368,7 +368,7 @@ function toCaseCallback(value) {
368
368
  const { capitalizeAny, capitalizeFirst, type } = this;
369
369
  const parts = words(value);
370
370
  const partsLength = parts.length;
371
- const result = [];
371
+ const cased = [];
372
372
  for (let partIndex = 0; partIndex < partsLength; partIndex += 1) {
373
373
  const items = parts[partIndex].replace(EXPRESSION_ACRONYM, (full, one, two, three) => three === "s" ? full : `${one}-${two}${three}`).replace(EXPRESSION_CAMEL_CASE, REPLACEMENT_CAMEL_CASE).split("-");
374
374
  const itemsLength = items.length;
@@ -381,9 +381,9 @@ function toCaseCallback(value) {
381
381
  else partResult.push(capitalize(item));
382
382
  itemCount += 1;
383
383
  }
384
- result.push(join(partResult, delimiters[type]));
384
+ cased.push(join(partResult, delimiters[type]));
385
385
  }
386
- return join(result, delimiters[type]);
386
+ return join(cased, delimiters[type]);
387
387
  }
388
388
  var EXPRESSION_CAMEL_CASE = /(\p{Ll})(\p{Lu})/gu;
389
389
  var EXPRESSION_ACRONYM = /(\p{Lu}*)(\p{Lu})(\p{Ll}+)/gu;
@@ -417,7 +417,7 @@ function handleValue(data, path, value, get, ignoreCase) {
417
417
  if (typeof data === "object" && data !== null && !ignoreKey(path)) {
418
418
  const key = ignoreCase ? findKey(path, data) : path;
419
419
  if (get) return data[key];
420
- data[key] = value;
420
+ data[key] = typeof value === "function" ? value(data[key]) : value;
421
421
  }
422
422
  }
423
423
  var EXPRESSION_BRACKET = /\[(\w+)\]/g;
@@ -656,14 +656,15 @@ function on(target, type, listener, options) {
656
656
  }
657
657
  const PROPERTY_DETAIL = "detail";
658
658
  function findAncestor(origin, selector) {
659
- if (!(origin instanceof Element) || selector == null) return null;
659
+ const element = getElement(origin);
660
+ if (element == null || selector == null) return null;
660
661
  if (typeof selector === "string") {
661
- if (origin.matches?.(selector)) return origin;
662
- return origin.closest(selector);
662
+ if (element.matches?.(selector)) return element;
663
+ return element.closest(selector);
663
664
  }
664
665
  if (typeof selector !== "function") return null;
665
- if (selector(origin)) return origin;
666
- let parent = origin.parentElement;
666
+ if (selector(element)) return element;
667
+ let parent = element.parentElement;
667
668
  while (parent != null && !selector(parent)) {
668
669
  if (parent === document.body) return null;
669
670
  parent = parent.parentElement;
@@ -702,6 +703,10 @@ function getDistance(origin, target) {
702
703
  const preceding = comparison & Node.DOCUMENT_POSITION_PRECEDING;
703
704
  return traverse(preceding ? origin : target, preceding ? target : origin) ?? -1;
704
705
  }
706
+ function getElement(origin) {
707
+ if (origin instanceof Element) return origin;
708
+ return origin instanceof Event && origin.target instanceof Element ? origin.target : void 0;
709
+ }
705
710
  function traverse(from, to) {
706
711
  let current = from;
707
712
  let distance = 0;
package/package.json CHANGED
@@ -4,16 +4,16 @@
4
4
  "url": "https://oscarpalmer.se"
5
5
  },
6
6
  "dependencies": {
7
- "@oscarpalmer/atoms": "^0.127"
7
+ "@oscarpalmer/atoms": "^0.128.1"
8
8
  },
9
9
  "description": "A collection of badass DOM utilities.",
10
10
  "devDependencies": {
11
11
  "@types/node": "^25",
12
12
  "@vitest/coverage-istanbul": "^4",
13
13
  "jsdom": "^27.4",
14
- "oxfmt": "^0.22",
15
- "oxlint": "^1.38",
16
- "rolldown": "1.0.0-beta.58",
14
+ "oxfmt": "^0.24",
15
+ "oxlint": "^1.39",
16
+ "rolldown": "1.0.0-beta.60",
17
17
  "tslib": "^2.8",
18
18
  "typescript": "^5.9",
19
19
  "vite": "8.0.0-beta.5",
@@ -93,5 +93,5 @@
93
93
  },
94
94
  "type": "module",
95
95
  "types": "types/index.d.ts",
96
- "version": "0.35.0"
96
+ "version": "0.36.0"
97
97
  }
@@ -3,35 +3,37 @@
3
3
  *
4
4
  * - If no match is found, `null` is returned
5
5
  * - _(If you want to search upwards, downwards, and sideways, use {@link findRelatives})_
6
- * @param origin Element to start from
6
+ * @param origin Origin to start from
7
7
  * @param selector Selector to match
8
8
  * @returns Found ancestor or `null`
9
9
  */
10
10
  export function findAncestor(
11
- origin: Element,
11
+ origin: Element | Event | EventTarget,
12
12
  selector: string | ((element: Element) => boolean),
13
13
  ): Element | null {
14
- if (!(origin instanceof Element) || selector == null) {
14
+ const element = getElement(origin);
15
+
16
+ if (element == null || selector == null) {
15
17
  return null;
16
18
  }
17
19
 
18
20
  if (typeof selector === 'string') {
19
- if (origin.matches?.(selector)) {
20
- return origin;
21
+ if (element.matches?.(selector)) {
22
+ return element;
21
23
  }
22
24
 
23
- return origin.closest(selector);
25
+ return element.closest(selector);
24
26
  }
25
27
 
26
28
  if (typeof selector !== 'function') {
27
29
  return null;
28
30
  }
29
31
 
30
- if (selector(origin)) {
31
- return origin;
32
+ if (selector(element)) {
33
+ return element;
32
34
  }
33
35
 
34
- let parent: Element | null = origin.parentElement;
36
+ let parent: Element | null = element.parentElement;
35
37
 
36
38
  while (parent != null && !selector(parent)) {
37
39
  if (parent === document.body) {
@@ -95,9 +97,7 @@ export function findRelatives(
95
97
  }
96
98
  }
97
99
 
98
- return distances
99
- .filter(found => found.distance === minimum)
100
- .map(found => found.element);
100
+ return distances.filter(found => found.distance === minimum).map(found => found.element);
101
101
  }
102
102
 
103
103
  /**
@@ -132,6 +132,14 @@ export function getDistance(origin: Element, target: Element): number {
132
132
  return traverse(preceding ? origin : target, preceding ? target : origin) ?? -1;
133
133
  }
134
134
 
135
+ function getElement(origin: unknown): Element | undefined {
136
+ if (origin instanceof Element) {
137
+ return origin;
138
+ }
139
+
140
+ return origin instanceof Event && origin.target instanceof Element ? origin.target : undefined;
141
+ }
142
+
135
143
  function traverse(from: Element, to: Element): number | undefined {
136
144
  let current = from;
137
145
  let distance = 0;
@@ -3,11 +3,11 @@
3
3
  *
4
4
  * - If no match is found, `null` is returned
5
5
  * - _(If you want to search upwards, downwards, and sideways, use {@link findRelatives})_
6
- * @param origin Element to start from
6
+ * @param origin Origin to start from
7
7
  * @param selector Selector to match
8
8
  * @returns Found ancestor or `null`
9
9
  */
10
- export declare function findAncestor(origin: Element, selector: string | ((element: Element) => boolean)): Element | null;
10
+ export declare function findAncestor(origin: Element | Event | EventTarget, selector: string | ((element: Element) => boolean)): Element | null;
11
11
  /**
12
12
  * Finds the closest elements to the origin element that matches the selector
13
13
  *