@rootreeweb/js-lib 4.0.10

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.
Files changed (218) hide show
  1. package/README.md +49 -0
  2. package/dist/cjs/browser/dom.cjs +95 -0
  3. package/dist/cjs/browser/dom.d.ts +44 -0
  4. package/dist/cjs/browser/dom.d.ts.map +1 -0
  5. package/dist/cjs/browser/dom.js.map +1 -0
  6. package/dist/cjs/browser/download.cjs +40 -0
  7. package/dist/cjs/browser/download.d.ts +16 -0
  8. package/dist/cjs/browser/download.d.ts.map +1 -0
  9. package/dist/cjs/browser/download.js.map +1 -0
  10. package/dist/cjs/browser/index.cjs +20 -0
  11. package/dist/cjs/browser/index.d.ts +4 -0
  12. package/dist/cjs/browser/index.d.ts.map +1 -0
  13. package/dist/cjs/browser/index.js.map +1 -0
  14. package/dist/cjs/browser/queryOrThrow.cjs +42 -0
  15. package/dist/cjs/browser/queryOrThrow.d.ts +44 -0
  16. package/dist/cjs/browser/queryOrThrow.d.ts.map +1 -0
  17. package/dist/cjs/browser/queryOrThrow.js.map +1 -0
  18. package/dist/cjs/general/arrays.cjs +151 -0
  19. package/dist/cjs/general/arrays.d.ts +70 -0
  20. package/dist/cjs/general/arrays.d.ts.map +1 -0
  21. package/dist/cjs/general/arrays.js.map +1 -0
  22. package/dist/cjs/general/boolean.cjs +19 -0
  23. package/dist/cjs/general/boolean.d.ts +10 -0
  24. package/dist/cjs/general/boolean.d.ts.map +1 -0
  25. package/dist/cjs/general/boolean.js.map +1 -0
  26. package/dist/cjs/general/clock.cjs +22 -0
  27. package/dist/cjs/general/clock.d.ts +12 -0
  28. package/dist/cjs/general/clock.d.ts.map +1 -0
  29. package/dist/cjs/general/clock.js.map +1 -0
  30. package/dist/cjs/general/dates/create.cjs +81 -0
  31. package/dist/cjs/general/dates/create.d.ts +54 -0
  32. package/dist/cjs/general/dates/create.d.ts.map +1 -0
  33. package/dist/cjs/general/dates/create.js.map +1 -0
  34. package/dist/cjs/general/dates/dates.cjs +88 -0
  35. package/dist/cjs/general/dates/dates.d.ts +56 -0
  36. package/dist/cjs/general/dates/dates.d.ts.map +1 -0
  37. package/dist/cjs/general/dates/dates.js.map +1 -0
  38. package/dist/cjs/general/dates/get.cjs +77 -0
  39. package/dist/cjs/general/dates/get.d.ts +41 -0
  40. package/dist/cjs/general/dates/get.d.ts.map +1 -0
  41. package/dist/cjs/general/dates/get.js.map +1 -0
  42. package/dist/cjs/general/dates/index.cjs +20 -0
  43. package/dist/cjs/general/dates/index.d.ts +4 -0
  44. package/dist/cjs/general/dates/index.d.ts.map +1 -0
  45. package/dist/cjs/general/dates/index.js.map +1 -0
  46. package/dist/cjs/general/forms.cjs +31 -0
  47. package/dist/cjs/general/forms.d.ts +17 -0
  48. package/dist/cjs/general/forms.d.ts.map +1 -0
  49. package/dist/cjs/general/forms.js.map +1 -0
  50. package/dist/cjs/general/images.cjs +23 -0
  51. package/dist/cjs/general/images.d.ts +17 -0
  52. package/dist/cjs/general/images.d.ts.map +1 -0
  53. package/dist/cjs/general/images.js.map +1 -0
  54. package/dist/cjs/general/index.cjs +31 -0
  55. package/dist/cjs/general/index.d.ts +15 -0
  56. package/dist/cjs/general/index.d.ts.map +1 -0
  57. package/dist/cjs/general/index.js.map +1 -0
  58. package/dist/cjs/general/localization.cjs +199 -0
  59. package/dist/cjs/general/localization.d.ts +83 -0
  60. package/dist/cjs/general/localization.d.ts.map +1 -0
  61. package/dist/cjs/general/localization.js.map +1 -0
  62. package/dist/cjs/general/maps.cjs +63 -0
  63. package/dist/cjs/general/maps.d.ts +35 -0
  64. package/dist/cjs/general/maps.d.ts.map +1 -0
  65. package/dist/cjs/general/maps.js.map +1 -0
  66. package/dist/cjs/general/messenger/createMessenger.cjs +68 -0
  67. package/dist/cjs/general/messenger/createMessenger.d.ts +10 -0
  68. package/dist/cjs/general/messenger/createMessenger.d.ts.map +1 -0
  69. package/dist/cjs/general/messenger/createMessenger.js.map +1 -0
  70. package/dist/cjs/general/messenger/index.cjs +19 -0
  71. package/dist/cjs/general/messenger/index.d.ts +3 -0
  72. package/dist/cjs/general/messenger/index.d.ts.map +1 -0
  73. package/dist/cjs/general/messenger/index.js.map +1 -0
  74. package/dist/cjs/general/messenger/types.cjs +3 -0
  75. package/dist/cjs/general/messenger/types.d.ts +26 -0
  76. package/dist/cjs/general/messenger/types.d.ts.map +1 -0
  77. package/dist/cjs/general/messenger/types.js.map +1 -0
  78. package/dist/cjs/general/namedEntities.cjs +260 -0
  79. package/dist/cjs/general/namedEntities.d.ts +256 -0
  80. package/dist/cjs/general/namedEntities.d.ts.map +1 -0
  81. package/dist/cjs/general/namedEntities.js.map +1 -0
  82. package/dist/cjs/general/numbers.cjs +94 -0
  83. package/dist/cjs/general/numbers.d.ts +43 -0
  84. package/dist/cjs/general/numbers.d.ts.map +1 -0
  85. package/dist/cjs/general/numbers.js.map +1 -0
  86. package/dist/cjs/general/objects.cjs +98 -0
  87. package/dist/cjs/general/objects.d.ts +45 -0
  88. package/dist/cjs/general/objects.d.ts.map +1 -0
  89. package/dist/cjs/general/objects.js.map +1 -0
  90. package/dist/cjs/general/sorting.cjs +72 -0
  91. package/dist/cjs/general/sorting.d.ts +32 -0
  92. package/dist/cjs/general/sorting.d.ts.map +1 -0
  93. package/dist/cjs/general/sorting.js.map +1 -0
  94. package/dist/cjs/general/string.cjs +73 -0
  95. package/dist/cjs/general/string.d.ts +31 -0
  96. package/dist/cjs/general/string.d.ts.map +1 -0
  97. package/dist/cjs/general/string.js.map +1 -0
  98. package/dist/cjs/general/timeout.cjs +17 -0
  99. package/dist/cjs/general/timeout.d.ts +8 -0
  100. package/dist/cjs/general/timeout.d.ts.map +1 -0
  101. package/dist/cjs/general/timeout.js.map +1 -0
  102. package/dist/cjs/index.cjs +20 -0
  103. package/dist/cjs/index.d.ts +4 -0
  104. package/dist/cjs/index.d.ts.map +1 -0
  105. package/dist/cjs/index.js.map +1 -0
  106. package/dist/cjs/node/index.cjs +30 -0
  107. package/dist/cjs/node/index.d.ts +25 -0
  108. package/dist/cjs/node/index.d.ts.map +1 -0
  109. package/dist/cjs/node/index.js.map +1 -0
  110. package/dist/esm/browser/dom.d.ts +44 -0
  111. package/dist/esm/browser/dom.d.ts.map +1 -0
  112. package/dist/esm/browser/dom.js +88 -0
  113. package/dist/esm/browser/dom.js.map +1 -0
  114. package/dist/esm/browser/download.d.ts +16 -0
  115. package/dist/esm/browser/download.d.ts.map +1 -0
  116. package/dist/esm/browser/download.js +36 -0
  117. package/dist/esm/browser/download.js.map +1 -0
  118. package/dist/esm/browser/index.d.ts +4 -0
  119. package/dist/esm/browser/index.d.ts.map +1 -0
  120. package/dist/esm/browser/index.js +4 -0
  121. package/dist/esm/browser/index.js.map +1 -0
  122. package/dist/esm/browser/queryOrThrow.d.ts +44 -0
  123. package/dist/esm/browser/queryOrThrow.d.ts.map +1 -0
  124. package/dist/esm/browser/queryOrThrow.js +39 -0
  125. package/dist/esm/browser/queryOrThrow.js.map +1 -0
  126. package/dist/esm/general/arrays.d.ts +70 -0
  127. package/dist/esm/general/arrays.d.ts.map +1 -0
  128. package/dist/esm/general/arrays.js +144 -0
  129. package/dist/esm/general/arrays.js.map +1 -0
  130. package/dist/esm/general/boolean.d.ts +10 -0
  131. package/dist/esm/general/boolean.d.ts.map +1 -0
  132. package/dist/esm/general/boolean.js +16 -0
  133. package/dist/esm/general/boolean.js.map +1 -0
  134. package/dist/esm/general/clock.d.ts +12 -0
  135. package/dist/esm/general/clock.d.ts.map +1 -0
  136. package/dist/esm/general/clock.js +19 -0
  137. package/dist/esm/general/clock.js.map +1 -0
  138. package/dist/esm/general/dates/create.d.ts +54 -0
  139. package/dist/esm/general/dates/create.d.ts.map +1 -0
  140. package/dist/esm/general/dates/create.js +73 -0
  141. package/dist/esm/general/dates/create.js.map +1 -0
  142. package/dist/esm/general/dates/dates.d.ts +56 -0
  143. package/dist/esm/general/dates/dates.d.ts.map +1 -0
  144. package/dist/esm/general/dates/dates.js +82 -0
  145. package/dist/esm/general/dates/dates.js.map +1 -0
  146. package/dist/esm/general/dates/get.d.ts +41 -0
  147. package/dist/esm/general/dates/get.d.ts.map +1 -0
  148. package/dist/esm/general/dates/get.js +70 -0
  149. package/dist/esm/general/dates/get.js.map +1 -0
  150. package/dist/esm/general/dates/index.d.ts +4 -0
  151. package/dist/esm/general/dates/index.d.ts.map +1 -0
  152. package/dist/esm/general/dates/index.js +4 -0
  153. package/dist/esm/general/dates/index.js.map +1 -0
  154. package/dist/esm/general/forms.d.ts +17 -0
  155. package/dist/esm/general/forms.d.ts.map +1 -0
  156. package/dist/esm/general/forms.js +27 -0
  157. package/dist/esm/general/forms.js.map +1 -0
  158. package/dist/esm/general/images.d.ts +17 -0
  159. package/dist/esm/general/images.d.ts.map +1 -0
  160. package/dist/esm/general/images.js +20 -0
  161. package/dist/esm/general/images.js.map +1 -0
  162. package/dist/esm/general/index.d.ts +15 -0
  163. package/dist/esm/general/index.d.ts.map +1 -0
  164. package/dist/esm/general/index.js +15 -0
  165. package/dist/esm/general/index.js.map +1 -0
  166. package/dist/esm/general/localization.d.ts +83 -0
  167. package/dist/esm/general/localization.d.ts.map +1 -0
  168. package/dist/esm/general/localization.js +188 -0
  169. package/dist/esm/general/localization.js.map +1 -0
  170. package/dist/esm/general/maps.d.ts +35 -0
  171. package/dist/esm/general/maps.d.ts.map +1 -0
  172. package/dist/esm/general/maps.js +59 -0
  173. package/dist/esm/general/maps.js.map +1 -0
  174. package/dist/esm/general/messenger/createMessenger.d.ts +10 -0
  175. package/dist/esm/general/messenger/createMessenger.d.ts.map +1 -0
  176. package/dist/esm/general/messenger/createMessenger.js +65 -0
  177. package/dist/esm/general/messenger/createMessenger.js.map +1 -0
  178. package/dist/esm/general/messenger/index.d.ts +3 -0
  179. package/dist/esm/general/messenger/index.d.ts.map +1 -0
  180. package/dist/esm/general/messenger/index.js +3 -0
  181. package/dist/esm/general/messenger/index.js.map +1 -0
  182. package/dist/esm/general/messenger/types.d.ts +26 -0
  183. package/dist/esm/general/messenger/types.d.ts.map +1 -0
  184. package/dist/esm/general/messenger/types.js +2 -0
  185. package/dist/esm/general/messenger/types.js.map +1 -0
  186. package/dist/esm/general/namedEntities.d.ts +256 -0
  187. package/dist/esm/general/namedEntities.d.ts.map +1 -0
  188. package/dist/esm/general/namedEntities.js +257 -0
  189. package/dist/esm/general/namedEntities.js.map +1 -0
  190. package/dist/esm/general/numbers.d.ts +43 -0
  191. package/dist/esm/general/numbers.d.ts.map +1 -0
  192. package/dist/esm/general/numbers.js +87 -0
  193. package/dist/esm/general/numbers.js.map +1 -0
  194. package/dist/esm/general/objects.d.ts +45 -0
  195. package/dist/esm/general/objects.d.ts.map +1 -0
  196. package/dist/esm/general/objects.js +91 -0
  197. package/dist/esm/general/objects.js.map +1 -0
  198. package/dist/esm/general/sorting.d.ts +32 -0
  199. package/dist/esm/general/sorting.d.ts.map +1 -0
  200. package/dist/esm/general/sorting.js +67 -0
  201. package/dist/esm/general/sorting.js.map +1 -0
  202. package/dist/esm/general/string.d.ts +31 -0
  203. package/dist/esm/general/string.d.ts.map +1 -0
  204. package/dist/esm/general/string.js +68 -0
  205. package/dist/esm/general/string.js.map +1 -0
  206. package/dist/esm/general/timeout.d.ts +8 -0
  207. package/dist/esm/general/timeout.d.ts.map +1 -0
  208. package/dist/esm/general/timeout.js +14 -0
  209. package/dist/esm/general/timeout.js.map +1 -0
  210. package/dist/esm/index.d.ts +4 -0
  211. package/dist/esm/index.d.ts.map +1 -0
  212. package/dist/esm/index.js +4 -0
  213. package/dist/esm/index.js.map +1 -0
  214. package/dist/esm/node/index.d.ts +25 -0
  215. package/dist/esm/node/index.d.ts.map +1 -0
  216. package/dist/esm/node/index.js +27 -0
  217. package/dist/esm/node/index.js.map +1 -0
  218. package/package.json +62 -0
