@oscarpalmer/toretto 0.34.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;
@@ -53,12 +53,16 @@ function getString(value) {
53
53
  const asString = String(value.valueOf?.() ?? value);
54
54
  return asString.startsWith("[object ") ? JSON.stringify(value) : asString;
55
55
  }
56
+ function ignoreKey(key) {
57
+ return EXPRESSION_IGNORED.test(key);
58
+ }
56
59
  function join(value, delimiter) {
57
60
  return compact(value).map(getString).join(typeof delimiter === "string" ? delimiter : "");
58
61
  }
59
62
  function words(value) {
60
63
  return typeof value === "string" ? value.match(EXPRESSION_WORDS) ?? [] : [];
61
64
  }
65
+ var EXPRESSION_IGNORED = /(^|\.)(__proto__|constructor|prototype)(\.|$)/i;
62
66
  var EXPRESSION_WORDS = /[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+/g;
63
67
  function isNullableOrWhitespace(value) {
64
68
  return value == null || EXPRESSION_WHITESPACE$1.test(getString(value));
@@ -237,7 +241,7 @@ function calculate() {
237
241
  var TOTAL = 10;
238
242
  var TRIM_PART = 2;
239
243
  var TRIM_TOTAL = 4;
240
- await calculate();
244
+ calculate().then((value) => {});
241
245
  function clamp(value, minimum, maximum, loop) {
242
246
  if (![
243
247
  value,
@@ -364,7 +368,7 @@ function toCaseCallback(value) {
364
368
  const { capitalizeAny, capitalizeFirst, type } = this;
365
369
  const parts = words(value);
366
370
  const partsLength = parts.length;
367
- const result = [];
371
+ const cased = [];
368
372
  for (let partIndex = 0; partIndex < partsLength; partIndex += 1) {
369
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("-");
370
374
  const itemsLength = items.length;
@@ -377,9 +381,9 @@ function toCaseCallback(value) {
377
381
  else partResult.push(capitalize(item));
378
382
  itemCount += 1;
379
383
  }
380
- result.push(join(partResult, delimiters[type]));
384
+ cased.push(join(partResult, delimiters[type]));
381
385
  }
382
- return join(result, delimiters[type]);
386
+ return join(cased, delimiters[type]);
383
387
  }
384
388
  var EXPRESSION_CAMEL_CASE = /(\p{Ll})(\p{Lu})/gu;
385
389
  var EXPRESSION_ACRONYM = /(\p{Lu}*)(\p{Lu})(\p{Ll}+)/gu;
@@ -399,6 +403,67 @@ function parse(value, reviver) {
399
403
  return;
400
404
  }
401
405
  }
406
+ function findKey(needle, haystack) {
407
+ const keys = Object.keys(haystack);
408
+ const index = keys.map((key) => key.toLowerCase()).indexOf(needle.toLowerCase());
409
+ return index > -1 ? keys[index] : needle;
410
+ }
411
+ function getPaths(path, lowercase) {
412
+ const normalized = lowercase ? path.toLowerCase() : path;
413
+ if (!EXPRESSION_NESTED.test(normalized)) return normalized;
414
+ return normalized.replace(EXPRESSION_BRACKET, ".$1").replace(EXPRESSION_DOTS, "").split(".");
415
+ }
416
+ function handleValue(data, path, value, get, ignoreCase) {
417
+ if (typeof data === "object" && data !== null && !ignoreKey(path)) {
418
+ const key = ignoreCase ? findKey(path, data) : path;
419
+ if (get) return data[key];
420
+ data[key] = typeof value === "function" ? value(data[key]) : value;
421
+ }
422
+ }
423
+ var EXPRESSION_BRACKET = /\[(\w+)\]/g;
424
+ var EXPRESSION_DOTS = /^\.|\.$/g;
425
+ var EXPRESSION_NESTED = /\.|\[\w+\]/;
426
+ function getValue(data, path, ignoreCase) {
427
+ if (typeof data !== "object" || data === null || typeof path !== "string" || path.trim().length === 0) return;
428
+ const shouldIgnoreCase = ignoreCase === true;
429
+ const paths = getPaths(path, shouldIgnoreCase);
430
+ if (typeof paths === "string") return handleValue(data, paths, null, true, shouldIgnoreCase);
431
+ const { length } = paths;
432
+ let index = 0;
433
+ let value = data;
434
+ while (index < length && value != null) value = handleValue(value, paths[index++], null, true, shouldIgnoreCase);
435
+ return value;
436
+ }
437
+ function getTemplateOptions(input) {
438
+ const options = isPlainObject(input) ? input : {};
439
+ return {
440
+ ignoreCase: options.ignoreCase === true,
441
+ pattern: options.pattern instanceof RegExp ? options.pattern : EXPRESSION_VARIABLE
442
+ };
443
+ }
444
+ function handleTemplate(value, pattern, ignoreCase, variables) {
445
+ if (typeof value !== "string") return "";
446
+ if (typeof variables !== "object" || variables === null) return value;
447
+ const values = {};
448
+ return value.replace(pattern, (_, key) => {
449
+ if (values[key] == null) {
450
+ const templateValue = getValue(variables, key, ignoreCase);
451
+ values[key] = templateValue == null ? "" : getString(templateValue);
452
+ }
453
+ return values[key];
454
+ });
455
+ }
456
+ function template(value, variables, options) {
457
+ const { ignoreCase, pattern } = getTemplateOptions(options);
458
+ return handleTemplate(value, pattern, ignoreCase, variables);
459
+ }
460
+ template.initialize = function(options) {
461
+ const { ignoreCase, pattern } = getTemplateOptions(options);
462
+ return (value, variables) => {
463
+ return handleTemplate(value, pattern, ignoreCase, variables);
464
+ };
465
+ };
466
+ var EXPRESSION_VARIABLE = /{{([\s\S]+?)}}/g;
402
467
  function getBoolean(value, defaultValue) {
403
468
  return typeof value === "boolean" ? value : defaultValue ?? false;
404
469
  }
@@ -591,14 +656,15 @@ function on(target, type, listener, options) {
591
656
  }
592
657
  const PROPERTY_DETAIL = "detail";
593
658
  function findAncestor(origin, selector) {
594
- if (!(origin instanceof Element) || selector == null) return null;
659
+ const element = getElement(origin);
660
+ if (element == null || selector == null) return null;
595
661
  if (typeof selector === "string") {
596
- if (origin.matches?.(selector)) return origin;
597
- return origin.closest(selector);
662
+ if (element.matches?.(selector)) return element;
663
+ return element.closest(selector);
598
664
  }
599
665
  if (typeof selector !== "function") return null;
600
- if (selector(origin)) return origin;
601
- let parent = origin.parentElement;
666
+ if (selector(element)) return element;
667
+ let parent = element.parentElement;
602
668
  while (parent != null && !selector(parent)) {
603
669
  if (parent === document.body) return null;
604
670
  parent = parent.parentElement;
@@ -637,6 +703,10 @@ function getDistance(origin, target) {
637
703
  const preceding = comparison & Node.DOCUMENT_POSITION_PRECEDING;
638
704
  return traverse(preceding ? origin : target, preceding ? target : origin) ?? -1;
639
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
+ }
640
710
  function traverse(from, to) {
641
711
  let current = from;
642
712
  let distance = 0;
@@ -917,18 +987,18 @@ function createHtml(value) {
917
987
  return parsed.body.innerHTML;
918
988
  }
919
989
  function createTemplate(value, options) {
920
- const template = document.createElement(TEMPLATE_TAG);
921
- template.innerHTML = createHtml(value);
922
- if (typeof value === "string" && options.cache) templates[value] = template;
923
- return template;
990
+ const template$1 = document.createElement(TEMPLATE_TAG);
991
+ template$1.innerHTML = createHtml(value);
992
+ if (typeof value === "string" && options.cache) templates[value] = template$1;
993
+ return template$1;
924
994
  }
925
995
  function getHtml(value) {
926
996
  return `${TEMPORARY_ELEMENT}${typeof value === "string" ? value : value.innerHTML}${TEMPORARY_ELEMENT}`;
927
997
  }
928
998
  function getNodes(value, options) {
929
999
  if (typeof value !== "string" && !(value instanceof HTMLTemplateElement)) return [];
930
- const template = getTemplate(value, options);
931
- return template == null ? [] : [...template.content.cloneNode(true).childNodes];
1000
+ const template$1 = getTemplate(value, options);
1001
+ return template$1 == null ? [] : [...template$1.content.cloneNode(true).childNodes];
932
1002
  }
933
1003
  function getOptions(input) {
934
1004
  const options = isPlainObject(input) ? input : {};
@@ -942,8 +1012,8 @@ function getParser() {
942
1012
  function getTemplate(value, options) {
943
1013
  if (value instanceof HTMLTemplateElement) return createTemplate(value, options);
944
1014
  if (value.trim().length === 0) return;
945
- let template = templates[value];
946
- if (template != null) return template;
1015
+ let template$1 = templates[value];
1016
+ if (template$1 != null) return template$1;
947
1017
  const element = EXPRESSION_ID.test(value) ? document.querySelector(`#${value}`) : null;
948
1018
  return createTemplate(element instanceof HTMLTemplateElement ? element : value, options);
949
1019
  }
@@ -953,14 +1023,14 @@ const html = ((value, options) => {
953
1023
  html.clear = () => {
954
1024
  templates = {};
955
1025
  };
956
- html.remove = (template) => {
957
- if (typeof template !== "string" || templates[template] == null) return;
1026
+ html.remove = (template$1) => {
1027
+ if (typeof template$1 !== "string" || templates[template$1] == null) return;
958
1028
  const keys = Object.keys(templates);
959
1029
  const { length } = keys;
960
1030
  const updated = {};
961
1031
  for (let index = 0; index < length; index += 1) {
962
1032
  const key = keys[index];
963
- if (key !== template) updated[key] = templates[key];
1033
+ if (key !== template$1) updated[key] = templates[key];
964
1034
  }
965
1035
  templates = updated;
966
1036
  };
package/package.json CHANGED
@@ -4,16 +4,16 @@
4
4
  "url": "https://oscarpalmer.se"
5
5
  },
6
6
  "dependencies": {
7
- "@oscarpalmer/atoms": "^0.124"
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.21",
15
- "oxlint": "^1.36",
16
- "rolldown": "1.0.0-beta.57",
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.34.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
  *