@oscarpalmer/atoms 0.186.2 → 0.187.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 (229) hide show
  1. package/dist/array/filter.d.mts +4 -4
  2. package/dist/array/find.d.mts +4 -4
  3. package/dist/array/first.d.mts +4 -4
  4. package/dist/array/get.d.mts +27 -1
  5. package/dist/array/get.mjs +5 -3
  6. package/dist/array/group-by.d.mts +6 -6
  7. package/dist/array/last.d.mts +4 -4
  8. package/dist/array/match.d.mts +7 -6
  9. package/dist/array/move.d.mts +7 -7
  10. package/dist/array/move.mjs +1 -1
  11. package/dist/array/select.d.mts +1 -0
  12. package/dist/array/slice.d.mts +3 -3
  13. package/dist/array/sort.d.mts +10 -7
  14. package/dist/array/sort.mjs +4 -3
  15. package/dist/array/swap.d.mts +1 -1
  16. package/dist/array/swap.mjs +1 -1
  17. package/dist/array/to-map.d.mts +32 -32
  18. package/dist/array/to-record.d.mts +6 -6
  19. package/dist/array/to-set.d.mts +6 -6
  20. package/dist/beacon.d.mts +6 -0
  21. package/dist/beacon.mjs +3 -0
  22. package/dist/color/index.d.mts +6 -2
  23. package/dist/color/index.mjs +6 -2
  24. package/dist/color/instance.d.mts +78 -14
  25. package/dist/color/instance.mjs +78 -14
  26. package/dist/color/misc/get.d.mts +39 -11
  27. package/dist/color/misc/get.mjs +39 -11
  28. package/dist/color/misc/is.d.mts +26 -11
  29. package/dist/color/misc/is.mjs +26 -11
  30. package/dist/color/misc/state.mjs +1 -1
  31. package/dist/color/models.d.mts +7 -4
  32. package/dist/color/space/hex.d.mts +15 -6
  33. package/dist/color/space/hex.mjs +15 -6
  34. package/dist/color/space/hsl.d.mts +10 -4
  35. package/dist/color/space/hsl.mjs +10 -4
  36. package/dist/color/space/rgb.d.mts +21 -12
  37. package/dist/color/space/rgb.mjs +21 -12
  38. package/dist/function/assert.d.mts +20 -14
  39. package/dist/function/assert.mjs +19 -13
  40. package/dist/function/limit.d.mts +8 -6
  41. package/dist/function/limit.mjs +8 -6
  42. package/dist/function/memoize.d.mts +15 -4
  43. package/dist/function/memoize.mjs +18 -4
  44. package/dist/function/once.d.mts +5 -3
  45. package/dist/function/once.mjs +5 -3
  46. package/dist/function/retry.d.mts +5 -2
  47. package/dist/function/retry.mjs +3 -1
  48. package/dist/function/work.d.mts +146 -98
  49. package/dist/{kalas.d.mts → herald.d.mts} +17 -8
  50. package/dist/{kalas.mjs → herald.mjs} +22 -13
  51. package/dist/index.d.mts +1464 -708
  52. package/dist/index.mjs +677 -324
  53. package/dist/internal/array/index-of.d.mts +4 -4
  54. package/dist/internal/array/insert.mjs +1 -1
  55. package/dist/internal/array/shuffle.d.mts +1 -0
  56. package/dist/internal/array/shuffle.mjs +2 -1
  57. package/dist/internal/is.d.mts +26 -8
  58. package/dist/internal/is.mjs +26 -8
  59. package/dist/internal/math/aggregate.d.mts +9 -9
  60. package/dist/internal/number.d.mts +13 -2
  61. package/dist/internal/number.mjs +13 -2
  62. package/dist/internal/random.d.mts +4 -2
  63. package/dist/internal/random.mjs +7 -5
  64. package/dist/internal/result.d.mts +17 -11
  65. package/dist/internal/result.mjs +10 -14
  66. package/dist/internal/string.d.mts +16 -1
  67. package/dist/internal/string.mjs +24 -1
  68. package/dist/internal/value/compare.d.mts +5 -2
  69. package/dist/internal/value/compare.mjs +5 -2
  70. package/dist/internal/value/equal.d.mts +12 -3
  71. package/dist/internal/value/equal.mjs +7 -4
  72. package/dist/internal/value/get.d.mts +17 -17
  73. package/dist/internal/value/has.d.mts +55 -11
  74. package/dist/internal/value/set.d.mts +24 -0
  75. package/dist/is.d.mts +12 -0
  76. package/dist/is.mjs +16 -1
  77. package/dist/logger.d.mts +7 -6
  78. package/dist/logger.mjs +6 -5
  79. package/dist/math.d.mts +36 -18
  80. package/dist/math.mjs +6 -3
  81. package/dist/models.d.mts +3 -3
  82. package/dist/promise/delay.d.mts +2 -0
  83. package/dist/promise/helpers.d.mts +6 -4
  84. package/dist/promise/helpers.mjs +6 -4
  85. package/dist/promise/index.d.mts +49 -38
  86. package/dist/promise/misc.d.mts +10 -7
  87. package/dist/promise/misc.mjs +4 -3
  88. package/dist/promise/models.d.mts +18 -19
  89. package/dist/promise/models.mjs +4 -3
  90. package/dist/promise/timed.d.mts +8 -6
  91. package/dist/query.d.mts +2 -0
  92. package/dist/query.mjs +2 -0
  93. package/dist/queue.d.mts +15 -4
  94. package/dist/queue.mjs +10 -3
  95. package/dist/random.d.mts +9 -2
  96. package/dist/random.mjs +7 -2
  97. package/dist/result/index.d.mts +14 -8
  98. package/dist/result/match.d.mts +18 -10
  99. package/dist/result/misc.d.mts +14 -7
  100. package/dist/result/misc.mjs +4 -2
  101. package/dist/result/models.d.mts +2 -0
  102. package/dist/result/work/flow.d.mts +86 -62
  103. package/dist/result/work/pipe.d.mts +55 -33
  104. package/dist/sized/map.d.mts +19 -16
  105. package/dist/sized/map.mjs +4 -4
  106. package/dist/sized/set.d.mts +20 -16
  107. package/dist/sized/set.mjs +8 -7
  108. package/dist/string/case.d.mts +8 -0
  109. package/dist/string/case.mjs +8 -0
  110. package/dist/string/fuzzy.d.mts +19 -5
  111. package/dist/string/fuzzy.mjs +7 -0
  112. package/dist/string/index.d.mts +17 -2
  113. package/dist/string/index.mjs +7 -9
  114. package/dist/string/match.d.mts +3 -0
  115. package/dist/string/match.mjs +3 -0
  116. package/dist/string/normalize.d.mts +6 -2
  117. package/dist/string/normalize.mjs +5 -2
  118. package/dist/string/template.d.mts +38 -9
  119. package/dist/string/template.mjs +19 -19
  120. package/dist/value/clone.d.mts +25 -4
  121. package/dist/value/clone.mjs +36 -22
  122. package/dist/value/collection.d.mts +9 -6
  123. package/dist/value/collection.mjs +3 -2
  124. package/dist/value/diff.d.mts +3 -1
  125. package/dist/value/diff.mjs +1 -0
  126. package/dist/value/freeze.d.mts +118 -9
  127. package/dist/value/freeze.mjs +56 -14
  128. package/dist/value/index.d.mts +1 -2
  129. package/dist/value/index.mjs +1 -2
  130. package/dist/value/merge.d.mts +11 -5
  131. package/dist/value/merge.mjs +2 -2
  132. package/dist/value/omit.d.mts +1 -0
  133. package/dist/value/omit.mjs +1 -0
  134. package/dist/value/pick.d.mts +1 -0
  135. package/dist/value/pick.mjs +1 -0
  136. package/dist/value/shake.d.mts +1 -0
  137. package/dist/value/shake.mjs +1 -0
  138. package/dist/value/smush.d.mts +1 -0
  139. package/dist/value/smush.mjs +1 -0
  140. package/dist/value/transform.d.mts +7 -3
  141. package/dist/value/unsmush.d.mts +1 -0
  142. package/dist/value/unsmush.mjs +1 -0
  143. package/package.json +11 -7
  144. package/src/array/filter.ts +4 -4
  145. package/src/array/find.ts +4 -4
  146. package/src/array/first.ts +4 -4
  147. package/src/array/get.ts +40 -5
  148. package/src/array/group-by.ts +6 -6
  149. package/src/array/last.ts +4 -4
  150. package/src/array/match.ts +7 -6
  151. package/src/array/move.ts +7 -7
  152. package/src/array/select.ts +1 -0
  153. package/src/array/slice.ts +3 -3
  154. package/src/array/sort.ts +15 -10
  155. package/src/array/swap.ts +1 -1
  156. package/src/array/to-map.ts +32 -32
  157. package/src/array/to-record.ts +6 -6
  158. package/src/array/to-set.ts +6 -6
  159. package/src/beacon.ts +6 -0
  160. package/src/color/index.ts +6 -2
  161. package/src/color/instance.ts +78 -14
  162. package/src/color/misc/get.ts +39 -11
  163. package/src/color/misc/is.ts +26 -11
  164. package/src/color/misc/state.ts +1 -1
  165. package/src/color/models.ts +7 -4
  166. package/src/color/space/hex.ts +15 -6
  167. package/src/color/space/hsl.ts +10 -4
  168. package/src/color/space/rgb.ts +21 -12
  169. package/src/function/assert.ts +20 -14
  170. package/src/function/limit.ts +8 -6
  171. package/src/function/memoize.ts +24 -5
  172. package/src/function/once.ts +5 -3
  173. package/src/function/retry.ts +7 -3
  174. package/src/function/work.ts +146 -98
  175. package/src/{kalas.ts → herald.ts} +23 -14
  176. package/src/index.ts +3 -2
  177. package/src/internal/array/index-of.ts +4 -4
  178. package/src/internal/array/insert.ts +1 -1
  179. package/src/internal/array/shuffle.ts +2 -1
  180. package/src/internal/is.ts +26 -8
  181. package/src/internal/math/aggregate.ts +9 -9
  182. package/src/internal/number.ts +13 -2
  183. package/src/internal/random.ts +10 -4
  184. package/src/internal/result.ts +30 -29
  185. package/src/internal/string.ts +28 -0
  186. package/src/internal/value/compare.ts +5 -2
  187. package/src/internal/value/equal.ts +13 -4
  188. package/src/internal/value/get.ts +17 -17
  189. package/src/internal/value/has.ts +54 -11
  190. package/src/internal/value/set.ts +24 -0
  191. package/src/is.ts +15 -1
  192. package/src/logger.ts +8 -7
  193. package/src/math.ts +36 -18
  194. package/src/models.ts +3 -3
  195. package/src/promise/delay.ts +2 -0
  196. package/src/promise/helpers.ts +6 -4
  197. package/src/promise/index.ts +49 -38
  198. package/src/promise/misc.ts +10 -7
  199. package/src/promise/models.ts +18 -19
  200. package/src/promise/timed.ts +8 -6
  201. package/src/query.ts +2 -0
  202. package/src/queue.ts +15 -4
  203. package/src/random.ts +8 -1
  204. package/src/result/index.ts +14 -8
  205. package/src/result/match.ts +18 -10
  206. package/src/result/misc.ts +17 -9
  207. package/src/result/models.ts +2 -0
  208. package/src/result/work/flow.ts +86 -62
  209. package/src/result/work/pipe.ts +55 -33
  210. package/src/sized/map.ts +20 -17
  211. package/src/sized/set.ts +21 -17
  212. package/src/string/case.ts +8 -0
  213. package/src/string/fuzzy.ts +19 -5
  214. package/src/string/index.ts +18 -15
  215. package/src/string/match.ts +3 -0
  216. package/src/string/normalize.ts +6 -2
  217. package/src/string/template.ts +69 -18
  218. package/src/value/clone.ts +51 -26
  219. package/src/value/collection.ts +9 -6
  220. package/src/value/diff.ts +3 -1
  221. package/src/value/freeze.ts +239 -25
  222. package/src/value/index.ts +0 -1
  223. package/src/value/merge.ts +11 -5
  224. package/src/value/omit.ts +1 -0
  225. package/src/value/pick.ts +1 -0
  226. package/src/value/shake.ts +1 -0
  227. package/src/value/smush.ts +1 -0
  228. package/src/value/transform.ts +7 -3
  229. package/src/value/unsmush.ts +1 -0