package/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # @rootreeweb/js-lib
2
+
3
+ ## What is This?
4
+
5
+ The Rootree JS Library is a library of commonly used functions
6
+ in JavaScript and TypeScript applications.
7
+ It provides functions for working with dates, the DOM, and more.
8
+
9
+ ## Installation
10
+
11
+ 1. Make sure that your device is set up to use the `@rootreeweb` npm registry.
12
+ More details on how to do this can be found on the [Rootree Web Wiki](https://docs.cappy.rootree.ca/guides/packages).
13
+ 2. Install the package
14
+
15
+ ```bash
16
+ yarn add @rootreeweb/rt-js-lib
17
+ ```
18
+
19
+ 3. Import the required functions into your script.
20
+ For example, if you wanted to use the ready function
21
+ to display a message when the page is fully loaded, you would write:
22
+
23
+ ```typescript
24
+ import { ready } from "@rootreeweb/rt-js-lib";
25
+
26
+ ready(() => console.log("I'm ready!"));
27
+ ```
28
+
29
+ ## Functions
30
+
31
+ Here are some functions that are available in the library:
32
+
33
+ - removeChildren(element: Element)
34
+ - setPropertyOnSelector<T extends HTMLElement>(
35
+ selector: string,
36
+ property: keyof T,
37
+ value: T[keyof T]
38
+ )
39
+ - ready(callback: () => unknown)
40
+ - waitFor(
41
+ containerSelector: string,
42
+ waitingForSelector: string,
43
+ callback: () => void,
44
+ runOnce = false
45
+ )
46
+ - download(filename: string, text: string)
47
+ - downloadBinary(filename: string, binary: ArrayBuffer)
48
+ - getDayNames(format: "long" | "short" | "narrow")
49
+ - and many more!
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addListenerToSelector = addListenerToSelector;
4
+ exports.ready = ready;
5
+ exports.removeChildren = removeChildren;
6
+ exports.setPropertyOnSelector = setPropertyOnSelector;
7
+ exports.waitFor = waitFor;
8
+ /**
9
+ * Add an event to all elements that match the provided selector.
10
+ *
11
+ * @template T The type of the element to add the event to.
12
+ * @param selector The CSS selector to match.
13
+ * @param eventName The event to listen for. Ex. click
14
+ * @param callback The function to call on the event being triggered.
15
+ */
16
+ function addListenerToSelector(selector, eventName, callback) {
17
+ const elements = document.querySelectorAll(selector);
18
+ elements.forEach((element) => {
19
+ element.addEventListener(eventName, (e) => callback(e, element));
20
+ });
21
+ }
22
+ /**
23
+ * Executes a function once the DOM is ready.
24
+ *
25
+ * @param callback The function to execute.
26
+ */
27
+ function ready(callback) {
28
+ if (document.readyState !== "loading") {
29
+ callback();
30
+ }
31
+ else {
32
+ document.addEventListener("DOMContentLoaded", () => {
33
+ callback();
34
+ });
35
+ }
36
+ }
37
+ /**
38
+ * Removes all the children from the provided element.
39
+ *
40
+ * @param element The element to remove the children of.
41
+ */
42
+ function removeChildren(element) {
43
+ while (element.firstChild) {
44
+ element.removeChild(element.firstChild);
45
+ }
46
+ }
47
+ /**
48
+ * Sets a property on all elements that match the provided selector. Note: these
49
+ * are not CSS properties or necessarily HTML attributes.
50
+ *
51
+ * @template TElement The type of the element to set the property on.
52
+ * @template TProperty The property to set.
53
+ * @param selector The CSS selector to match.
54
+ * @param property The property to set.
55
+ * @param value The value to set the property to.
56
+ */
57
+ function setPropertyOnSelector(selector, property, value) {
58
+ const elements = document.querySelectorAll(selector);
59
+ elements.forEach((element) => {
60
+ element.setAttribute(property, value);
61
+ });
62
+ }
63
+ /**
64
+ * Waits for an element to exist before executing a callback.
65
+ *
66
+ * @param containerSelector The selector for the container of the element to
67
+ * watch for.
68
+ * @param waitingForSelector The selector for the element to watch for.
69
+ * @param callback The function to execute once the element exists.
70
+ * @param runOnce If true, the callback will only be executed once.
71
+ * @returns False if the container element does not exist. Undefined otherwise.
72
+ */
73
+ function waitFor(containerSelector, waitingForSelector, callback, runOnce = false) {
74
+ const observerOptions = {
75
+ childList: true,
76
+ subtree: true,
77
+ };
78
+ const elementToWatch = document.querySelector(containerSelector);
79
+ if (!elementToWatch) {
80
+ return false;
81
+ }
82
+ if (elementToWatch.querySelector(waitingForSelector)) {
83
+ callback();
84
+ }
85
+ const observer = new MutationObserver(() => {
86
+ if (!elementToWatch.querySelector(waitingForSelector))
87
+ return;
88
+ if (runOnce) {
89
+ observer.disconnect();
90
+ }
91
+ callback();
92
+ });
93
+ observer.observe(elementToWatch, observerOptions);
94
+ }
95
+ //# sourceMappingURL=dom.js.map
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Add an event to all elements that match the provided selector.
3
+ *
4
+ * @template T The type of the element to add the event to.
5
+ * @param selector The CSS selector to match.
6
+ * @param eventName The event to listen for. Ex. click
7
+ * @param callback The function to call on the event being triggered.
8
+ */
9
+ export declare function addListenerToSelector<T extends HTMLElement>(selector: string, eventName: string, callback: (e: Event, element: T) => unknown): void;
10
+ /**
11
+ * Executes a function once the DOM is ready.
12
+ *
13
+ * @param callback The function to execute.
14
+ */
15
+ export declare function ready(callback: () => unknown): void;
16
+ /**
17
+ * Removes all the children from the provided element.
18
+ *
19
+ * @param element The element to remove the children of.
20
+ */
21
+ export declare function removeChildren(element: Element): void;
22
+ /**
23
+ * Sets a property on all elements that match the provided selector. Note: these
24
+ * are not CSS properties or necessarily HTML attributes.
25
+ *
26
+ * @template TElement The type of the element to set the property on.
27
+ * @template TProperty The property to set.
28
+ * @param selector The CSS selector to match.
29
+ * @param property The property to set.
30
+ * @param value The value to set the property to.
31
+ */
32
+ export declare function setPropertyOnSelector<TElement extends HTMLElement, TProperty extends keyof TElement & string>(selector: string, property: TProperty, value: string & TElement[TProperty]): void;
33
+ /**
34
+ * Waits for an element to exist before executing a callback.
35
+ *
36
+ * @param containerSelector The selector for the container of the element to
37
+ * watch for.
38
+ * @param waitingForSelector The selector for the element to watch for.
39
+ * @param callback The function to execute once the element exists.
40
+ * @param runOnce If true, the callback will only be executed once.
41
+ * @returns False if the container element does not exist. Undefined otherwise.
42
+ */
43
+ export declare function waitFor(containerSelector: string, waitingForSelector: string, callback: () => unknown, runOnce?: boolean): false | undefined;
44
+ //# sourceMappingURL=dom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dom.d.ts","sourceRoot":"","sources":["../../../src/browser/dom.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,SAAS,WAAW,EAC1D,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,EACjB,QAAQ,E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,CAAC,kBAAkB,CAAC;YAAE,OAAO;QAC9D,IAAI,OAAO,EAAE,CAAC;YACb,QAAQ,CAAC,UAAU,EAAE,CAAC;QACvB,CAAC;QACD,QAAQ,EAAE,CAAC;IACZ,CAAC,CAAC,CAAC;IACH,QAAQ,CAAC,OAAO,CAAC,cAAc,EAAE,eAAe,CAAC,CAAC;AACnD,CAAC","sourcesContent":["/**\n * Add an event to all elements that match the provided selector.\n *\n * @template T The type of the element to add the event to.\n * @param selector The CSS selector to match.\n * @param eventName The event to listen for. Ex. click\n * @param callback The function to call on the event being triggered.\n */\nexport function addListenerToSelector<T extends HTMLElement>(\n\tselector: string,\n\teventName: string,\n\tcallback: (e: Event, element: T) => unknown,\n): void {\n\tconst elements = document.querySelectorAll<T>(selector);\n\telements.forEach((element) => {\n\t\telement.addEventListener(eventName, (e) => callback(e, element));\n\t});\n}\n\n/**\n * Executes a function once the DOM is ready.\n *\n * @param callback The function to execute.\n */\nexport function ready(callback: () => unknown): void {\n\tif (document.readyState !== \"loading\") {\n\t\tcallback();\n\t} else {\n\t\tdocument.addEventListener(\"DOMContentLoaded\", () => {\n\t\t\tcallback();\n\t\t});\n\t}\n}\n\n/**\n * Removes all the children from the provided element.\n *\n * @param element The element to remove the children of.\n */\nexport function removeChildren(element: Element): void {\n\twhile (element.firstChild) {\n\t\telement.removeChild(element.firstChild);\n\t}\n}\n/**\n * Sets a property on all elements that match the provided selector. Note: these\n * are not CSS properties or necessarily HTML attributes.\n *\n * @template TElement The type of the element to set the property on.\n * @template TProperty The property to set.\n * @param selector The CSS selector to match.\n * @param property The property to set.\n * @param value The value to set the property to.\n */\nexport function setPropertyOnSelector<\n\tTElement extends HTMLElement,\n\tTProperty extends keyof TElement & string,\n>(\n\tselector: string,\n\tproperty: TProperty,\n\tvalue: string & TElement[TProperty],\n): void {\n\tconst elements = document.querySelectorAll<TElement>(selector);\n\telements.forEach((element) => {\n\t\telement.setAttribute(property, value);\n\t});\n}\n/**\n * Waits for an element to exist before executing a callback.\n *\n * @param containerSelector The selector for the container of the element to\n * watch for.\n * @param waitingForSelector The selector for the element to watch for.\n * @param callback The function to execute once the element exists.\n * @param runOnce If true, the callback will only be executed once.\n * @returns False if the container element does not exist. Undefined otherwise.\n */\nexport function waitFor(\n\tcontainerSelector: string,\n\twaitingForSelector: string,\n\tcallback: () => unknown,\n\trunOnce = false,\n): false | undefined {\n\tconst observerOptions = {\n\t\tchildList: true,\n\t\tsubtree: true,\n\t};\n\tconst elementToWatch = document.querySelector(containerSelector);\n\tif (!elementToWatch) {\n\t\treturn false;\n\t}\n\n\tif (elementToWatch.querySelector(waitingForSelector)) {\n\t\tcallback();\n\t}\n\tconst observer = new MutationObserver(() => {\n\t\tif (!elementToWatch.querySelector(waitingForSelector)) return;\n\t\tif (runOnce) {\n\t\t\tobserver.disconnect();\n\t\t}\n\t\tcallback();\n\t});\n\tobserver.observe(elementToWatch, observerOptions);\n}\n"]}
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.download = download;
4
+ exports.downloadBinary = downloadBinary;
5
+ /**
6
+ * Downloads a text file with the specified file name and content.
7
+ *
8
+ * @param fileName - The name of the file to be downloaded.
9
+ * @param text - The content of the file.
10
+ * @param raw Bypasses encoding if true.
11
+ */
12
+ function download(fileName, text, raw = false) {
13
+ const element = document.createElement("a");
14
+ element.setAttribute("href", raw ? text : "data:text/plain;charset=utf-8," + encodeURIComponent(text));
15
+ element.setAttribute("download", fileName);
16
+ element.style.display = "none";
17
+ document.body.appendChild(element);
18
+ element.click();
19
+ document.body.removeChild(element);
20
+ }
21
+ /**
22
+ * Downloads a binary file with the specified file name and content.
23
+ *
24
+ * @param fileName - The name of the file to be downloaded.
25
+ * @param binary - The content of the file.
26
+ */
27
+ function downloadBinary(fileName, binary) {
28
+ const blob = new Blob([binary], {
29
+ type: "application/octet-stream",
30
+ });
31
+ const blobUrl = URL.createObjectURL(blob);
32
+ const link = document.createElement("a");
33
+ link.href = blobUrl;
34
+ link.download = fileName;
35
+ document.body.appendChild(link);
36
+ link.click();
37
+ document.body.removeChild(link);
38
+ URL.revokeObjectURL(blobUrl);
39
+ }
40
+ //# sourceMappingURL=download.js.map
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Downloads a text file with the specified file name and content.
3
+ *
4
+ * @param fileName - The name of the file to be downloaded.
5
+ * @param text - The content of the file.
6
+ * @param raw Bypasses encoding if true.
7
+ */
8
+ export declare function download(fileName: string, text: string, raw?: boolean): void;
9
+ /**
10
+ * Downloads a binary file with the specified file name and content.
11
+ *
12
+ * @param fileName - The name of the file to be downloaded.
13
+ * @param binary - The content of the file.
14
+ */
15
+ export declare function downloadBinary(fileName: string, binary: ArrayBuffer): void;
16
+ //# sourceMappingURL=download.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"download.d.ts","sourceRoot":"","sources":["../../../src/browser/download.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,UAAQ,GAAG,IAAI,CAc1E;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CAe1E"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"download.js","sourceRoot":"","sources":["../../../src/browser/download.ts"],"names":[],"mappings":";;AAOA,4BAcC;AAQD,wCAeC;AA5CD;;;;;;GAMG;AACH,SAAgB,QAAQ,CAAC,QAAgB,EAAE,IAAY,EAAE,GAAG,GAAG,KAAK;IACnE,MAAM,OAAO,GAAG,QAAQ,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;IAC5C,OAAO,CAAC,YAAY,CACnB,MAAM,EACN,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,gCAAgC,GAAG,kBAAkB,CAAC,IAAI,CAAC,CACxE,CAAC;IACF,OAAO,CAAC,YAAY,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC;IAE3C,OAAO,CAAC,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC;IAC/B,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IAEnC,OAAO,CAAC,KAAK,EAAE,CAAC;IAEhB,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;AACpC,CAAC;AAED;;;;;GAKG;AACH,SAAgB,cAAc,CAAC,QAAgB,EAAE,MAAmB;IACnE,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,CAAC,MAAM,CAAC,EAAE;QAC/B,IAAI,EAAE,0BAA0B;KAChC,CAAC,CAAC;IACH,MAAM,OAAO,GAAG,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;IAE1C,MAAM,IAAI,GAAG,QAAQ,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;IACzC,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC;IACpB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAEzB,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IAChC,IAAI,CAAC,KAAK,EAAE,CAAC;IAEb,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IAChC,GAAG,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;AAC9B,CAAC","sourcesContent":["/**\n * Downloads a text file with the specified file name and content.\n *\n * @param fileName - The name of the file to be downloaded.\n * @param text - The content of the file.\n * @param raw Bypasses encoding if true.\n */\nexport function download(fileName: string, text: string, raw = false): void {\n\tconst element = document.createElement(\"a\");\n\telement.setAttribute(\n\t\t\"href\",\n\t\traw ? text : \"data:text/plain;charset=utf-8,\" + encodeURIComponent(text),\n\t);\n\telement.setAttribute(\"download\", fileName);\n\n\telement.style.display = \"none\";\n\tdocument.body.appendChild(element);\n\n\telement.click();\n\n\tdocument.body.removeChild(element);\n}\n\n/**\n * Downloads a binary file with the specified file name and content.\n *\n * @param fileName - The name of the file to be downloaded.\n * @param binary - The content of the file.\n */\nexport function downloadBinary(fileName: string, binary: ArrayBuffer): void {\n\tconst blob = new Blob([binary], {\n\t\ttype: \"application/octet-stream\",\n\t});\n\tconst blobUrl = URL.createObjectURL(blob);\n\n\tconst link = document.createElement(\"a\");\n\tlink.href = blobUrl;\n\tlink.download = fileName;\n\n\tdocument.body.appendChild(link);\n\tlink.click();\n\n\tdocument.body.removeChild(link);\n\tURL.revokeObjectURL(blobUrl);\n}\n"]}
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./dom.cjs"), exports);
18
+ __exportStar(require("./download.cjs"), exports);
19
+ __exportStar(require("./queryOrThrow.cjs"), exports);
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,4 @@
1
+ export * from "./dom.js";
2
+ export * from "./download.js";
3
+ export * from "./queryOrThrow.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/browser/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,eAAe,CAAC;AAC9B,cAAc,mBAAmB,CAAC"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/browser/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,gDAA8B;AAC9B,oDAAkC","sourcesContent":["export * from \"./dom.js\";\nexport * from \"./download.js\";\nexport * from \"./queryOrThrow.js\";\n"]}
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.queryOrThrow = queryOrThrow;
4
+ /**
5
+ * Implementation of queryOrThrow. Queries an element and optionally validates
6
+ * its type.
7
+ *
8
+ * @template T The type of the element to query.
9
+ * @param arg1 The parent element or the selector string.
10
+ * @param arg2 The selector string or the element type constructor.
11
+ * @param arg3 The element type constructor.
12
+ * @returns The element that was found.
13
+ * @throws {Error} If the element is not found or fails the type validation.
14
+ */
15
+ function queryOrThrow(arg1, arg2, arg3) {
16
+ let parent;
17
+ let selector;
18
+ let type;
19
+ if (typeof arg1 === "string") {
20
+ parent = document;
21
+ selector = arg1;
22
+ if (typeof arg2 === "function") {
23
+ type = arg2;
24
+ }
25
+ }
26
+ else {
27
+ parent = arg1;
28
+ selector = arg2; // Overloads guarantee arg2 is a string here.
29
+ if (typeof arg3 === "function") {
30
+ type = arg3;
31
+ }
32
+ }
33
+ const el = parent.querySelector(selector);
34
+ if (!el) {
35
+ throw new Error(`Element matching selector "${selector}" not found.`);
36
+ }
37
+ if (type && !(el instanceof type)) {
38
+ throw new Error(`Element matching selector "${selector}" is not an instance of "${type.name}". Found "${el.constructor.name}" instead.`);
39
+ }
40
+ return el;
41
+ }
42
+ //# sourceMappingURL=queryOrThrow.js.map
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Overload for querying with a selector and validating the element type.
3
+ *
4
+ * @template T The specific Element type to return.
5
+ * @param selector The selector to query.
6
+ * @param type The constructor of the element type to validate against (e.g.,
7
+ * HTMLInputElement).
8
+ * @returns The element that was found and validated.
9
+ * @throws {Error} If the element is not found or is not of the specified type.
10
+ */
11
+ export declare function queryOrThrow<T extends Element>(selector: string, type: new () => T): T;
12
+ /**
13
+ * Overload for querying within a parent element and validating the element
14
+ * type.
15
+ *
16
+ * @template T The specific Element type to return.
17
+ * @param parent The parent element to query within.
18
+ * @param selector The selector to query.
19
+ * @param type The constructor of the element type to validate against (e.g.,
20
+ * HTMLInputElement).
21
+ * @returns The element that was found and validated.
22
+ * @throws {Error} If the element is not found or is not of the specified type.
23
+ */
24
+ export declare function queryOrThrow<T extends Element>(parent: Element, selector: string, type: new () => T): T;
25
+ /**
26
+ * Overload for querying with just a selector.
27
+ *
28
+ * @template T The generic Element type to return.
29
+ * @param selector The selector to query.
30
+ * @returns The element that was found.
31
+ * @throws {Error} If the element is not found.
32
+ */
33
+ export declare function queryOrThrow<T extends Element>(selector: string): T;
34
+ /**
35
+ * Overload for querying within a parent element with just a selector.
36
+ *
37
+ * @template T The generic Element type to return.
38
+ * @param parent The parent element to query within.
39
+ * @param selector The selector to query.
40
+ * @returns The element that was found.
41
+ * @throws {Error} If the element is not found.
42
+ */
43
+ export declare function queryOrThrow<T extends Element>(parent: Element, selector: string): T;
44
+ //# sourceMappingURL=queryOrThrow.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queryOrThrow.d.ts","sourceRoot":"","sources":["../../../src/browser/queryOrThrow.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,OAAO,EAC7C,QAAQ,EAAE,MAAM,EAEhB,IAAI,EAAE,UAAU,CAAC,GACf,CAAC,CAAC;AACL;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,OAAO,EAC7C,MAAM,EAAE,OAAO,EACf,QAAQ,EAAE,MAAM,EAEhB,IAAI,EAAE,UAAU,CAAC,GACf,CAAC,CAAC;AACL;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,OAAO,EAAE,QAAQ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AO,EAAE,CAAC;AACX,CAAC","sourcesContent":["/**\n * Overload for querying with a selector and validating the element type.\n *\n * @template T The specific Element type to return.\n * @param selector The selector to query.\n * @param type The constructor of the element type to validate against (e.g.,\n * HTMLInputElement).\n * @returns The element that was found and validated.\n * @throws {Error} If the element is not found or is not of the specified type.\n */\nexport function queryOrThrow<T extends Element>(\n\tselector: string,\n\t// eslint-disable-next-line @typescript-eslint/unified-signatures\n\ttype: new () => T,\n): T;\n/**\n * Overload for querying within a parent element and validating the element\n * type.\n *\n * @template T The specific Element type to return.\n * @param parent The parent element to query within.\n * @param selector The selector to query.\n * @param type The constructor of the element type to validate against (e.g.,\n * HTMLInputElement).\n * @returns The element that was found and validated.\n * @throws {Error} If the element is not found or is not of the specified type.\n */\nexport function queryOrThrow<T extends Element>(\n\tparent: Element,\n\tselector: string,\n\t// eslint-disable-next-line @typescript-eslint/unified-signatures\n\ttype: new () => T,\n): T;\n/**\n * Overload for querying with just a selector.\n *\n * @template T The generic Element type to return.\n * @param selector The selector to query.\n * @returns The element that was found.\n * @throws {Error} If the element is not found.\n */\nexport function queryOrThrow<T extends Element>(selector: string): T;\n/**\n * Overload for querying within a parent element with just a selector.\n *\n * @template T The generic Element type to return.\n * @param parent The parent element to query within.\n * @param selector The selector to query.\n * @returns The element that was found.\n * @throws {Error} If the element is not found.\n */\nexport function queryOrThrow<T extends Element>(\n\tparent: Element,\n\tselector: string,\n): T;\n/**\n * Implementation of queryOrThrow. Queries an element and optionally validates\n * its type.\n *\n * @template T The type of the element to query.\n * @param arg1 The parent element or the selector string.\n * @param arg2 The selector string or the element type constructor.\n * @param arg3 The element type constructor.\n * @returns The element that was found.\n * @throws {Error} If the element is not found or fails the type validation.\n */\nexport function queryOrThrow<T extends Element>(\n\targ1: Element | string,\n\targ2?: (new () => T) | string,\n\targ3?: new () => T,\n): T {\n\tlet parent: Document | Element;\n\tlet selector: string;\n\tlet type: (new () => T) | undefined;\n\n\tif (typeof arg1 === \"string\") {\n\t\tparent = document;\n\t\tselector = arg1;\n\t\tif (typeof arg2 === \"function\") {\n\t\t\ttype = arg2;\n\t\t}\n\t} else {\n\t\tparent = arg1;\n\t\tselector = arg2 as string; // Overloads guarantee arg2 is a string here.\n\t\tif (typeof arg3 === \"function\") {\n\t\t\ttype = arg3;\n\t\t}\n\t}\n\n\tconst el = parent.querySelector<T>(selector);\n\n\tif (!el) {\n\t\tthrow new Error(`Element matching selector \"${selector}\" not found.`);\n\t}\n\n\tif (type && !(el instanceof type)) {\n\t\tthrow new Error(\n\t\t\t`Element matching selector \"${selector}\" is not an instance of \"${type.name}\". Found \"${el.constructor.name}\" instead.`,\n\t\t);\n\t}\n\n\treturn el;\n}\n"]}
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Direction = void 0;
4
+ exports.dedupe = dedupe;
5
+ exports.enforceUniquePositions = enforceUniquePositions;
6
+ exports.getLoopedIndex = getLoopedIndex;
7
+ exports.moveItemInArray = moveItemInArray;
8
+ var Direction;
9
+ (function (Direction) {
10
+ Direction[Direction["Left"] = 0] = "Left";
11
+ Direction[Direction["Right"] = 1] = "Right";
12
+ })(Direction || (exports.Direction = Direction = {}));
13
+ /**
14
+ * Remove duplicates from an array
15
+ *
16
+ * @template TData The type of the array
17
+ * @param data The array to remove duplicates from
18
+ * @returns The array with duplicates removed
19
+ */
20
+ function dedupe(data) {
21
+ return [...new Set(data)];
22
+ }
23
+ /**
24
+ * Mutates the position field in an array of objects to enforce uniqueness.
25
+ *
26
+ * @template TData The type of the objects in the array.
27
+ * @param array The array of objects.
28
+ * @throws {Error} If an undefined item is found in the array.
29
+ */
30
+ function enforceUniquePositions(array) {
31
+ const newArray = [...array];
32
+ newArray.sort((a, b) => a.position - b.position);
33
+ for (let i = 0; i < newArray.length; i++) {
34
+ const item = getItemAtIndex(newArray, i);
35
+ item.position = i;
36
+ }
37
+ }
38
+ /**
39
+ * Get the next index or previous index in array, looping around if necessary.
40
+ *
41
+ * @param currentIndex The current index.
42
+ * @param direction The direction to move in.
43
+ * @param items The array of items.
44
+ * @returns The next index.
45
+ */
46
+ function getLoopedIndex(currentIndex, direction, items) {
47
+ const length = Array.isArray(items) ? items.length : items;
48
+ if (direction === Direction.Left) {
49
+ return currentIndex - 1 < 0 ? length - 1 : currentIndex - 1;
50
+ }
51
+ else {
52
+ return currentIndex + 2 > length ? 0 : currentIndex + 1;
53
+ }
54
+ }
55
+ /**
56
+ * Reassigns the position field in an array of objects.
57
+ *
58
+ * @example
59
+ * const array = [
60
+ * { id: 1, position: 0 },
61
+ * { id: 2, position: 1 },
62
+ * { id: 3, position: 2 },
63
+ * ];
64
+ * const newArray = moveItemInArray(array, "id", 2, 1);
65
+ * // newArray is now [{ id: 1, position: 0 }, { id: 3, position: 1 }, { id: 2, position: 2 }];
66
+ *
67
+ * const notIntrinsicallyOrderedArray = [
68
+ * { id: 1, position: 0 },
69
+ * { id: 3, position: 2 },
70
+ * { id: 2, position: 1 },
71
+ * { id: 4, position: 3 },
72
+ * ];
73
+ * const newArray2 = moveItemInArray(
74
+ * notIntrinsicallyOrderedArray,
75
+ * "id",
76
+ * 2,
77
+ * -1,
78
+ * );
79
+ * // newArray2 is now [{ id: 1, position: 0 }, { id: 2, position: 1 }, { id: 3, position: 2 }, { id: 4, position: 3 }];
80
+ *
81
+ * @template T The type of the objects in the array. The objects must have a
82
+ * numeric `position` field.
83
+ * @template K The key field in an array item.
84
+ * @param array The array of objects.
85
+ * @param idField The field to use as the id.
86
+ * @param id The id of the item to move.
87
+ * @param direction The direction to move the item in. 1 for down, -1 for up.
88
+ * @returns An array with the new position values set.
89
+ */
90
+ function moveItemInArray(array, idField, id, direction) {
91
+ const newArray = [...array];
92
+ const index = newArray.findIndex((item) => item[idField] === id);
93
+ if (index === -1) {
94
+ return newArray;
95
+ }
96
+ const item = getItemAtIndex(newArray, index);
97
+ const originalPosition = item.position;
98
+ for (let i = 0; i < newArray.length; i++) {
99
+ handleItem(i, index, direction, newArray, originalPosition);
100
+ }
101
+ return newArray;
102
+ }
103
+ /**
104
+ * Gets an item at a specific index in an array, throwing an error if the item
105
+ * is undefined.
106
+ *
107
+ * @template TData The type of the items in the array.
108
+ * @param array The array to get the item from.
109
+ * @param index The index of the item to get.
110
+ * @returns The item at the specified index.
111
+ * @throws {Error} If the item at the specified index is undefined.
112
+ */
113
+ function getItemAtIndex(array, index) {
114
+ // eslint-disable-next-line security/detect-object-injection
115
+ const item = array[index];
116
+ if (item === undefined) {
117
+ throw new Error("Undefined item in array at index " + String(index));
118
+ }
119
+ return item;
120
+ }
121
+ /**
122
+ * Handles the movement of each item within an array.
123
+ *
124
+ * @template T The type of the array item
125
+ * @param currentIndex The index of the item who's movement is being handled.
126
+ * @param indexBeingMoved The index of the item that was moved.
127
+ * @param direction Whether the indexBeingMoved is moving ahead or back.
128
+ * @param newArray The array being built.
129
+ * @param originalPosition The original position of the item being moved.
130
+ */
131
+ function handleItem(currentIndex, indexBeingMoved, direction, newArray, originalPosition) {
132
+ const item = getItemAtIndex(newArray, currentIndex);
133
+ if (currentIndex === indexBeingMoved) {
134
+ if (direction === -1 || currentIndex < newArray.length - 1) {
135
+ item.position += direction;
136
+ }
137
+ }
138
+ else if (direction === 1) {
139
+ if (item.position > originalPosition &&
140
+ item.position <= originalPosition + 1) {
141
+ item.position--;
142
+ }
143
+ }
144
+ else {
145
+ if (item.position < originalPosition &&
146
+ item.position >= originalPosition - 1) {
147
+ item.position++;
148
+ }
149
+ }
150
+ }
151
+ //# sourceMappingURL=arrays.js.map
@@ -0,0 +1,70 @@
1
+ export declare enum Direction {
2
+ Left = 0,
3
+ Right = 1
4
+ }
5
+ /**
6
+ * Remove duplicates from an array
7
+ *
8
+ * @template TData The type of the array
9
+ * @param data The array to remove duplicates from
10
+ * @returns The array with duplicates removed
11
+ */
12
+ export declare function dedupe<TData>(data: TData[]): TData[];
13
+ /**
14
+ * Mutates the position field in an array of objects to enforce uniqueness.
15
+ *
16
+ * @template TData The type of the objects in the array.
17
+ * @param array The array of objects.
18
+ * @throws {Error} If an undefined item is found in the array.
19
+ */
20
+ export declare function enforceUniquePositions<TData extends {
21
+ position: number;
22
+ }>(array: TData[]): void;
23
+ /**
24
+ * Get the next index or previous index in array, looping around if necessary.
25
+ *
26
+ * @param currentIndex The current index.
27
+ * @param direction The direction to move in.
28
+ * @param items The array of items.
29
+ * @returns The next index.
30
+ */
31
+ export declare function getLoopedIndex(currentIndex: number, direction: Direction, items: number | unknown[]): number;
32
+ /**
33
+ * Reassigns the position field in an array of objects.
34
+ *
35
+ * @example
36
+ * const array = [
37
+ * { id: 1, position: 0 },
38
+ * { id: 2, position: 1 },
39
+ * { id: 3, position: 2 },
40
+ * ];
41
+ * const newArray = moveItemInArray(array, "id", 2, 1);
42
+ * // newArray is now [{ id: 1, position: 0 }, { id: 3, position: 1 }, { id: 2, position: 2 }];
43
+ *
44
+ * const notIntrinsicallyOrderedArray = [
45
+ * { id: 1, position: 0 },
46
+ * { id: 3, position: 2 },
47
+ * { id: 2, position: 1 },
48
+ * { id: 4, position: 3 },
49
+ * ];
50
+ * const newArray2 = moveItemInArray(
51
+ * notIntrinsicallyOrderedArray,
52
+ * "id",
53
+ * 2,
54
+ * -1,
55
+ * );
56
+ * // newArray2 is now [{ id: 1, position: 0 }, { id: 2, position: 1 }, { id: 3, position: 2 }, { id: 4, position: 3 }];
57
+ *
58
+ * @template T The type of the objects in the array. The objects must have a
59
+ * numeric `position` field.
60
+ * @template K The key field in an array item.
61
+ * @param array The array of objects.
62
+ * @param idField The field to use as the id.
63
+ * @param id The id of the item to move.
64
+ * @param direction The direction to move the item in. 1 for down, -1 for up.
65
+ * @returns An array with the new position values set.
66
+ */
67
+ export declare function moveItemInArray<T extends {
68
+ position: number;
69
+ }, K extends keyof T>(array: T[], idField: K, id: T[K], direction: -1 | 1): T[];
70
+ //# sourceMappingURL=arrays.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"arrays.d.ts","sourceRoot":"","sources":["../../../src/general/arrays.ts"],"names":[],"mappings":"AAAA,oBAAY,SAAS;IACpB,IAAI,IAAA;IACJ,KAAK,IAAA;CACL;AAED;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,KAAK,EAAE,CAEpD;AAED;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,SAAS;IAAE,QAAQ,EAAE,MAAM,CAAA;CAAE,EACxE,KAAK,EAAE,KAAK,EAAE,GACZ,IAAI,CAON;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAC7B,YAAY,EAAE,MAAM,EACpB,SAAS,EAAE,SAAS,EACpB,KAAK,EAAE,MAAM,GAAG,OAAO,EAAE,GACvB,MAAM,CAOR;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,eAAe,CAC9B,CAAC,SAAS;IAAE,QAAQ,EAAE,MAAM,CAAA;CAAE,EAC9B,CAAC,SAAS,MAAM,CAAC,EAChB,KAAK,EAAE,CAAC,EAAE,EAAE,OAAO,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAa1D"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"arrays.js","sourceRoot":"","sources":["../../../src/general/arrays.ts"],"names":[],"mappings":";;;AAYA,wBAEC;AASD,wDASC;AAUD,wCAWC;AAqCD,0CAgBC;AA1GD,IAAY,SAGX;AAHD,WAAY,SAAS;IACpB,yCAAI,CAAA;IACJ,2CAAK,CAAA;AACN,CAAC,EAHW,SAAS,yBAAT,SAAS,QAGpB;AAED;;;;;;GAMG;AACH,SAAgB,MAAM,CAAQ,IAAa;IAC1C,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;AAC3B,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,sBAA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,CAAC,QAAQ,GAAG,gBAAgB;YAChC,IAAI,CAAC,QAAQ,IAAI,gBAAgB,GAAG,CAAC,EACpC,CAAC;YACF,IAAI,CAAC,QAAQ,EAAE,CAAC;QACjB,CAAC;IACF,CAAC;SAAM,CAAC;QACP,IACC,IAAI,CAAC,QAAQ,GAAG,gBAAgB;YAChC,IAAI,CAAC,QAAQ,IAAI,gBAAgB,GAAG,CAAC,EACpC,CAAC;YACF,IAAI,CAAC,QAAQ,EAAE,CAAC;QACjB,CAAC;IACF,CAAC;AACF,CAAC","sourcesContent":["export enum Direction {\n\tLeft,\n\tRight,\n}\n\n/**\n * Remove duplicates from an array\n *\n * @template TData The type of the array\n * @param data The array to remove duplicates from\n * @returns The array with duplicates removed\n */\nexport function dedupe<TData>(data: TData[]): TData[] {\n\treturn [...new Set(data)];\n}\n\n/**\n * Mutates the position field in an array of objects to enforce uniqueness.\n *\n * @template TData The type of the objects in the array.\n * @param array The array of objects.\n * @throws {Error} If an undefined item is found in the array.\n */\nexport function enforceUniquePositions<TData extends { position: number }>(\n\tarray: TData[],\n): void {\n\tconst newArray = [...array];\n\tnewArray.sort((a, b) => a.position - b.position);\n\tfor (let i = 0; i < newArray.length; i++) {\n\t\tconst item = getItemAtIndex(newArray, i);\n\t\titem.position = i;\n\t}\n}\n\n/**\n * Get the next index or previous index in array, looping around if necessary.\n *\n * @param currentIndex The current index.\n * @param direction The direction to move in.\n * @param items The array of items.\n * @returns The next index.\n */\nexport function getLoopedIndex(\n\tcurrentIndex: number,\n\tdirection: Direction,\n\titems: number | unknown[],\n): number {\n\tconst length = Array.isArray(items) ? items.length : items;\n\tif (direction === Direction.Left) {\n\t\treturn currentIndex - 1 < 0 ? length - 1 : currentIndex - 1;\n\t} else {\n\t\treturn currentIndex + 2 > length ? 0 : currentIndex + 1;\n\t}\n}\n\n/**\n * Reassigns the position field in an array of objects.\n *\n * @example\n * \tconst array = [\n * \t\t{ id: 1, position: 0 },\n * \t\t{ id: 2, position: 1 },\n * \t\t{ id: 3, position: 2 },\n * \t];\n * \tconst newArray = moveItemInArray(array, \"id\", 2, 1);\n * \t// newArray is now [{ id: 1, position: 0 }, { id: 3, position: 1 }, { id: 2, position: 2 }];\n *\n * \tconst notIntrinsicallyOrderedArray = [\n * \t\t{ id: 1, position: 0 },\n * \t\t{ id: 3, position: 2 },\n * \t\t{ id: 2, position: 1 },\n * \t\t{ id: 4, position: 3 },\n * \t];\n * \tconst newArray2 = moveItemInArray(\n * \t\tnotIntrinsicallyOrderedArray,\n * \t\t\"id\",\n * \t\t2,\n * \t\t-1,\n * \t);\n * \t// newArray2 is now [{ id: 1, position: 0 }, { id: 2, position: 1 }, { id: 3, position: 2 }, { id: 4, position: 3 }];\n *\n * @template T The type of the objects in the array. The objects must have a\n * numeric `position` field.\n * @template K The key field in an array item.\n * @param array The array of objects.\n * @param idField The field to use as the id.\n * @param id The id of the item to move.\n * @param direction The direction to move the item in. 1 for down, -1 for up.\n * @returns An array with the new position values set.\n */\nexport function moveItemInArray<\n\tT extends { position: number },\n\tK extends keyof T,\n>(array: T[], idField: K, id: T[K], direction: -1 | 1): T[] {\n\tconst newArray = [...array];\n\tconst index = newArray.findIndex((item) => item[idField] === id);\n\tif (index === -1) {\n\t\treturn newArray;\n\t}\n\tconst item = getItemAtIndex(newArray, index);\n\tconst originalPosition = item.position;\n\tfor (let i = 0; i < newArray.length; i++) {\n\t\thandleItem<T>(i, index, direction, newArray, originalPosition);\n\t}\n\n\treturn newArray;\n}\n\n/**\n * Gets an item at a specific index in an array, throwing an error if the item\n * is undefined.\n *\n * @template TData The type of the items in the array.\n * @param array The array to get the item from.\n * @param index The index of the item to get.\n * @returns The item at the specified index.\n * @throws {Error} If the item at the specified index is undefined.\n */\nfunction getItemAtIndex<TData>(array: TData[], index: number) {\n\t// eslint-disable-next-line security/detect-object-injection\n\tconst item = array[index];\n\tif (item === undefined) {\n\t\tthrow new Error(\"Undefined item in array at index \" + String(index));\n\t}\n\treturn item;\n}\n\n/**\n * Handles the movement of each item within an array.\n *\n * @template T The type of the array item\n * @param currentIndex The index of the item who's movement is being handled.\n * @param indexBeingMoved The index of the item that was moved.\n * @param direction Whether the indexBeingMoved is moving ahead or back.\n * @param newArray The array being built.\n * @param originalPosition The original position of the item being moved.\n */\nfunction handleItem<T extends { position: number }>(\n\tcurrentIndex: number,\n\tindexBeingMoved: number,\n\tdirection: number,\n\tnewArray: T[],\n\toriginalPosition: number,\n) {\n\tconst item = getItemAtIndex(newArray, currentIndex);\n\tif (currentIndex === indexBeingMoved) {\n\t\tif (direction === -1 || currentIndex < newArray.length - 1) {\n\t\t\titem.position += direction;\n\t\t}\n\t} else if (direction === 1) {\n\t\tif (\n\t\t\titem.position > originalPosition &&\n\t\t\titem.position <= originalPosition + 1\n\t\t) {\n\t\t\titem.position--;\n\t\t}\n\t} else {\n\t\tif (\n\t\t\titem.position < originalPosition &&\n\t\t\titem.position >= originalPosition - 1\n\t\t) {\n\t\t\titem.position++;\n\t\t}\n\t}\n}\n"]}