@oscarpalmer/atoms 0.74.1 → 0.76.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.
Files changed (282) hide show
  1. package/dist/js/array/chunk.cjs +18 -0
  2. package/dist/js/array/{chunk.mjs → chunk.js} +2 -2
  3. package/dist/js/array/compact.cjs +6 -0
  4. package/dist/js/array/{compact.mjs → compact.js} +0 -1
  5. package/dist/js/array/count.cjs +14 -0
  6. package/dist/js/array/count.js +14 -0
  7. package/dist/js/array/exists.cjs +14 -0
  8. package/dist/js/array/exists.js +14 -0
  9. package/dist/js/array/filter.cjs +14 -0
  10. package/dist/js/array/filter.js +14 -0
  11. package/dist/js/array/find.cjs +14 -0
  12. package/dist/js/array/find.js +14 -0
  13. package/dist/js/array/group-by.cjs +35 -0
  14. package/dist/js/array/group-by.js +35 -0
  15. package/dist/js/array/index-of.cjs +14 -0
  16. package/dist/js/array/index-of.js +14 -0
  17. package/dist/js/array/index.cjs +40 -0
  18. package/dist/js/array/index.js +34 -381
  19. package/dist/js/array/insert.cjs +31 -0
  20. package/dist/js/array/insert.js +31 -0
  21. package/dist/js/array/models.cjs +1 -0
  22. package/dist/js/array/models.js +1 -0
  23. package/dist/js/array/shuffle.cjs +13 -0
  24. package/dist/js/array/{shuffle.mjs → shuffle.js} +2 -3
  25. package/dist/js/array/sort.cjs +53 -0
  26. package/dist/js/array/{sort.mjs → sort.js} +13 -9
  27. package/dist/js/array/splice.cjs +14 -0
  28. package/dist/js/array/splice.js +14 -0
  29. package/dist/js/array/to-map.cjs +27 -0
  30. package/dist/js/array/to-map.js +27 -0
  31. package/dist/js/array/to-record.cjs +12 -0
  32. package/dist/js/array/to-record.js +12 -0
  33. package/dist/js/array/unique.cjs +7 -0
  34. package/dist/js/array/unique.js +7 -0
  35. package/dist/js/colour/base.cjs +18 -0
  36. package/dist/js/colour/{base.mjs → base.js} +4 -3
  37. package/dist/js/colour/functions.cjs +96 -0
  38. package/dist/js/colour/{functions.mjs → functions.js} +18 -19
  39. package/dist/js/colour/hex.cjs +56 -0
  40. package/dist/js/colour/{hex.mjs → hex.js} +34 -15
  41. package/dist/js/colour/hsl.cjs +85 -0
  42. package/dist/js/colour/{hsl.mjs → hsl.js} +40 -12
  43. package/dist/js/colour/index.cjs +29 -0
  44. package/dist/js/colour/index.js +14 -272
  45. package/dist/js/colour/is.cjs +27 -0
  46. package/dist/js/colour/{is.mjs → is.js} +7 -6
  47. package/dist/js/colour/rgb.cjs +87 -0
  48. package/dist/js/colour/{rgb.mjs → rgb.js} +43 -12
  49. package/dist/js/{emitter.mjs → emitter.cjs} +88 -56
  50. package/dist/js/emitter.js +85 -56
  51. package/dist/js/{function.mjs → function.cjs} +66 -46
  52. package/dist/js/function.js +61 -48
  53. package/dist/js/index.cjs +136 -0
  54. package/dist/js/index.js +133 -1724
  55. package/dist/js/internal/array/callbacks.cjs +23 -0
  56. package/dist/js/internal/array/callbacks.js +23 -0
  57. package/dist/js/internal/array/find.cjs +51 -0
  58. package/dist/js/internal/array/find.js +51 -0
  59. package/dist/js/internal/value/handle.cjs +21 -0
  60. package/dist/js/internal/{value-handle.mjs → value/handle.js} +0 -1
  61. package/dist/js/{is.mjs → is.cjs} +17 -18
  62. package/dist/js/is.js +15 -27
  63. package/dist/js/logger.cjs +108 -0
  64. package/dist/js/logger.js +44 -8
  65. package/dist/js/{math.mjs → math.cjs} +7 -8
  66. package/dist/js/math.js +4 -5
  67. package/dist/js/models.cjs +1 -0
  68. package/dist/js/models.js +1 -0
  69. package/dist/js/{number.mjs → number.cjs} +8 -8
  70. package/dist/js/number.js +5 -5
  71. package/dist/js/{query.mjs → query.cjs} +18 -18
  72. package/dist/js/query.js +17 -86
  73. package/dist/js/{queue.mjs → queue.cjs} +12 -13
  74. package/dist/js/queue.js +9 -10
  75. package/dist/js/{random.mjs → random.cjs} +14 -21
  76. package/dist/js/random.js +9 -26
  77. package/dist/js/{sized.mjs → sized.cjs} +68 -33
  78. package/dist/js/sized.js +66 -38
  79. package/dist/js/string/case.cjs +41 -0
  80. package/dist/js/string/{case.mjs → case.js} +13 -9
  81. package/dist/js/string/index.cjs +59 -0
  82. package/dist/js/string/index.js +30 -100
  83. package/dist/js/string/template.cjs +21 -0
  84. package/dist/js/string/{template.mjs → template.js} +4 -5
  85. package/dist/js/{touch.mjs → touch.cjs} +4 -7
  86. package/dist/js/touch.js +3 -5
  87. package/dist/js/value/clone.cjs +76 -0
  88. package/dist/js/value/{clone.mjs → clone.js} +5 -6
  89. package/dist/js/value/compare.cjs +59 -0
  90. package/dist/js/value/{compare.mjs → compare.js} +4 -6
  91. package/dist/js/value/diff.cjs +75 -0
  92. package/dist/js/value/{diff.mjs → diff.js} +13 -11
  93. package/dist/js/value/equal.cjs +106 -0
  94. package/dist/js/value/{equal.mjs → equal.js} +13 -8
  95. package/dist/js/value/get.cjs +21 -0
  96. package/dist/js/value/{get.mjs → get.js} +9 -4
  97. package/dist/js/value/index.cjs +30 -0
  98. package/dist/js/value/index.js +19 -510
  99. package/dist/js/value/merge.cjs +35 -0
  100. package/dist/js/value/{merge.mjs → merge.js} +7 -6
  101. package/dist/js/value/set.cjs +25 -0
  102. package/dist/js/value/{set.mjs → set.js} +3 -4
  103. package/dist/js/value/smush.cjs +26 -0
  104. package/dist/js/value/{smush.mjs → smush.js} +3 -4
  105. package/dist/js/value/unsmush.cjs +39 -0
  106. package/dist/js/value/{unsmush.mjs → unsmush.js} +10 -7
  107. package/package.json +50 -47
  108. package/src/js/array/chunk.ts +6 -5
  109. package/src/js/array/compact.ts +8 -10
  110. package/src/js/array/count.ts +39 -19
  111. package/src/js/array/exists.ts +39 -15
  112. package/src/js/array/filter.ts +42 -20
  113. package/src/js/array/find.ts +45 -20
  114. package/src/js/array/group-by.ts +158 -27
  115. package/src/js/array/index-of.ts +40 -18
  116. package/src/js/array/index.ts +10 -9
  117. package/src/js/array/insert.ts +28 -14
  118. package/src/js/array/models.ts +18 -15
  119. package/src/js/array/shuffle.ts +3 -3
  120. package/src/js/array/sort.ts +32 -34
  121. package/src/js/array/splice.ts +22 -26
  122. package/src/js/array/to-map.ts +133 -45
  123. package/src/js/array/to-record.ts +137 -39
  124. package/src/js/array/unique.ts +20 -15
  125. package/src/js/colour/base.ts +3 -3
  126. package/src/js/colour/functions.ts +4 -6
  127. package/src/js/colour/hex.ts +5 -5
  128. package/src/js/colour/hsl.ts +18 -12
  129. package/src/js/colour/index.ts +1 -1
  130. package/src/js/colour/is.ts +3 -3
  131. package/src/js/colour/rgb.ts +17 -11
  132. package/src/js/emitter.ts +12 -9
  133. package/src/js/function.ts +17 -17
  134. package/src/js/index.ts +16 -16
  135. package/src/js/internal/array/callbacks.ts +33 -0
  136. package/src/js/internal/{array-find.ts → array/find.ts} +23 -24
  137. package/src/js/internal/{value-handle.ts → value/handle.ts} +1 -1
  138. package/src/js/is.ts +2 -2
  139. package/src/js/logger.ts +14 -14
  140. package/src/js/math.ts +1 -1
  141. package/src/js/models.ts +5 -5
  142. package/src/js/number.ts +2 -2
  143. package/src/js/query.ts +6 -10
  144. package/src/js/queue.ts +1 -1
  145. package/src/js/random.ts +9 -19
  146. package/src/js/sized.ts +145 -145
  147. package/src/js/string/case.ts +1 -1
  148. package/src/js/string/index.ts +13 -7
  149. package/src/js/string/template.ts +6 -3
  150. package/src/js/value/clone.ts +3 -7
  151. package/src/js/value/compare.ts +3 -5
  152. package/src/js/value/diff.ts +4 -4
  153. package/src/js/value/equal.ts +2 -2
  154. package/src/js/value/get.ts +3 -3
  155. package/src/js/value/index.ts +2 -2
  156. package/src/js/value/merge.ts +3 -3
  157. package/src/js/value/set.ts +3 -4
  158. package/src/js/value/smush.ts +4 -4
  159. package/src/js/value/unsmush.ts +4 -4
  160. package/types/array/chunk.d.cts +8 -0
  161. package/types/array/chunk.d.ts +2 -2
  162. package/types/array/compact.d.cts +12 -0
  163. package/types/array/compact.d.ts +4 -4
  164. package/types/array/count.d.cts +24 -0
  165. package/types/array/count.d.ts +13 -7
  166. package/types/array/exists.d.cts +26 -0
  167. package/types/array/exists.d.ts +11 -3
  168. package/types/array/filter.d.cts +26 -0
  169. package/types/array/filter.d.ts +14 -6
  170. package/types/array/find.d.cts +26 -0
  171. package/types/array/find.d.ts +14 -6
  172. package/types/array/group-by.d.cts +58 -0
  173. package/types/array/group-by.d.ts +49 -5
  174. package/types/array/index-of.d.cts +26 -0
  175. package/types/array/index-of.d.ts +14 -6
  176. package/types/array/index.d.cts +239 -95
  177. package/types/array/index.d.ts +7 -6
  178. package/types/array/insert.d.cts +16 -0
  179. package/types/array/insert.d.ts +10 -5
  180. package/types/array/models.d.cts +14 -11
  181. package/types/array/models.d.ts +14 -12
  182. package/types/array/shuffle.d.cts +8 -0
  183. package/types/array/shuffle.d.ts +2 -2
  184. package/types/array/sort.d.cts +24 -0
  185. package/types/array/sort.d.ts +10 -10
  186. package/types/array/splice.d.cts +22 -0
  187. package/types/array/splice.d.ts +4 -4
  188. package/types/array/to-map.d.cts +61 -0
  189. package/types/array/to-map.d.ts +42 -20
  190. package/types/array/to-record.d.cts +61 -0
  191. package/types/array/to-record.d.ts +42 -20
  192. package/types/array/unique.d.cts +21 -0
  193. package/types/array/unique.d.ts +10 -6
  194. package/types/colour/base.d.cts +153 -0
  195. package/types/colour/base.d.ts +2 -2
  196. package/types/colour/functions.d.cts +173 -0
  197. package/types/colour/functions.d.ts +3 -3
  198. package/types/colour/hex.d.cts +157 -0
  199. package/types/colour/hex.d.ts +4 -4
  200. package/types/colour/hsl.d.cts +157 -0
  201. package/types/colour/hsl.d.ts +16 -10
  202. package/types/colour/index.d.cts +28 -16
  203. package/types/colour/index.d.ts +1 -1
  204. package/types/colour/is.d.cts +170 -0
  205. package/types/colour/is.d.ts +3 -3
  206. package/types/colour/rgb.d.cts +157 -0
  207. package/types/colour/rgb.d.ts +15 -9
  208. package/types/emitter.d.cts +11 -8
  209. package/types/emitter.d.ts +11 -8
  210. package/types/function.d.cts +10 -10
  211. package/types/function.d.ts +11 -11
  212. package/types/index.d.cts +1120 -736
  213. package/types/index.d.ts +16 -16
  214. package/types/internal/array/callbacks.d.cts +11 -0
  215. package/types/internal/array/callbacks.d.ts +2 -0
  216. package/types/internal/array/find.d.cts +7 -0
  217. package/types/internal/array/find.d.ts +3 -0
  218. package/types/internal/value/handle.d.cts +37 -0
  219. package/types/internal/{value-handle.d.ts → value/handle.d.ts} +1 -1
  220. package/types/is.d.ts +1 -1
  221. package/types/logger.d.cts +13 -21
  222. package/types/logger.d.ts +13 -21
  223. package/types/math.d.cts +1 -1
  224. package/types/math.d.ts +1 -1
  225. package/types/models.d.cts +4 -4
  226. package/types/models.d.ts +1 -4
  227. package/types/number.d.cts +2 -2
  228. package/types/number.d.ts +2 -2
  229. package/types/query.d.cts +2 -2
  230. package/types/query.d.ts +3 -3
  231. package/types/queue.d.cts +1 -1
  232. package/types/queue.d.ts +1 -1
  233. package/types/random.d.cts +8 -12
  234. package/types/random.d.ts +8 -12
  235. package/types/sized.d.cts +6 -6
  236. package/types/sized.d.ts +6 -6
  237. package/types/string/case.d.cts +28 -0
  238. package/types/string/index.d.cts +30 -27
  239. package/types/string/index.d.ts +3 -3
  240. package/types/string/template.d.cts +50 -0
  241. package/types/string/template.d.ts +4 -1
  242. package/types/value/clone.d.cts +8 -0
  243. package/types/value/clone.d.ts +4 -2
  244. package/types/value/compare.d.cts +8 -0
  245. package/types/value/diff.d.cts +25 -0
  246. package/types/value/equal.d.cts +12 -0
  247. package/types/value/get.d.cts +1319 -0
  248. package/types/value/get.d.ts +1 -1
  249. package/types/value/index.d.cts +8 -6
  250. package/types/value/index.d.ts +2 -2
  251. package/types/value/merge.d.cts +72 -0
  252. package/types/value/merge.d.ts +2 -2
  253. package/types/value/set.d.cts +1011 -0
  254. package/types/value/set.d.ts +1 -1
  255. package/types/value/smush.d.cts +1368 -0
  256. package/types/value/smush.d.ts +2 -2
  257. package/types/value/unsmush.d.cts +141 -0
  258. package/types/value/unsmush.d.ts +2 -2
  259. package/dist/js/array/count.mjs +0 -8
  260. package/dist/js/array/exists.mjs +0 -8
  261. package/dist/js/array/filter.mjs +0 -8
  262. package/dist/js/array/find.mjs +0 -8
  263. package/dist/js/array/group-by.mjs +0 -33
  264. package/dist/js/array/index-of.mjs +0 -8
  265. package/dist/js/array/index.mjs +0 -34
  266. package/dist/js/array/insert.mjs +0 -22
  267. package/dist/js/array/models.mjs +0 -0
  268. package/dist/js/array/splice.mjs +0 -9
  269. package/dist/js/array/to-map.mjs +0 -27
  270. package/dist/js/array/to-record.mjs +0 -8
  271. package/dist/js/array/unique.mjs +0 -8
  272. package/dist/js/colour/index.mjs +0 -30
  273. package/dist/js/index.mjs +0 -17
  274. package/dist/js/internal/array-callbacks.mjs +0 -19
  275. package/dist/js/internal/array-find.mjs +0 -51
  276. package/dist/js/logger.mjs +0 -69
  277. package/dist/js/models.mjs +0 -0
  278. package/dist/js/string/index.mjs +0 -44
  279. package/dist/js/value/index.mjs +0 -23
  280. package/src/js/internal/array-callbacks.ts +0 -28
  281. package/types/internal/array-callbacks.d.ts +0 -2
  282. package/types/internal/array-find.d.ts +0 -4