@@ -33,9 +33,10 @@ function isBytey(value: unknown): value is number {
33
33
  }
34
34
 
35
35
  /**
36
- * Is the value a Color?
36
+ * Is the value a _Color_?
37
+ *
37
38
  * @param value Value to check
38
- * @returns `true` if the value is a Color, otherwise `false`
39
+ * @returns `true` if the value is a _Color_, otherwise `false`
39
40
  */
40
41
  export function isColor(value: unknown): value is Color {
41
42
  return typeof value === 'object' && value !== null && '$color' in value && value.$color === true;
@@ -75,9 +76,19 @@ function isDegree(value: unknown): value is number {
75
76
 
76
77
  /**
77
78
  * Is the value a hex color?
79
+ *
78
80
  * @param value Value to check
79
- * @param alpha Allow alpha channel? _(defaults to `true`)_
81
+ * @param alpha Allow alpha channel _(opacity)_? _(defaults to `true`)_
80
82
  * @returns `true` if the value is a hex color, otherwise `false`
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * isHexColor('ff0000'); // => true
87
+ * isHexColor('#ff0000'); // => true
88
+ *
89
+ * isHexColor('#ff000050'); // => true
90
+ * isHexColor('#ff000050', false); // => false
91
+ * ```
81
92
  */
82
93
  export function isHexColor(value: unknown, alpha?: boolean): value is string {
83
94
  if (typeof value !== 'string') {
@@ -96,18 +107,20 @@ export function isHexColor(value: unknown, alpha?: boolean): value is string {
96
107
  }
97
108
 
98
109
  /**
99
- * Is the value an HSLA color?
110
+ * Is the value an _HSLA_ color?
111
+ *
100
112
  * @param value Value to check
101
- * @returns `true` if the value is an HSLA color, otherwise `false`
113
+ * @returns `true` if the value is an _HSLA_ color, otherwise `false`
102
114
  */
103
115
  export function isHslaColor(value: unknown): value is HSLAColor {
104
116
  return isColorValue(value, KEYS_HSLA);
105
117
  }
106
118
 
107
119
  /**
108
- * Is the value an HSL color?
120
+ * Is the value an _HSL_ color?
121
+ *
109
122
  * @param value Value to check
110
- * @returns `true` if the value is an HSL color, otherwise `false`
123
+ * @returns `true` if the value is an _HSL_ color, otherwise `false`
111
124
  */
112
125
  export function isHslColor(value: unknown): value is HSLColor {
113
126
  return isColorValue(value, KEYS_HSLA) || isColorValue(value, KEYS_HSL);
@@ -118,18 +131,20 @@ export function isHslLike(value: unknown): value is Record<keyof HSLColor, unkno
118
131
  }
119
132
 
120
133
  /**
121
- * Is the value an RGBA color?
134
+ * Is the value an _RGBA_ color?
135
+ *
122
136
  * @param value Value to check
123
- * @returns `true` if the value is an RGBA color, otherwise `false`
137
+ * @returns `true` if the value is an _RGBA_ color, otherwise `false`
124
138
  */
125
139
  export function isRgbaColor(value: unknown): value is RGBAColor {
126
140
  return isColorValue(value, KEYS_RGBA);
127
141
  }
128
142
 
129
143
  /**
130
- * Is the value an RGB color?
144
+ * Is the value an _RGB_ color?
145
+ *
131
146
  * @param value Value to check
132
- * @returns `true` if the value is an RGB color, otherwise `false`
147
+ * @returns `true` if the value is an _RGB_ color, otherwise `false`
133
148
  */
134
149
  export function isRgbColor(value: unknown): value is RGBColor {
135
150
  return isColorValue(value, KEYS_RGBA) || isColorValue(value, KEYS_RGB);
@@ -63,7 +63,7 @@ export function getColorState(value: unknown): ColorState {
63
63
  }
64
64
 
65
65
  state.alpha ??= getAlpha(ALPHA_FULL_VALUE);
66
- state.hex ??= String(HEX_BLACK);
66
+ state.hex ??= HEX_BLACK;
67
67
  state.hsl ??= {...DEFAULT_HSL};
68
68
  state.rgb ??= {...DEFAULT_RGB};
69
69
 
@@ -6,16 +6,19 @@ export type Alpha = {
6
6
  };
7
7
 
8
8
  type ColorWithAlpha = {
9
+ /**
10
+ * Alpha channel _(opacity)_ of the color _(in percentage; 0-100)_
11
+ */
9
12
  alpha: number;
10
13
  };
11
14
 
12
15
  /**
13
- * An _HSL_-color with an alpha channel
16
+ * An _HSL_ color with an alpha channel _(opacity)_
14
17
  */
15
18
  export type HSLAColor = HSLColor & ColorWithAlpha;
16
19
 
17
20
  /**
18
- * An _HSL_-color
21
+ * An _HSL_ color
19
22
  */
20
23
  export type HSLColor = {
21
24
  /**
@@ -33,12 +36,12 @@ export type HSLColor = {
33
36
  };
34
37
 
35
38
  /**
36
- * An _RGB_-color with an alpha channel
39
+ * An _RGB_ color with an alpha channel _(opacity)_
37
40
  */
38
41
  export type RGBAColor = RGBColor & ColorWithAlpha;
39
42
 
40
43
  /**
41
- * An _RGB_-color
44
+ * An _RGB_ color
42
45
  */
43
46
  export type RGBColor = {
44
47
  /**
@@ -36,9 +36,12 @@ function convertHexToRgba(value: string): RGBAColor {
36
36
 
37
37
  /**
38
38
  * Get the normalized hex color from a value
39
+ *
40
+ * _If the value is unable to be parsed or normalized, a hex color of `000000` will be returned, with the alpha channel _(opacity)_ value `0` included, if specified_
41
+ *
39
42
  * @param value Value to normalize
40
- * @param alpha Include alpha channel? _(defaults to `false`)_
41
- * @returns Normalized hex color, or `000000` if the value is unable to be normalized
43
+ * @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
44
+ * @returns Normalized hex color
42
45
  */
43
46
  export function getNormalizedHex(value: unknown, alpha?: boolean): string {
44
47
  const includeAlpha = alpha ?? false;
@@ -78,9 +81,12 @@ export function hexToHsla(value: string): HSLAColor {
78
81
  }
79
82
 
80
83
  /**
81
- * Convert a hex color to an RGB color
84
+ * Convert a hex color to an _RGB_ color
85
+ *
86
+ * _If the value is unable to be converted, a black RGB color will be returned_
87
+ *
82
88
  * @param value Original value
83
- * @returns RGB color
89
+ * @returns _RGB_ color
84
90
  */
85
91
  export function hexToRgb(value: string): RGBColor {
86
92
  const {blue, green, red} = convertHexToRgba(value);
@@ -89,9 +95,12 @@ export function hexToRgb(value: string): RGBColor {
89
95
  }
90
96
 
91
97
  /**
92
- * Convert a hex color to an RGBA color
98
+ * Convert a hex color to an _RGBA_ color
99
+ *
100
+ * _If the value is unable to be converted, a black RGBA color with an alpha channel (opacity) of `0` will be returned_
101
+ *
93
102
  * @param value Original value
94
- * @returns RGBA color
103
+ * @returns _RGBA_ color
95
104
  */
96
105
  export function hexToRgba(value: string): RGBAColor {
97
106
  return convertHexToRgba(value);
@@ -42,9 +42,12 @@ export function hslToHex(hsl: HSLAColor | HSLColor, alpha?: boolean): string {
42
42
  }
43
43
 
44
44
  /**
45
- * Convert an HSL(A) color to an RGB color
45
+ * Convert an _HSL(A)_ color to an _RGB_ color
46
+ *
47
+ * _If the value is unable to be converted, a black RGB color will be returned_
48
+ *
49
+ * _Thanks, https://github.com/color-js/color.js/blob/main/src/spaces/hsl.js#L61_
46
50
  *
47
- * Thanks, https://github.com/color-js/color.js/blob/main/src/spaces/hsl.js#L61
48
51
  * @param hsl HSL(A) color
49
52
  * @returns RGB color
50
53
  */
@@ -59,9 +62,12 @@ export function hslToRgb(hsl: HSLAColor | HSLColor): RGBColor {
59
62
  }
60
63
 
61
64
  /**
62
- * Convert an HSL(A) color to an RGBA color
65
+ * Convert an _HSL(A)_ color to an _RGBA_ color
66
+ *
67
+ * _If the value is unable to be converted, a black RGBA color with an alpha channel (opactiy) of `0` will be returned_
68
+ *
69
+ * _Thanks, https://github.com/color-js/color.js/blob/main/src/spaces/hsl.js#L61_
63
70
  *
64
- * Thanks, https://github.com/color-js/color.js/blob/main/src/spaces/hsl.js#L61
65
71
  * @param hsl HSL(A) color
66
72
  * @returns RGBA color
67
73
  */
@@ -77,21 +77,27 @@ export function getRgbValue(value: Record<keyof RGBColor, unknown>): RGBColor {
77
77
  }
78
78
 
79
79
  /**
80
- * Convert an RGB(A) color to a hex color _(with optional alpha channel)_
81
- * @param rgb RGB(A) color
82
- * @param alpha Include alpha channel? _(defaults to `false`)_
83
- * @returns Hex color
80
+ * Convert an _RGB(A)_ color to a hex color _(with optional alpha channel, i.e., opacity)_
81
+ *
82
+ * _If the value is unable to be converted, a black hex color will be returned_
83
+ *
84
+ * @param rgb _RGB(A)_ color
85
+ * @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
86
+ * @returns Hex color string
84
87
  */
85
88
  export function rgbToHex(rgb: RGBAColor | RGBColor, alpha?: boolean): string {
86
89
  return convertRgbToHex(isRgbLike(rgb) ? getRgbValue(rgb) : {...DEFAULT_RGB}, alpha ?? false);
87
90
  }
88
91
 
89
92
  /**
90
- * Convert an RGB(A) color to an HSL color
93
+ * Convert an _RGB(A)_ color to an _HSL_ color
94
+ *
95
+ * _If the value is unable to be converted, a black HSL color will be returned_
91
96
  *
92
- * Thanks, https://github.com/color-js/color.js/blob/main/src/spaces/hsl.js#L26
93
- * @param rgb RGB(A) color
94
- * @returns HSL color
97
+ * _Thanks, https://github.com/color-js/color.js/blob/main/src/spaces/hsl.js#L26_
98
+ *
99
+ * @param rgb _RGB(A)_ color
100
+ * @returns _HSL_ color
95
101
  */
96
102
  export function rgbToHsl(rgb: RGBAColor | RGBColor): HSLColor {
97
103
  const {hue, lightness, saturation} = convertRgbToHsla(rgb);
@@ -104,11 +110,14 @@ export function rgbToHsl(rgb: RGBAColor | RGBColor): HSLColor {
104
110
  }
105
111
 
106
112
  /**
107
- * Convert an RGB(A) color to an HSLA color
113
+ * Convert an _RGB(A)_ color to an _HSLA_ color
114
+ *
115
+ * _If the value is unable to be converted, a black _HSLA_ color with an alpha channel (opacity) of `0` will be returned_
116
+ *
117
+ * _Thanks, https://github.com/color-js/color.js/blob/main/src/spaces/hsl.js#L26_
108
118
  *
109
- * Thanks, https://github.com/color-js/color.js/blob/main/src/spaces/hsl.js#L26
110
- * @param rgb RGB(A) color
111
- * @returns HSLA color
119
+ * @param rgb _RGB(A)_ color
120
+ * @returns _HSLA_ color
112
121
  */
113
122
  export function rgbToHsla(rgb: RGBAColor | RGBColor): HSLAColor {
114
123
  return convertRgbToHsla(rgb);
@@ -4,7 +4,7 @@ import type {Constructor, NestedKeys, NestedValue, PlainObject} from '../models'
4
4
  // #region Types
5
5
 
6
6
  /**
7
- * Asserter for a nested property of a value
7
+ * Asserter for a property of a value
8
8
  */
9
9
  export type AssertProperty<
10
10
  Value extends PlainObject,
@@ -33,6 +33,7 @@ type NestedPick<Value, Path extends string> = Value extends PlainObject
33
33
 
34
34
  /**
35
35
  * Asserts that a condition is true, throwing an error if it is not
36
+ *
36
37
  * @param condition Condition to assert
37
38
  * @param message Error message
38
39
  * @param error Error constructor _(defaults to `Error`)_
@@ -54,13 +55,14 @@ assert.is = assertIs;
54
55
  assert.property = assertProperty;
55
56
 
56
57
  /**
57
- * Creates an asserter that asserts a condition is true, throwing an error if it is not
58
+ * Creates an _Asserter_ that asserts a condition is true, throwing an error if it is not
59
+ *
60
+ * _Available as `assertCondition` and `assert.condition`_
58
61
  *
59
- * Available as `assertCondition` and `assert.condition`
60
62
  * @param condition Condition to assert
61
63
  * @param message Error message
62
64
  * @param error Error constructor _(defaults to `Error`)_
63
- * @returns Asserter
65
+ * @returns _Asserter_
64
66
  */
65
67
  export function assertCondition<Value>(
66
68
  condition: (value: unknown) => boolean,
@@ -75,7 +77,8 @@ export function assertCondition<Value>(
75
77
  /**
76
78
  * Asserts that a value is defined, throwing an error if it is not
77
79
  *
78
- * Available as `assertDefined` and `assert.defined`
80
+ * _Available as `assertDefined` and `assert.defined`_
81
+ *
79
82
  * @param value Value to assert
80
83
  * @param message Error message
81
84
  * @param error Error constructor _(defaults to `Error`)_
@@ -89,13 +92,14 @@ export function assertDefined<Value>(
89
92
  }
90
93
 
91
94
  /**
92
- * Creates an asserter that asserts a value is an instance of a constructor, throwing an error if it is not
95
+ * Creates an _Asserter_ that asserts a value is an instance of a constructor, throwing an error if it is not
96
+ *
97
+ * _Available as `assertInstanceOf` and `assert.instanceOf`_
93
98
  *
94
- * Available as `assertInstanceOf` and `assert.instanceOf`
95
99
  * @param constructor Constructor to check against
96
100
  * @param message Error message
97
101
  * @param error Error constructor _(defaults to `Error`)_
98
- * @returns Asserter
102
+ * @returns _Asserter_
99
103
  */
100
104
  export function assertInstanceOf<Value>(
101
105
  constructor: Constructor<Value>,
@@ -108,13 +112,14 @@ export function assertInstanceOf<Value>(
108
112
  }
109
113
 
110
114
  /**
111
- * Creates an asserter that asserts a value is of a specific type, throwing an error if it is not
115
+ * Creates an _Asserter_ that asserts a value is of a specific type, throwing an error if it is not
116
+ *
117
+ * _Available as `assertIs` and `assert.is`_
112
118
  *
113
- * Available as `assertIs` and `assert.is`
114
119
  * @param condition Type guard function to check the value
115
120
  * @param message Error message
116
121
  * @param error Error constructor _(defaults to `Error`)_
117
- * @returns Asserter
122
+ * @returns _Asserter_
118
123
  */
119
124
  export function assertIs<Value>(
120
125
  condition: (value: unknown) => value is Value,
@@ -127,14 +132,15 @@ export function assertIs<Value>(
127
132
  }
128
133
 
129
134
  /**
130
- * Creates an asserter that asserts a property of a value exists and satisfies a condition, throwing an error if it does not
135
+ * Creates an _Asserter_ that asserts a property of a value exists and satisfies a condition, throwing an error if it does not
136
+ *
137
+ * _Available as `assertProperty` and `assert.property`_
131
138
  *
132
- * Available as `assertProperty` and `assert.property`
133
139
  * @param path Path to the property to check, e.g., `foo.bar.baz` for a nested property
134
140
  * @param condition Condition to assert for the property
135
141
  * @param message Error message
136
142
  * @param error Error constructor _(defaults to `Error`)_
137
- * @returns Asserter
143
+ * @returns _Asserter_
138
144
  */
139
145
  export function assertProperty<
140
146
  Value extends PlainObject,
@@ -11,11 +11,11 @@ import type {
11
11
  /**
12
12
  * Debounce a function, ensuring it is only called after `time` milliseconds have passed
13
13
  *
14
- * When called, successful _(finished)_ results will resolve and errors will reject.
14
+ * - When called, successful _(finished)_ results will resolve and errors will reject
15
+ * - On subsequent calls, existing calls will be canceled _(rejected)_, the timer reset, and will wait another `time` milliseconds before the new call is made _(and so on...)_
15
16
  *
16
- * On subsequent calls, existing calls will be canceled _(rejected)_, the timer reset, and will wait another `time` milliseconds before the new call is made _(and so on...)_
17
+ * _Available as `asyncDebounce` and `debounce.async`_
17
18
  *
18
- * Available as `asyncDebounce` and `debounce.async`
19
19
  * @param callback Callback to debounce
20
20
  * @param time Time in milliseconds to wait before calling the callback _(defaults to `0`; e.g., as soon as possible)_
21
21
  * @returns Debounced callback handler with a `cancel` method
@@ -30,11 +30,11 @@ export function asyncDebounce<Callback extends GenericAsyncCallback | GenericCal
30
30
  /**
31
31
  * Throttle a function, ensuring it is only called once every `time` milliseconds
32
32
  *
33
- * When called, successful _(finished)_ results will resolve and errors will reject.
33
+ * - When called, successful _(finished)_ results will resolve and errors will reject
34
+ * - On subsequent calls, existing calls will be canceled _(rejected)_ and will wait until the next valid time to call the callback again _(and so on...)_
34
35
  *
35
- * On subsequent calls, existing calls will be canceled _(rejected)_ and will wait until the next valid time to call the callback again _(and so on...)_
36
+ * _Available as `asyncThrottle` and `throttle.async`_
36
37
  *
37
- * Available as `asyncThrottle` and `throttle.async`
38
38
  * @param callback Callback to throttle
39
39
  * @param time Time in milliseconds to wait before calling the callback again _(defaults to `0`; e.g., as soon as possible)_
40
40
  * @returns Throttled callback handler with a `cancel` method
@@ -50,6 +50,7 @@ export function asyncThrottle<Callback extends GenericAsyncCallback | GenericCal
50
50
  * Debounce a function, ensuring it is only called after `time` milliseconds have passed
51
51
  *
52
52
  * On subsequent calls, the timer is reset and will wait another `time` milliseconds _(and so on...)_
53
+ *
53
54
  * @param callback Callback to debounce
54
55
  * @param time Time in milliseconds to wait before calling the callback _(defaults to `0`; e.g., as soon as possible)_
55
56
  * @returns Debounced callback handler with a `cancel` method
@@ -65,6 +66,7 @@ debounce.async = asyncDebounce;
65
66
 
66
67
  /**
67
68
  * Throttle a function, ensuring it is only called once every `time` milliseconds
69
+ *
68
70
  * @param callback Callback to throttle
69
71
  * @param time Time in milliseconds to wait before calling the callback again _(defaults to `0`; e.g., as soon as possible)_
70
72
  * @returns Throttled callback handler with a `cancel` method
@@ -6,13 +6,15 @@ import {SizedMap} from '../sized/map';
6
6
  // #region Types
7
7
 
8
8
  /**
9
- * A memoized function, caching and retrieving results based on the its parameters _(or a custom cache key)_
9
+ * A _Memoized_ function instance, caching and retrieving results based on the its parameters _(or a custom cache key)_
10
10
  */
11
11
  class Memoized<Callback extends GenericCallback> {
12
12
  readonly #state: MemoizedState<Callback>;
13
13
 
14
14
  /**
15
15
  * Maximum cache size
16
+ *
17
+ * @returns Maximum cache size _(or `Number.NaN` if the instance has been destroyed)_
16
18
  */
17
19
  get maximum(): number {
18
20
  return this.#state.cache?.maximum ?? Number.NaN;
@@ -20,6 +22,8 @@ class Memoized<Callback extends GenericCallback> {
20
22
 
21
23
  /**
22
24
  * Current cache size
25
+ *
26
+ * @returns Current cache size _(or `Number.NaN` if the instance has been destroyed)_
23
27
  */
24
28
  get size(): number {
25
29
  return this.#state.cache?.size ?? Number.NaN;
@@ -56,6 +60,7 @@ class Memoized<Callback extends GenericCallback> {
56
60
 
57
61
  /**
58
62
  * Delete a result from the cache
63
+ *
59
64
  * @param key Key to delete
60
65
  * @returns `true` if the key existed and was removed, otherwise `false`
61
66
  */
@@ -64,7 +69,9 @@ class Memoized<Callback extends GenericCallback> {
64
69
  }
65
70
 
66
71
  /**
67
- * Destroy the instance _(clearing its cache and removing its callback)_
72
+ * Destroy the instance
73
+ *
74
+ * _(When a Memoized instance is destroyed, its cache and callback are removed, and calls to `run` will throw an error)_
68
75
  */
69
76
  destroy(): void {
70
77
  this.#state.cache?.clear();
@@ -75,6 +82,7 @@ class Memoized<Callback extends GenericCallback> {
75
82
 
76
83
  /**
77
84
  * Get a result from the cache
85
+ *
78
86
  * @param key Key to get
79
87
  * @returns Cached result or `undefined` if it does not exist
80
88
  */
@@ -84,6 +92,7 @@ class Memoized<Callback extends GenericCallback> {
84
92
 
85
93
  /**
86
94
  * Does the result exist?
95
+ *
87
96
  * @param key Key to check
88
97
  * @returns `true` if the result exists, otherwise `false`
89
98
  */
@@ -93,12 +102,13 @@ class Memoized<Callback extends GenericCallback> {
93
102
 
94
103
  /**
95
104
  * Run the callback with the provided parameters
105
+ *
96
106
  * @param parameters Parameters to pass to the callback
97
107
  * @returns Cached or computed _(then cached)_ result
98
108
  */
99
109
  run(...parameters: Parameters<Callback>): ReturnType<Callback> {
100
110
  if (this.#state.cache == null || this.#state.getter == null) {
101
- throw new Error('The Memoized instance has been destroyed');
111
+ throw new Error(MEMOIZED_ERROR_DESTROYED);
102
112
  }
103
113
 
104
114
  return this.#state.getter(...parameters);
@@ -106,7 +116,7 @@ class Memoized<Callback extends GenericCallback> {
106
116
  }
107
117
 
108
118
  /**
109
- * Options for memoized functions
119
+ * Options for a _Memoized_ function
110
120
  */
111
121
  type MemoizedOptions<Callback extends GenericCallback> = {
112
122
  /**
@@ -146,14 +156,19 @@ function getMemoizationOptions<Callback extends GenericCallback>(
146
156
 
147
157
  /**
148
158
  * Memoize a function, caching and retrieving results based on the first parameter
159
+ *
149
160
  * @param callback Callback to memoize
150
161
  * @param options Memoization options
151
- * @returns Memoized instance
162
+ * @returns _Memoized_ instance
152
163
  */
153
164
  export function memoize<Callback extends GenericCallback>(
154
165
  callback: Callback,
155
166
  options?: MemoizedOptions<Callback>,
156
167
  ): Memoized<Callback> {
168
+ if (typeof callback !== 'function') {
169
+ throw new TypeError(MEMOIZED_ERROR_CALLBACK);
170
+ }
171
+
157
172
  return new Memoized(callback, getMemoizationOptions(options));
158
173
  }
159
174
 
@@ -163,6 +178,10 @@ export function memoize<Callback extends GenericCallback>(
163
178
 
164
179
  const DEFAULT_CACHE_SIZE = 1024;
165
180
 
181
+ const MEMOIZED_ERROR_CALLBACK = 'Memoized requires a callback function';
182
+
183
+ const MEMOIZED_ERROR_DESTROYED = 'The Memoized instance has been destroyed';
184
+
166
185
  const SEPARATOR = '_';
167
186
 
168
187
  // #endregion
@@ -32,9 +32,10 @@ type OnceState<Value> = {
32
32
  /**
33
33
  * Create an asynchronous function that can only be called once, rejecting or resolving the same result on subsequent calls
34
34
  *
35
- * Available as `asyncOnce` and `once.async`
35
+ * _Available as `asyncOnce` and `once.async`_
36
+ *
36
37
  * @param callback Callback to use once
37
- * @returns Once callback
38
+ * @returns _Once_ callback
38
39
  */
39
40
  export function asyncOnce<Callback extends GenericAsyncCallback>(
40
41
  callback: Callback,
@@ -132,8 +133,9 @@ function handleOnceResult<Value>(
132
133
 
133
134
  /**
134
135
  * Create a function that can only be called once, returning the same value on subsequent calls
136
+ *
135
137
  * @param callback Callback to use once
136
- * @returns Once callback
138
+ * @returns _Once_ callback
137
139
  */
138
140
  export function once<Callback extends GenericCallback>(callback: Callback): OnceCallback<Callback> {
139
141
  assert(() => typeof callback === 'function', ONCE_MESSAGE_EXPECTATION);
@@ -31,7 +31,8 @@ export type RetryOptions = {
31
31
  /**
32
32
  * Retry a callback a specified number of times, with a delay between attempts
33
33
  *
34
- * Available as `asyncRetry` and `retry.async`
34
+ * _Available as `asyncRetry` and `retry.async`_
35
+ *
35
36
  * @param callback Callback to retry
36
37
  * @param options Retry options
37
38
  * @returns Callback result
@@ -44,7 +45,8 @@ async function asyncRetry<Callback extends GenericAsyncCallback>(
44
45
  /**
45
46
  * Retry a callback a specified number of times, with a delay between attempts
46
47
  *
47
- * Available as `asyncRetry` and `retry.async`
48
+ * _Available as `asyncRetry` and `retry.async`_
49
+ *
48
50
  * @param callback Callback to retry
49
51
  * @param options Retry options
50
52
  * @returns Callback result
@@ -57,7 +59,8 @@ async function asyncRetry<Callback extends GenericCallback>(
57
59
  /**
58
60
  * Retry a callback a specified number of times, with a delay between attempts
59
61
  *
60
- * Available as `asyncRetry` and `retry.async`
62
+ * _Available as `asyncRetry` and `retry.async`_
63
+ *
61
64
  * @param callback Callback to retry
62
65
  * @param options Retry options
63
66
  * @returns Callback result
@@ -119,6 +122,7 @@ function getRetryOptions(input?: RetryOptions): Required<RetryOptions> {
119
122
 
120
123
  /**
121
124
  * Retry a callback a specified number of times
125
+ *
122
126
  * @param callback Callback to retry
123
127
  * @param options Retry options
124
128
  * @returns Callback result