@@ -1,57 +1,27 @@
1
- // src/js/emitter.ts
2
- import { noop } from "./function";
3
- function emitter(value) {
4
- return new Emitter(value);
5
- }
6
- function finishEmitter(state, emit) {
7
- if (state.active) {
8
- state.active = false;
9
- const entries = [...state.observers.entries()];
10
- const { length } = entries;
11
- for (let index = 0;index < length; index += 1) {
12
- const [subscription, observer] = entries[index];
13
- if (emit) {
14
- observer.complete?.();
15
- }
16
- subscription.destroy();
17
- }
18
- state.observers.clear();
19
- state.observable = undefined;
20
- state.observers = undefined;
21
- state.value = undefined;
22
- }
23
- }
24
- function getObserver(first, second, third) {
25
- let observer = {
26
- next: noop
27
- };
28
- if (typeof first === "object") {
29
- observer = first !== null && properties.every((property) => {
30
- const value = first[property];
31
- return value == null || typeof value === "function";
32
- }) ? first : observer;
33
- } else if (typeof first === "function") {
34
- observer = {
35
- error: typeof second === "function" ? second : noop,
36
- next: first,
37
- complete: typeof third === "function" ? third : undefined
38
- };
39
- }
40
- return observer;
41
- }
42
-
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const _function = require("./function.cjs");
43
4
  class Emitter {
5
+ /**
6
+ * Is the emitter active?
7
+ */
44
8
  get active() {
45
9
  return this.state.active;
46
10
  }
11
+ /**
12
+ * The observable that can be subscribed to
13
+ */
47
14
  get observable() {
48
15
  return this.state.observable;
49
16
  }
17
+ /**
18
+ * The current value
19
+ */
50
20
  get value() {
51
21
  return this.state.value;
52
22
  }
53
23
  constructor(value) {
54
- const observers = new Map;
24
+ const observers = /* @__PURE__ */ new Map();
55
25
  this.state = {
56
26
  observers,
57
27
  value,
@@ -59,35 +29,48 @@ class Emitter {
59
29
  observable: new Observable(this, observers)
60
30
  };
61
31
  }
32
+ /**
33
+ * Destroy the emitter
34
+ */
62
35
  destroy() {
63
36
  finishEmitter(this.state, false);
64
37
  }
38
+ /**
39
+ * Emit a new value _(and optionally finishes the emitter)_
40
+ */
65
41
  emit(value, finish) {
42
+ var _a;
66
43
  if (this.state.active) {
67
44
  this.state.value = value;
68
45
  for (const [, observer] of this.state.observers) {
69
- observer.next?.(value);
46
+ (_a = observer.next) == null ? void 0 : _a.call(observer, value);
70
47
  }
71
48
  if (finish === true) {
72
49
  finishEmitter(this.state, true);
73
50
  }
74
51
  }
75
52
  }
53
+ /**
54
+ * Emit an error _(and optionally finishes the emitter)_
55
+ */
76
56
  error(error, finish) {
57
+ var _a;
77
58
  if (this.state.active) {
78
59
  for (const [, observer] of this.state.observers) {
79
- observer.error?.(error);
60
+ (_a = observer.error) == null ? void 0 : _a.call(observer, error);
80
61
  }
81
62
  if (finish === true) {
82
63
  finishEmitter(this.state, true);
83
64
  }
84
65
  }
85
66
  }
67
+ /**
68
+ * Finish the emitter
69
+ */
86
70
  finish() {
87
71
  finishEmitter(this.state, true);
88
72
  }
89
73
  }
90
-
91
74
  class Observable {
92
75
  constructor(emitter2, observers) {
93
76
  this.state = {
@@ -96,14 +79,14 @@ class Observable {
96
79
  };
97
80
  }
98
81
  subscribe(first, second, third) {
82
+ var _a;
99
83
  const observer = getObserver(first, second, third);
100
84
  const instance = new Subscription(this.state);
101
85
  this.state.observers.set(instance, observer);
102
- observer.next?.(this.state.emitter.value);
86
+ (_a = observer.next) == null ? void 0 : _a.call(observer, this.state.emitter.value);
103
87
  return instance;
104
88
  }
105
89
  }
106
-
107
90
  class Subscription {
108
91
  constructor(state) {
109
92
  this.state = {
@@ -111,22 +94,71 @@ class Subscription {
111
94
  closed: false
112
95
  };
113
96
  }
97
+ /**
98
+ * Is the subscription closed?
99
+ */
114
100
  get closed() {
115
- return this.state.closed || !(this.state.emitter?.active ?? false);
101
+ var _a;
102
+ return this.state.closed || !(((_a = this.state.emitter) == null ? void 0 : _a.active) ?? false);
116
103
  }
104
+ /**
105
+ * Destroy the subscription
106
+ */
117
107
  destroy() {
118
108
  this.unsubscribe();
119
- this.state.emitter = undefined;
120
- this.state.observers = undefined;
109
+ this.state.emitter = void 0;
110
+ this.state.observers = void 0;
121
111
  }
112
+ /**
113
+ * Unsubscribe from its observable
114
+ */
122
115
  unsubscribe() {
116
+ var _a;
123
117
  if (!this.state.closed) {
124
118
  this.state.closed = true;
125
- this.state.observers?.delete(this);
119
+ (_a = this.state.observers) == null ? void 0 : _a.delete(this);
126
120
  }
127
121
  }
128
122
  }
129
- var properties = ["complete", "error", "next"];
130
- export {
131
- emitter
132
- };
123
+ const properties = ["complete", "error", "next"];
124
+ function emitter(value) {
125
+ return new Emitter(value);
126
+ }
127
+ function finishEmitter(state, emit) {
128
+ var _a;
129
+ if (state.active) {
130
+ state.active = false;
131
+ const entries = [...state.observers.entries()];
132
+ const { length } = entries;
133
+ for (let index = 0; index < length; index += 1) {
134
+ const [subscription, observer] = entries[index];
135
+ if (emit) {
136
+ (_a = observer.complete) == null ? void 0 : _a.call(observer);
137
+ }
138
+ subscription.destroy();
139
+ }
140
+ state.observers.clear();
141
+ state.observable = void 0;
142
+ state.observers = void 0;
143
+ state.value = void 0;
144
+ }
145
+ }
146
+ function getObserver(first, second, third) {
147
+ let observer = {
148
+ next: _function.noop
149
+ };
150
+ if (typeof first === "object") {
151
+ observer = first !== null && properties.every((property) => {
152
+ const value = first[property];
153
+ return value == null || typeof value === "function";
154
+ }) ? first : observer;
155
+ } else if (typeof first === "function") {
156
+ observer = {
157
+ error: typeof second === "function" ? second : _function.noop,
158
+ next: first,
159
+ complete: typeof third === "function" ? third : void 0
160
+ };
161
+ }
162
+ return observer;
163
+ }
164
+ exports.emitter = emitter;
@@ -1,60 +1,25 @@
1
- // src/js/function.ts
2
- function noop() {
3
- }
4
-
5
- // src/js/emitter.ts
6
- function emitter(value) {
7
- return new Emitter(value);
8
- }
9
- function finishEmitter(state, emit) {
10
- if (state.active) {
11
- state.active = false;
12
- const entries = [...state.observers.entries()];
13
- const { length } = entries;
14
- for (let index = 0;index < length; index += 1) {
15
- const [subscription, observer] = entries[index];
16
- if (emit) {
17
- observer.complete?.();
18
- }
19
- subscription.destroy();
20
- }
21
- state.observers.clear();
22
- state.observable = undefined;
23
- state.observers = undefined;
24
- state.value = undefined;
25
- }
26
- }
27
- function getObserver(first, second, third) {
28
- let observer = {
29
- next: noop
30
- };
31
- if (typeof first === "object") {
32
- observer = first !== null && properties.every((property) => {
33
- const value = first[property];
34
- return value == null || typeof value === "function";
35
- }) ? first : observer;
36
- } else if (typeof first === "function") {
37
- observer = {
38
- error: typeof second === "function" ? second : noop,
39
- next: first,
40
- complete: typeof third === "function" ? third : undefined
41
- };
42
- }
43
- return observer;
44
- }
45
-
1
+ import { noop } from "./function.js";
46
2
  class Emitter {
3
+ /**
4
+ * Is the emitter active?
5
+ */
47
6
  get active() {
48
7
  return this.state.active;
49
8
  }
9
+ /**
10
+ * The observable that can be subscribed to
11
+ */
50
12
  get observable() {
51
13
  return this.state.observable;
52
14
  }
15
+ /**
16
+ * The current value
17
+ */
53
18
  get value() {
54
19
  return this.state.value;
55
20
  }
56
21
  constructor(value) {
57
- const observers = new Map;
22
+ const observers = /* @__PURE__ */ new Map();
58
23
  this.state = {
59
24
  observers,
60
25
  value,
@@ -62,35 +27,48 @@ class Emitter {
62
27
  observable: new Observable(this, observers)
63
28
  };
64
29
  }
30
+ /**
31
+ * Destroy the emitter
32
+ */
65
33
  destroy() {
66
34
  finishEmitter(this.state, false);
67
35
  }
36
+ /**
37
+ * Emit a new value _(and optionally finishes the emitter)_
38
+ */
68
39
  emit(value, finish) {
40
+ var _a;
69
41
  if (this.state.active) {
70
42
  this.state.value = value;
71
43
  for (const [, observer] of this.state.observers) {
72
- observer.next?.(value);
44
+ (_a = observer.next) == null ? void 0 : _a.call(observer, value);
73
45
  }
74
46
  if (finish === true) {
75
47
  finishEmitter(this.state, true);
76
48
  }
77
49
  }
78
50
  }
51
+ /**
52
+ * Emit an error _(and optionally finishes the emitter)_
53
+ */
79
54
  error(error, finish) {
55
+ var _a;
80
56
  if (this.state.active) {
81
57
  for (const [, observer] of this.state.observers) {
82
- observer.error?.(error);
58
+ (_a = observer.error) == null ? void 0 : _a.call(observer, error);
83
59
  }
84
60
  if (finish === true) {
85
61
  finishEmitter(this.state, true);
86
62
  }
87
63
  }
88
64
  }
65
+ /**
66
+ * Finish the emitter
67
+ */
89
68
  finish() {
90
69
  finishEmitter(this.state, true);
91
70
  }
92
71
  }
93
-
94
72
  class Observable {
95
73
  constructor(emitter2, observers) {
96
74
  this.state = {
@@ -99,14 +77,14 @@ class Observable {
99
77
  };
100
78
  }
101
79
  subscribe(first, second, third) {
80
+ var _a;
102
81
  const observer = getObserver(first, second, third);
103
82
  const instance = new Subscription(this.state);
104
83
  this.state.observers.set(instance, observer);
105
- observer.next?.(this.state.emitter.value);
84
+ (_a = observer.next) == null ? void 0 : _a.call(observer, this.state.emitter.value);
106
85
  return instance;
107
86
  }
108
87
  }
109
-
110
88
  class Subscription {
111
89
  constructor(state) {
112
90
  this.state = {
@@ -114,22 +92,73 @@ class Subscription {
114
92
  closed: false
115
93
  };
116
94
  }
95
+ /**
96
+ * Is the subscription closed?
97
+ */
117
98
  get closed() {
118
- return this.state.closed || !(this.state.emitter?.active ?? false);
99
+ var _a;
100
+ return this.state.closed || !(((_a = this.state.emitter) == null ? void 0 : _a.active) ?? false);
119
101
  }
102
+ /**
103
+ * Destroy the subscription
104
+ */
120
105
  destroy() {
121
106
  this.unsubscribe();
122
- this.state.emitter = undefined;
123
- this.state.observers = undefined;
107
+ this.state.emitter = void 0;
108
+ this.state.observers = void 0;
124
109
  }
110
+ /**
111
+ * Unsubscribe from its observable
112
+ */
125
113
  unsubscribe() {
114
+ var _a;
126
115
  if (!this.state.closed) {
127
116
  this.state.closed = true;
128
- this.state.observers?.delete(this);
117
+ (_a = this.state.observers) == null ? void 0 : _a.delete(this);
129
118
  }
130
119
  }
131
120
  }
132
- var properties = ["complete", "error", "next"];
121
+ const properties = ["complete", "error", "next"];
122
+ function emitter(value) {
123
+ return new Emitter(value);
124
+ }
125
+ function finishEmitter(state, emit) {
126
+ var _a;
127
+ if (state.active) {
128
+ state.active = false;
129
+ const entries = [...state.observers.entries()];
130
+ const { length } = entries;
131
+ for (let index = 0; index < length; index += 1) {
132
+ const [subscription, observer] = entries[index];
133
+ if (emit) {
134
+ (_a = observer.complete) == null ? void 0 : _a.call(observer);
135
+ }
136
+ subscription.destroy();
137
+ }
138
+ state.observers.clear();
139
+ state.observable = void 0;
140
+ state.observers = void 0;
141
+ state.value = void 0;
142
+ }
143
+ }
144
+ function getObserver(first, second, third) {
145
+ let observer = {
146
+ next: noop
147
+ };
148
+ if (typeof first === "object") {
149
+ observer = first !== null && properties.every((property) => {
150
+ const value = first[property];
151
+ return value == null || typeof value === "function";
152
+ }) ? first : observer;
153
+ } else if (typeof first === "function") {
154
+ observer = {
155
+ error: typeof second === "function" ? second : noop,
156
+ next: first,
157
+ complete: typeof third === "function" ? third : void 0
158
+ };
159
+ }
160
+ return observer;
161
+ }
133
162
  export {
134
163
  emitter
135
164
  };
@@ -1,7 +1,65 @@
1
- // src/js/function.ts
2
- import { clamp } from "./number";
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const number = require("./number.cjs");
4
+ class Memoised {
5
+ constructor(callback) {
6
+ const cache = /* @__PURE__ */ new Map();
7
+ const getter = (...parameters) => {
8
+ const key = parameters[0];
9
+ if (cache.has(key)) {
10
+ return cache.get(key);
11
+ }
12
+ const value = callback(...parameters);
13
+ cache.set(key, value);
14
+ return value;
15
+ };
16
+ this.state = { cache, getter };
17
+ }
18
+ /**
19
+ * Clear the cache
20
+ */
21
+ clear() {
22
+ var _a;
23
+ (_a = this.state.cache) == null ? void 0 : _a.clear();
24
+ }
25
+ /**
26
+ * Delete a result from the cache
27
+ */
28
+ delete(key) {
29
+ var _a;
30
+ return (_a = this.state.cache) == null ? void 0 : _a.delete(key);
31
+ }
32
+ /**
33
+ * Destroy the instance, clearing its cache and removing its callback
34
+ */
35
+ destroy() {
36
+ this.state.cache.clear();
37
+ this.state.cache = void 0;
38
+ this.state.getter = noop;
39
+ }
40
+ /**
41
+ * Get a result from the cache if it exists _(or `undefined` otherwise)_
42
+ */
43
+ get(key) {
44
+ var _a;
45
+ return (_a = this.state.cache) == null ? void 0 : _a.get(key);
46
+ }
47
+ /**
48
+ * Does the result exist?
49
+ */
50
+ has(key) {
51
+ var _a;
52
+ return ((_a = this.state.cache) == null ? void 0 : _a.has(key)) ?? false;
53
+ }
54
+ /**
55
+ * Get the result from the cache if it exists; otherwise runs the callback, caches the result, and returns it
56
+ */
57
+ run(...parameters) {
58
+ return this.state.getter(...parameters);
59
+ }
60
+ }
3
61
  function debounce(callback, time) {
4
- const interval = clamp(time ?? 0, 0, 1000);
62
+ const interval = number.clamp(time ?? 0, 0, 1e3);
5
63
  let timer;
6
64
  const debounced = (...parameters) => {
7
65
  clearTimeout(timer);
@@ -20,7 +78,7 @@ function memoise(callback) {
20
78
  function noop() {
21
79
  }
22
80
  function throttle(callback, time) {
23
- const interval = clamp(time ?? 0, 0, 1000);
81
+ const interval = number.clamp(time ?? 0, 0, 1e3);
24
82
  let timestamp = performance.now();
25
83
  let timer;
26
84
  return (...parameters) => {
@@ -38,45 +96,7 @@ function throttle(callback, time) {
38
96
  }
39
97
  };
40
98
  }
41
-
42
- class Memoised {
43
- constructor(callback) {
44
- const cache = new Map;
45
- const getter = (...parameters) => {
46
- const key = parameters[0];
47
- if (cache.has(key)) {
48
- return cache.get(key);
49
- }
50
- const value = callback(...parameters);
51
- cache.set(key, value);
52
- return value;
53
- };
54
- this.state = { cache, getter };
55
- }
56
- clear() {
57
- this.state.cache?.clear();
58
- }
59
- delete(key) {
60
- return this.state.cache?.delete(key);
61
- }
62
- destroy() {
63
- this.state.cache.clear();
64
- this.state.cache = undefined;
65
- this.state.getter = noop;
66
- }
67
- get(key) {
68
- return this.state.cache?.get(key);
69
- }
70
- has(key) {
71
- return this.state.cache?.has(key) ?? false;
72
- }
73
- run(...parameters) {
74
- return this.state.getter(...parameters);
75
- }
76
- }
77
- export {
78
- throttle,
79
- noop,
80
- memoise,
81
- debounce
82
- };
99
+ exports.debounce = debounce;
100
+ exports.memoise = memoise;
101
+ exports.noop = noop;
102
+ exports.throttle = throttle;
@@ -1,14 +1,63 @@
1
- // src/js/number.ts
2
- function clamp(value, min, max, loop) {
3
- if (value < min) {
4
- return loop === true ? max : min;
1
+ import { clamp } from "./number.js";
2
+ class Memoised {
3
+ constructor(callback) {
4
+ const cache = /* @__PURE__ */ new Map();
5
+ const getter = (...parameters) => {
6
+ const key = parameters[0];
7
+ if (cache.has(key)) {
8
+ return cache.get(key);
9
+ }
10
+ const value = callback(...parameters);
11
+ cache.set(key, value);
12
+ return value;
13
+ };
14
+ this.state = { cache, getter };
15
+ }
16
+ /**
17
+ * Clear the cache
18
+ */
19
+ clear() {
20
+ var _a;
21
+ (_a = this.state.cache) == null ? void 0 : _a.clear();
22
+ }
23
+ /**
24
+ * Delete a result from the cache
25
+ */
26
+ delete(key) {
27
+ var _a;
28
+ return (_a = this.state.cache) == null ? void 0 : _a.delete(key);
29
+ }
30
+ /**
31
+ * Destroy the instance, clearing its cache and removing its callback
32
+ */
33
+ destroy() {
34
+ this.state.cache.clear();
35
+ this.state.cache = void 0;
36
+ this.state.getter = noop;
37
+ }
38
+ /**
39
+ * Get a result from the cache if it exists _(or `undefined` otherwise)_
40
+ */
41
+ get(key) {
42
+ var _a;
43
+ return (_a = this.state.cache) == null ? void 0 : _a.get(key);
44
+ }
45
+ /**
46
+ * Does the result exist?
47
+ */
48
+ has(key) {
49
+ var _a;
50
+ return ((_a = this.state.cache) == null ? void 0 : _a.has(key)) ?? false;
51
+ }
52
+ /**
53
+ * Get the result from the cache if it exists; otherwise runs the callback, caches the result, and returns it
54
+ */
55
+ run(...parameters) {
56
+ return this.state.getter(...parameters);
5
57
  }
6
- return value > max ? loop === true ? min : max : value;
7
58
  }
8
-
9
- // src/js/function.ts
10
59
  function debounce(callback, time) {
11
- const interval = clamp(time ?? 0, 0, 1000);
60
+ const interval = clamp(time ?? 0, 0, 1e3);
12
61
  let timer;
13
62
  const debounced = (...parameters) => {
14
63
  clearTimeout(timer);
@@ -27,7 +76,7 @@ function memoise(callback) {
27
76
  function noop() {
28
77
  }
29
78
  function throttle(callback, time) {
30
- const interval = clamp(time ?? 0, 0, 1000);
79
+ const interval = clamp(time ?? 0, 0, 1e3);
31
80
  let timestamp = performance.now();
32
81
  let timer;
33
82
  return (...parameters) => {
@@ -45,45 +94,9 @@ function throttle(callback, time) {
45
94
  }
46
95
  };
47
96
  }
48
-
49
- class Memoised {
50
- constructor(callback) {
51
- const cache = new Map;
52
- const getter = (...parameters) => {
53
- const key = parameters[0];
54
- if (cache.has(key)) {
55
- return cache.get(key);
56
- }
57
- const value = callback(...parameters);
58
- cache.set(key, value);
59
- return value;
60
- };
61
- this.state = { cache, getter };
62
- }
63
- clear() {
64
- this.state.cache?.clear();
65
- }
66
- delete(key) {
67
- return this.state.cache?.delete(key);
68
- }
69
- destroy() {
70
- this.state.cache.clear();
71
- this.state.cache = undefined;
72
- this.state.getter = noop;
73
- }
74
- get(key) {
75
- return this.state.cache?.get(key);
76
- }
77
- has(key) {
78
- return this.state.cache?.has(key) ?? false;
79
- }
80
- run(...parameters) {
81
- return this.state.getter(...parameters);
82
- }
83
- }
84
97
  export {
85
- throttle,
86
- noop,
98
+ debounce,
87
99
  memoise,
88
- debounce
100
+ noop,
101
+ throttle
89
102
  };