@noeldemartin/utils 0.7.3 → 0.8.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 (234) hide show
  1. package/dist/_virtual/_rolldown/runtime.js +13 -0
  2. package/dist/fluent/FluentArray.d.ts +48 -0
  3. package/dist/fluent/FluentArray.js +48 -0
  4. package/dist/fluent/FluentArray.js.map +1 -0
  5. package/dist/fluent/FluentObject.d.ts +16 -0
  6. package/dist/fluent/FluentObject.js +45 -0
  7. package/dist/fluent/FluentObject.js.map +1 -0
  8. package/dist/fluent/FluentString.d.ts +21 -0
  9. package/dist/fluent/FluentString.js +28 -0
  10. package/dist/fluent/FluentString.js.map +1 -0
  11. package/dist/fluent/facade.d.ts +17 -0
  12. package/dist/fluent/facade.js +45 -0
  13. package/dist/fluent/facade.js.map +1 -0
  14. package/dist/fluent/index.d.ts +29 -0
  15. package/dist/fluent/index.js +41 -0
  16. package/dist/fluent/index.js.map +1 -0
  17. package/dist/fluent/invade.d.ts +6 -0
  18. package/dist/fluent/invade.js +8 -0
  19. package/dist/fluent/invade.js.map +1 -0
  20. package/dist/fluent/lazyFacade.d.ts +16 -0
  21. package/dist/fluent/lazyFacade.js +67 -0
  22. package/dist/fluent/lazyFacade.js.map +1 -0
  23. package/dist/fluent/noop.d.ts +4 -0
  24. package/dist/fluent/noop.js +6 -0
  25. package/dist/fluent/noop.js.map +1 -0
  26. package/dist/fluent/required.d.ts +5 -0
  27. package/dist/fluent/required.js +32 -0
  28. package/dist/fluent/required.js.map +1 -0
  29. package/dist/fluent/tap.d.ts +7 -0
  30. package/dist/fluent/tap.js +22 -0
  31. package/dist/fluent/tap.js.map +1 -0
  32. package/dist/fluent/value.d.ts +5 -0
  33. package/dist/fluent/value.js +8 -0
  34. package/dist/fluent/value.js.map +1 -0
  35. package/dist/fluent/when.d.ts +6 -0
  36. package/dist/fluent/when.js +11 -0
  37. package/dist/fluent/when.js.map +1 -0
  38. package/dist/helpers/InMemoryStorageService.d.ts +8 -0
  39. package/dist/helpers/InMemoryStorageService.js +16 -0
  40. package/dist/helpers/InMemoryStorageService.js.map +1 -0
  41. package/dist/helpers/IndexedDBMap.d.ts +17 -0
  42. package/dist/helpers/IndexedDBMap.js +52 -0
  43. package/dist/helpers/IndexedDBMap.js.map +1 -0
  44. package/dist/helpers/JSError.d.ts +14 -0
  45. package/dist/helpers/JSError.js +14 -0
  46. package/dist/helpers/JSError.js.map +1 -0
  47. package/dist/helpers/ListenersManager.d.ts +15 -0
  48. package/dist/helpers/ListenersManager.js +23 -0
  49. package/dist/helpers/ListenersManager.js.map +1 -0
  50. package/dist/helpers/MagicObject.d.ts +29 -0
  51. package/dist/helpers/MagicObject.js +77 -0
  52. package/dist/helpers/MagicObject.js.map +1 -0
  53. package/dist/helpers/ObjectsMap.d.ts +28 -0
  54. package/dist/helpers/ObjectsMap.js +86 -0
  55. package/dist/helpers/ObjectsMap.js.map +1 -0
  56. package/dist/helpers/PromisedValue.d.ts +32 -0
  57. package/dist/helpers/PromisedValue.js +68 -0
  58. package/dist/helpers/PromisedValue.js.map +1 -0
  59. package/dist/helpers/Semaphore.d.ts +14 -0
  60. package/dist/helpers/Semaphore.js +45 -0
  61. package/dist/helpers/Semaphore.js.map +1 -0
  62. package/dist/helpers/Storage.d.ts +17 -0
  63. package/dist/helpers/Storage.js +39 -0
  64. package/dist/helpers/Storage.js.map +1 -0
  65. package/dist/helpers/array_helpers.d.ts +57 -0
  66. package/dist/helpers/array_helpers.js +208 -0
  67. package/dist/helpers/array_helpers.js.map +1 -0
  68. package/dist/helpers/boolean_helpers.d.ts +4 -0
  69. package/dist/helpers/boolean_helpers.js +10 -0
  70. package/dist/helpers/boolean_helpers.js.map +1 -0
  71. package/dist/helpers/caching_helpers.d.ts +17 -0
  72. package/dist/helpers/caching_helpers.js +55 -0
  73. package/dist/helpers/caching_helpers.js.map +1 -0
  74. package/dist/helpers/chaos_helpers.d.ts +6 -0
  75. package/dist/helpers/chaos_helpers.js +11 -0
  76. package/dist/helpers/chaos_helpers.js.map +1 -0
  77. package/dist/helpers/date_helpers.d.ts +4 -0
  78. package/dist/helpers/date_helpers.js +15 -0
  79. package/dist/helpers/date_helpers.js.map +1 -0
  80. package/dist/helpers/deprecated.d.ts +24 -0
  81. package/dist/helpers/env.d.ts +9 -0
  82. package/dist/helpers/env.js +42 -0
  83. package/dist/helpers/env.js.map +1 -0
  84. package/dist/helpers/error_helpers.d.ts +12 -0
  85. package/dist/helpers/error_helpers.js +20 -0
  86. package/dist/helpers/error_helpers.js.map +1 -0
  87. package/dist/helpers/escape_helpers.d.ts +5 -0
  88. package/dist/helpers/escape_helpers.js +11 -0
  89. package/dist/helpers/escape_helpers.js.map +1 -0
  90. package/dist/helpers/file_helpers.d.ts +4 -0
  91. package/dist/helpers/file_helpers.js +16 -0
  92. package/dist/helpers/file_helpers.js.map +1 -0
  93. package/dist/helpers/format_helpers.d.ts +7 -0
  94. package/dist/helpers/format_helpers.js +28 -0
  95. package/dist/helpers/format_helpers.js.map +1 -0
  96. package/dist/helpers/hash_helpers.d.ts +4 -0
  97. package/dist/helpers/hash_helpers.js +156 -0
  98. package/dist/helpers/hash_helpers.js.map +1 -0
  99. package/dist/helpers/identity_helpers.d.ts +5 -0
  100. package/dist/helpers/identity_helpers.js +15 -0
  101. package/dist/helpers/identity_helpers.js.map +1 -0
  102. package/dist/helpers/location_helpers.d.ts +8 -0
  103. package/dist/helpers/location_helpers.js +37 -0
  104. package/dist/helpers/location_helpers.js.map +1 -0
  105. package/dist/helpers/logical_helpers.d.ts +4 -0
  106. package/dist/helpers/logical_helpers.js +13 -0
  107. package/dist/helpers/logical_helpers.js.map +1 -0
  108. package/dist/helpers/math_helpers.d.ts +5 -0
  109. package/dist/helpers/math_helpers.js +12 -0
  110. package/dist/helpers/math_helpers.js.map +1 -0
  111. package/dist/helpers/mixin_helpers.d.ts +20 -0
  112. package/dist/helpers/mixin_helpers.js +43 -0
  113. package/dist/helpers/mixin_helpers.js.map +1 -0
  114. package/dist/helpers/network_helpers.d.ts +4 -0
  115. package/dist/helpers/network_helpers.js +8 -0
  116. package/dist/helpers/network_helpers.js.map +1 -0
  117. package/dist/helpers/object_helpers.d.ts +58 -0
  118. package/dist/helpers/object_helpers.js +179 -0
  119. package/dist/helpers/object_helpers.js.map +1 -0
  120. package/dist/helpers/promise_helpers.d.ts +10 -0
  121. package/dist/helpers/promise_helpers.js +25 -0
  122. package/dist/helpers/promise_helpers.js.map +1 -0
  123. package/dist/helpers/replacements-scope.js +21 -0
  124. package/dist/helpers/replacements-scope.js.map +1 -0
  125. package/dist/helpers/replacements.d.ts +5 -0
  126. package/dist/helpers/replacements.js +34 -0
  127. package/dist/helpers/replacements.js.map +1 -0
  128. package/dist/helpers/runtime_helpers.d.ts +4 -0
  129. package/dist/helpers/runtime_helpers.js +8 -0
  130. package/dist/helpers/runtime_helpers.js.map +1 -0
  131. package/dist/helpers/string_helpers.d.ts +17 -0
  132. package/dist/helpers/string_helpers.js +42 -0
  133. package/dist/helpers/string_helpers.js.map +1 -0
  134. package/dist/helpers/time_constants.d.ts +15 -0
  135. package/dist/helpers/time_constants.js +17 -0
  136. package/dist/helpers/time_constants.js.map +1 -0
  137. package/dist/helpers/time_helpers.d.ts +24 -0
  138. package/dist/helpers/time_helpers.js +57 -0
  139. package/dist/helpers/time_helpers.js.map +1 -0
  140. package/dist/helpers/url_helpers.d.ts +24 -0
  141. package/dist/helpers/url_helpers.js +90 -0
  142. package/dist/helpers/url_helpers.js.map +1 -0
  143. package/dist/helpers/value_helpers.d.ts +7 -0
  144. package/dist/helpers/value_helpers.js +11 -0
  145. package/dist/helpers/value_helpers.js.map +1 -0
  146. package/dist/index.d.ts +53 -0
  147. package/dist/index.js +49 -0
  148. package/dist/types/arrays.d.ts +12 -0
  149. package/dist/types/classes.d.ts +12 -0
  150. package/dist/types/helpers.d.ts +36 -0
  151. package/dist/types/helpers.js +6 -0
  152. package/dist/types/helpers.js.map +1 -0
  153. package/dist/types/objects.d.ts +8 -0
  154. package/package.json +38 -52
  155. package/src/fluent/FluentArray.test.ts +2 -9
  156. package/src/fluent/FluentArray.ts +6 -5
  157. package/src/fluent/FluentObject.ts +2 -4
  158. package/src/fluent/FluentString.test.ts +1 -8
  159. package/src/fluent/FluentString.ts +0 -2
  160. package/src/fluent/facade.test.ts +16 -4
  161. package/src/fluent/facade.ts +6 -4
  162. package/src/fluent/index.test.ts +3 -8
  163. package/src/fluent/index.ts +3 -3
  164. package/src/fluent/invade.test.ts +1 -5
  165. package/src/fluent/invade.ts +1 -1
  166. package/src/fluent/lazyFacade.test.ts +191 -0
  167. package/src/fluent/lazyFacade.ts +115 -0
  168. package/src/fluent/required.test.ts +1 -5
  169. package/src/fluent/required.ts +2 -4
  170. package/src/fluent/tap.test.ts +2 -9
  171. package/src/fluent/tap.ts +4 -3
  172. package/src/fluent/value.test.ts +1 -4
  173. package/src/fluent/when.test.ts +1 -6
  174. package/src/fluent/when.ts +2 -2
  175. package/src/helpers/InMemoryStorageService.ts +13 -0
  176. package/src/helpers/IndexedDBMap.ts +0 -2
  177. package/src/helpers/JSError.test.ts +1 -6
  178. package/src/helpers/JSError.ts +0 -2
  179. package/src/helpers/ListenersManager.test.ts +3 -7
  180. package/src/helpers/ListenersManager.ts +2 -4
  181. package/src/helpers/MagicObject.test.ts +14 -8
  182. package/src/helpers/MagicObject.ts +12 -3
  183. package/src/helpers/ObjectsMap.test.ts +1 -3
  184. package/src/helpers/ObjectsMap.ts +1 -5
  185. package/src/helpers/PromisedValue.test.ts +1 -3
  186. package/src/helpers/PromisedValue.ts +5 -6
  187. package/src/helpers/Semaphore.test.ts +6 -10
  188. package/src/helpers/Semaphore.ts +0 -2
  189. package/src/helpers/Storage.test.ts +47 -0
  190. package/src/helpers/Storage.ts +11 -19
  191. package/src/helpers/array_helpers.test.ts +127 -38
  192. package/src/helpers/array_helpers.ts +137 -78
  193. package/src/helpers/caching_helpers.test.ts +1 -3
  194. package/src/helpers/caching_helpers.ts +2 -2
  195. package/src/helpers/date_helpers.test.ts +1 -4
  196. package/src/helpers/date_helpers.ts +1 -1
  197. package/src/helpers/env.ts +5 -1
  198. package/src/helpers/error_helpers.test.ts +9 -8
  199. package/src/helpers/error_helpers.ts +11 -11
  200. package/src/helpers/format_helpers.test.ts +2 -4
  201. package/src/helpers/hash_helpers.test.ts +1 -4
  202. package/src/helpers/hash_helpers.ts +3 -3
  203. package/src/helpers/identity_helpers.test.ts +1 -3
  204. package/src/helpers/index.ts +3 -0
  205. package/src/helpers/logical_helpers.test.ts +1 -3
  206. package/src/helpers/logical_helpers.ts +1 -1
  207. package/src/helpers/math_helpers.test.ts +1 -3
  208. package/src/helpers/mixin_helpers.stubs.ts +1 -13
  209. package/src/helpers/mixin_helpers.test.ts +2 -7
  210. package/src/helpers/mixin_helpers.ts +6 -9
  211. package/src/helpers/object_helpers.test.ts +32 -19
  212. package/src/helpers/object_helpers.ts +43 -21
  213. package/src/helpers/promise_helpers.test.ts +1 -4
  214. package/src/helpers/promise_helpers.ts +9 -9
  215. package/src/helpers/replacements-scope.ts +15 -0
  216. package/src/helpers/replacements.test.ts +23 -0
  217. package/src/helpers/replacements.ts +52 -0
  218. package/src/helpers/runtime_helpers.ts +3 -3
  219. package/src/helpers/string_helpers.test.ts +3 -5
  220. package/src/helpers/time_constants.ts +13 -0
  221. package/src/helpers/time_helpers.ts +35 -0
  222. package/src/helpers/url_helpers.test.ts +12 -4
  223. package/src/helpers/url_helpers.ts +9 -15
  224. package/src/helpers/value_helpers.test.ts +12 -0
  225. package/src/helpers/value_helpers.ts +6 -0
  226. package/src/types/arrays.test.ts +1 -4
  227. package/src/types/classes.ts +1 -0
  228. package/src/types/helpers.test.ts +1 -6
  229. package/src/types/helpers.ts +6 -5
  230. package/src/types/objects.test.ts +1 -4
  231. package/src/types/objects.ts +3 -3
  232. package/dist/noeldemartin-utils.d.ts +0 -901
  233. package/dist/noeldemartin-utils.js +0 -1473
  234. package/dist/noeldemartin-utils.js.map +0 -1
@@ -1,11 +1,9 @@
1
- import { describe, expect, it } from 'vitest';
2
-
3
1
  import { range } from '@noeldemartin/utils/helpers/array_helpers';
2
+ import { describe, expect, it } from 'vite-plus/test';
4
3
 
5
4
  import { asyncFirst } from './promise_helpers';
6
5
 
7
6
  describe('Promise helpers', () => {
8
-
9
7
  it('gets first async result', async () => {
10
8
  // Arrange
11
9
  let calls = 0;
@@ -18,5 +16,4 @@ describe('Promise helpers', () => {
18
16
  expect(calls).toEqual(4);
19
17
  expect(result).toEqual(3);
20
18
  });
21
-
22
19
  });
@@ -22,16 +22,16 @@ export function isPromise(p: unknown): p is Promise<unknown> {
22
22
  }
23
23
 
24
24
  export function silenced<Args extends ClosureArgs, Result>(
25
- operation: Closure<Args, Promise<Result>>
25
+ operation: Closure<Args, Promise<Result>>,
26
26
  ): Closure<Args, Promise<Result | null>>;
27
27
  export function silenced<Args extends ClosureArgs, Result, Default>(
28
28
  operation: Closure<Args, Promise<Result>>,
29
- defaultValue: Default
29
+ defaultValue: Default,
30
30
  ): Closure<Args, Promise<Result | Default>>;
31
31
  export function silenced<Result>(promise: Promise<Result>): Promise<Result | null>;
32
32
  export function silenced<Args extends ClosureArgs, Result, Default>(
33
33
  operation: Closure<Args, Promise<Result>>,
34
- defaultValue: Default
34
+ defaultValue: Default,
35
35
  ): Closure<Args, Promise<Result | Default>>;
36
36
  export function silenced<Args extends ClosureArgs, Result, Default>(
37
37
  operationOrPromise: Closure<Args, Promise<Result>> | Promise<Result>,
@@ -40,10 +40,10 @@ export function silenced<Args extends ClosureArgs, Result, Default>(
40
40
  return typeof operationOrPromise !== 'function'
41
41
  ? operationOrPromise.catch(() => defaultValue)
42
42
  : async (...args: Args) => {
43
- try {
44
- return await operationOrPromise(...args);
45
- } catch (error) {
46
- return defaultValue ?? null;
47
- }
48
- };
43
+ try {
44
+ return await operationOrPromise(...args);
45
+ } catch {
46
+ return defaultValue ?? null;
47
+ }
48
+ };
49
49
  }
@@ -0,0 +1,15 @@
1
+ import { uuid as generateUUID } from '@noeldemartin/utils/helpers/identity_helpers';
2
+
3
+ export interface ReplacementsScope {}
4
+
5
+ export function date(description: string): string {
6
+ return new Date(Date.parse(description)).toISOString();
7
+ }
8
+
9
+ export function now(): string {
10
+ return new Date().toISOString();
11
+ }
12
+
13
+ export function uuid(this: ReplacementsScope): string {
14
+ return generateUUID();
15
+ }
@@ -0,0 +1,23 @@
1
+ import { describe, expect, it } from 'vite-plus/test';
2
+
3
+ import { applyReplacements } from './replacements';
4
+
5
+ describe('Replacements', () => {
6
+ it('Applies replacements', () => {
7
+ expect(applyReplacements('Foo {{bar}}', { bar: 'Bar' })).toEqual('Foo Bar');
8
+ expect(applyReplacements('Foo {{ bar }}', { bar: 'Bar' })).toEqual('Foo Bar');
9
+ expect(applyReplacements('{{ one }}{{ o }}{{ one }}', { one: '1', o: '0' })).toEqual('101');
10
+ });
11
+
12
+ it('Evaluates replacements JavaScript', () => {
13
+ expect(applyReplacements("Foo {{ bar || 'Fallback' }}", { bar: 'Bar' })).toEqual('Foo Bar');
14
+ expect(applyReplacements("Foo {{ bar || 'Bar' }}")).toEqual('Foo Bar');
15
+ expect(applyReplacements('Foo {{ bar || uuid() }}')).toMatch(
16
+ /Foo [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/,
17
+ );
18
+ expect(applyReplacements('Now: {{ now() }}').replace(/\.\d{3}Z/, '.000Z')).toEqual(
19
+ `Now: ${new Date().toISOString().replace(/\.\d{3}Z/, '.000Z')}`,
20
+ );
21
+ expect(applyReplacements("Date: {{ date('2024-03-03') }}")).toEqual('Date: 2024-03-03T00:00:00.000Z');
22
+ });
23
+ });
@@ -0,0 +1,52 @@
1
+ import { toString } from '@noeldemartin/utils/helpers/object_helpers';
2
+ import { stringMatch, stringMatchAll } from '@noeldemartin/utils/helpers/string_helpers';
3
+
4
+ import * as replacementsScope from './replacements-scope';
5
+ import type { ReplacementsScope } from './replacements-scope';
6
+
7
+ function renderReplacement(script: string, scope: ReplacementsScope): string {
8
+ try {
9
+ // oxlint-disable-next-line typescript/no-implied-eval
10
+ return Function(`with (this) { return (${script}).toString() }`).bind({
11
+ ...replacementsScope,
12
+ ...scope,
13
+ })();
14
+ } catch (error) {
15
+ const undefinedVariable = stringMatch<2>(toString(error), /([^\s]+) is not defined/)?.[1];
16
+
17
+ if (!undefinedVariable) {
18
+ throw error;
19
+ }
20
+
21
+ return renderReplacement(script, {
22
+ ...scope,
23
+ [undefinedVariable]: undefined,
24
+ });
25
+ }
26
+ }
27
+
28
+ const REPLACEMENTS_REGEX = /{{([^}]+)}}/gm;
29
+
30
+ function reduce<TItem, TResult>(
31
+ items: IterableIterator<TItem>,
32
+ initial: TResult,
33
+ callback: (result: TResult, item: TItem) => TResult,
34
+ ) {
35
+ let result = initial;
36
+
37
+ for (const item of items) {
38
+ result = callback(result, item);
39
+ }
40
+
41
+ return result;
42
+ }
43
+
44
+ export type Replacements = Record<string, unknown>;
45
+
46
+ export function applyReplacements(text: string, replacements: Replacements = {}): string {
47
+ const matches = stringMatchAll<2>(text, REPLACEMENTS_REGEX);
48
+
49
+ return reduce(matches, text, (renderedText, match) => {
50
+ return renderedText.replace(match[0], renderReplacement(match[1], replacements));
51
+ });
52
+ }
@@ -1,8 +1,8 @@
1
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1
+ // oxlint-disable-next-line typescript/no-explicit-any
2
2
  export function runtimeGlobal<T = any>(): T {
3
3
  return typeof global !== 'undefined'
4
4
  ? (global as T)
5
5
  : typeof globalThis !== 'undefined'
6
- ? (globalThis as T)
7
- : (window as T);
6
+ ? (globalThis as T)
7
+ : (window as T);
8
8
  }
@@ -1,4 +1,4 @@
1
- import { describe, expect, it } from 'vitest';
1
+ import { describe, expect, it } from 'vite-plus/test';
2
2
 
3
3
  import {
4
4
  stringExcerpt,
@@ -10,7 +10,6 @@ import {
10
10
  } from './string_helpers';
11
11
 
12
12
  describe('String helpers', () => {
13
-
14
13
  it('reverses string characters', () => {
15
14
  expect(stringReverse('foobar')).toEqual('raboof');
16
15
  });
@@ -24,8 +23,8 @@ describe('String helpers', () => {
24
23
  it('converts strings to slug', () => {
25
24
  expect(stringToSlug('Sin perdón')).toEqual('sin-perdon');
26
25
  expect(stringToSlug('Mañana')).toEqual('manana');
27
- expect(stringToSlug('It\'s always sunny in philadelphia')).toEqual('its-always-sunny-in-philadelphia');
28
- expect(stringToSlug('It\'s always sunny in philadelphia ')).toEqual('its-always-sunny-in-philadelphia');
26
+ expect(stringToSlug("It's always sunny in philadelphia")).toEqual('its-always-sunny-in-philadelphia');
27
+ expect(stringToSlug("It's always sunny in philadelphia ")).toEqual('its-always-sunny-in-philadelphia');
29
28
  expect(stringToSlug('already-a-slug')).toEqual('already-a-slug');
30
29
  expect(stringToSlug('Not clean: [/\\?!] end')).toEqual('not-clean-end');
31
30
  expect(stringToSlug('土豆')).toEqual('土豆');
@@ -55,5 +54,4 @@ describe('String helpers', () => {
55
54
  'Your external self will seldom surpa [...]',
56
55
  );
57
56
  });
58
-
59
57
  });
@@ -0,0 +1,13 @@
1
+ export const MINUTE_MILLISECONDS = 60 * 1000;
2
+ export const HOUR_MILLISECONDS = 60 * MINUTE_MILLISECONDS;
3
+ export const DAY_MILLISECONDS = 24 * HOUR_MILLISECONDS;
4
+ export const WEEK_MILLISECONDS = 7 * DAY_MILLISECONDS;
5
+ export const MONTH_MILLISECONDS = 30 * DAY_MILLISECONDS;
6
+ export const YEAR_MILLISECONDS = 365 * DAY_MILLISECONDS;
7
+
8
+ export const MINUTE_SECONDS = 60;
9
+ export const HOUR_SECONDS = 60 * MINUTE_SECONDS;
10
+ export const DAY_SECONDS = 24 * HOUR_SECONDS;
11
+ export const WEEK_SECONDS = 7 * DAY_SECONDS;
12
+ export const MONTH_SECONDS = 30 * DAY_SECONDS;
13
+ export const YEAR_SECONDS = 365 * DAY_SECONDS;
@@ -23,6 +23,12 @@ export function after(msOrOptions: number | AfterOptions = {}): Promise<void> {
23
23
  return new Promise((resolve) => setTimeout(resolve, time));
24
24
  }
25
25
 
26
+ export function sleep(ms: number): Promise<void>;
27
+ export function sleep(options?: AfterOptions): Promise<void>;
28
+ export function sleep(msOrOptions: number | AfterOptions = {}): Promise<void> {
29
+ return after(msOrOptions as number);
30
+ }
31
+
26
32
  export function afterAnimationFrame(): Promise<void> {
27
33
  return new Promise((resolve) => requestAnimationFrame(() => resolve()));
28
34
  }
@@ -43,6 +49,35 @@ export function debounce<Args extends ClosureArgs>(
43
49
  return debouncedCallback;
44
50
  }
45
51
 
52
+ export function throttle<Args extends ClosureArgs>(
53
+ callback: (...args: Args) => unknown,
54
+ delay: number = 100,
55
+ ): (...args: Args) => void {
56
+ let trailingArgs: Args | null = null;
57
+ let timeout: ReturnType<typeof setTimeout> | null = null;
58
+
59
+ const throttledCallback = (...args: Args) => {
60
+ if (timeout) {
61
+ trailingArgs = args;
62
+
63
+ return;
64
+ }
65
+
66
+ callback(...args);
67
+
68
+ timeout = setTimeout(() => {
69
+ const nextArgs = trailingArgs;
70
+
71
+ trailingArgs = null;
72
+ timeout = null;
73
+
74
+ nextArgs && throttledCallback(...nextArgs);
75
+ }, delay);
76
+ };
77
+
78
+ return throttledCallback;
79
+ }
80
+
46
81
  export async function forever(): Promise<void> {
47
82
  await new Promise(() => {
48
83
  // Nothing to do here.
@@ -1,6 +1,6 @@
1
- import { describe, expect, it } from 'vitest';
2
-
3
1
  import { arrayChunk } from '@noeldemartin/utils/helpers/array_helpers';
2
+ import { describe, expect, it } from 'vite-plus/test';
3
+
4
4
  import {
5
5
  requireUrlDirectoryName,
6
6
  urlClean,
@@ -14,7 +14,6 @@ import {
14
14
  } from './url_helpers';
15
15
 
16
16
  describe('Url helper', () => {
17
-
18
17
  it('uses root when resolving absolute paths', () => {
19
18
  expect(urlResolve('http://example.com/something/else', '/foobar')).toEqual('http://example.com/foobar');
20
19
  });
@@ -25,6 +24,16 @@ describe('Url helper', () => {
25
24
  );
26
25
  });
27
26
 
27
+ it('resolves relative paths', () => {
28
+ expect(urlResolve('http://example.com/something/else', 'foobar')).toEqual(
29
+ 'http://example.com/something/foobar',
30
+ );
31
+ expect(urlResolve('http://example.com/something/else', './foobar')).toEqual(
32
+ 'http://example.com/something/foobar',
33
+ );
34
+ expect(urlResolve('http://example.com/something/else', '../foobar')).toEqual('http://example.com/foobar');
35
+ });
36
+
28
37
  it('splits arrays into chunks', () => {
29
38
  expect(arrayChunk([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]);
30
39
  expect(arrayChunk([1, 2, 3, 4, 5, 6], 2)).toEqual([
@@ -115,5 +124,4 @@ describe('Url helper', () => {
115
124
  expect(urlFileName('https://example.com/foobar#it')).toEqual('foobar');
116
125
  expect(urlFileName('https://example.com/foobar?ignore')).toEqual('foobar');
117
126
  });
118
-
119
127
  });
@@ -22,20 +22,14 @@ export function requireUrlParse(url: string): UrlParts {
22
22
  }
23
23
 
24
24
  export function urlResolve(...parts: string[]): string {
25
- let url = parts.shift() as string;
26
-
27
- while (parts.length > 0) {
28
- const fragment = parts.shift() as string;
29
-
30
- if (fragment.startsWith('/')) {
31
- url = urlRoot(url) + fragment;
32
- } else if (fragment.match(/^[\w-]+:\/\//)) {
33
- url = fragment;
34
- } else if (url.endsWith('/')) {
35
- url += fragment;
36
- } else {
37
- url += '/' + fragment;
38
- }
25
+ if (parts.length === 0) {
26
+ return '';
27
+ }
28
+
29
+ let url = parts[0];
30
+
31
+ for (const part of parts.slice(1)) {
32
+ url = new URL(part, url).toString();
39
33
  }
40
34
 
41
35
  return url;
@@ -132,7 +126,7 @@ export function urlRoute(url: string): string {
132
126
  export function validUrl(url: string): string | null {
133
127
  try {
134
128
  return new URL(url.trim()).toString();
135
- } catch (error) {
129
+ } catch {
136
130
  return null;
137
131
  }
138
132
  }
@@ -0,0 +1,12 @@
1
+ import { isTruthy } from '@noeldemartin/utils/helpers/value_helpers';
2
+ import { describe, expect, expectTypeOf, it } from 'vite-plus/test';
3
+
4
+ describe('Value helpers', () => {
5
+ it('filters truthy values', () => {
6
+ const items = [1, 2, 3, null, 0, '', 'foo'];
7
+ const filteredItems = items.filter(isTruthy);
8
+
9
+ expect(filteredItems).toEqual([1, 2, 3, 'foo']);
10
+ expectTypeOf(filteredItems).toEqualTypeOf<(number | string)[]>();
11
+ });
12
+ });
@@ -1,5 +1,11 @@
1
+ import type { Truthy } from '@noeldemartin/utils/types';
2
+
1
3
  export type EvaluatesTo<T> = T | (() => T);
2
4
 
3
5
  export function evaluate<T>(value: EvaluatesTo<T>): T {
4
6
  return typeof value === 'function' ? (value as () => T)() : value;
5
7
  }
8
+
9
+ export function isTruthy<T>(value: T): value is Truthy<T> {
10
+ return Boolean(value);
11
+ }
@@ -1,13 +1,11 @@
1
- import { describe, it } from 'vitest';
2
1
  import { tt } from '@noeldemartin/testing';
3
2
  import type { Expect } from '@noeldemartin/testing';
4
-
5
3
  import type { Equals } from '@noeldemartin/utils/types';
4
+ import { describe, it } from 'vite-plus/test';
6
5
 
7
6
  import type { ArrayItem, Tuple } from './arrays';
8
7
 
9
8
  describe('Array types', () => {
10
-
11
9
  it(
12
10
  'Has correct types',
13
11
  tt<
@@ -17,5 +15,4 @@ describe('Array types', () => {
17
15
  | true
18
16
  >(),
19
17
  );
20
-
21
18
  });
@@ -1,5 +1,6 @@
1
1
  import type { ClosureArgs } from './helpers';
2
2
 
3
+ // oxlint-disable-next-line typescript/no-wrapper-object-types
3
4
  export interface Constructor<T = Object> {
4
5
  new (...args: any[]): T;
5
6
  }
@@ -1,8 +1,7 @@
1
- import { describe, it } from 'vitest';
2
1
  import { tt } from '@noeldemartin/testing';
3
2
  import type { Expect } from '@noeldemartin/testing';
4
-
5
3
  import type { Equals } from '@noeldemartin/utils/types';
4
+ import { describe, it } from 'vite-plus/test';
6
5
 
7
6
  import type { KeyOf, NullableOptional, NullablePartial } from './helpers';
8
7
 
@@ -13,14 +12,11 @@ interface User {
13
12
  }
14
13
 
15
14
  describe('Type helpers', () => {
16
-
17
15
  it(
18
16
  'has correct types',
19
17
  tt<
20
18
  | Expect<Equals<KeyOf<User, string | number>, 'id' | 'name'>>
21
19
  | Expect<Equals<NullablePartial<{ foo: string }>, { foo?: string | null | undefined }>>
22
-
23
- // eslint-disable-next-line max-len
24
20
  | Expect<
25
21
  Equals<
26
22
  NullableOptional<{ foo: string; bar?: string }>,
@@ -30,5 +26,4 @@ describe('Type helpers', () => {
30
26
  | true
31
27
  >(),
32
28
  );
33
-
34
29
  });
@@ -1,11 +1,12 @@
1
1
  export const __brand = Symbol('__brand');
2
2
  export type ClassInstance<Class> = Class extends { new (...args: any[]): infer T } ? T : never;
3
3
  export type Closure<Args extends ClosureArgs = ClosureArgs, Result = unknown> = (...args: Args) => Result;
4
- export type ClosureArgs = any[]; // eslint-disable-line @typescript-eslint/no-explicit-any
5
- export type ClosureResult = any; // eslint-disable-line @typescript-eslint/no-explicit-any
4
+ export type ClosureArgs = any[]; // oxlint-disable-line typescript/no-explicit-any
5
+ export type ClosureResult = any; // oxlint-disable-line typescript/no-explicit-any
6
6
  export type Branded<T, Brand> = T & { [__brand]: Brand };
7
7
  export type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
8
- export type Falsy = null | undefined | false | 0;
8
+ export type Falsy = '' | null | undefined | false | 0;
9
+ export type Truthy<T> = Exclude<T, Falsy>;
9
10
  export type GetObjectMethods<T> = {
10
11
  [K in keyof T]: T[K] extends (...args: ClosureArgs) => ClosureResult ? K : never;
11
12
  }[keyof T];
@@ -31,7 +32,7 @@ export type IsAny<T> = { __: 'never' } extends T ? true : false;
31
32
  export type IsPlainObject<T> = T extends object
32
33
  ? T extends Function
33
34
  ? false
34
- : T extends any[] // eslint-disable-line @typescript-eslint/no-explicit-any
35
+ : T extends any[] // oxlint-disable-line typescript/no-explicit-any
35
36
  ? false
36
37
  : T extends Date
37
38
  ? false
@@ -41,4 +42,4 @@ export type IsPlainObject<T> = T extends object
41
42
  : false;
42
43
 
43
44
  // Workaround for https://github.com/typescript-eslint/typescript-eslint/issues/3573
44
- export type Use<T> = {}; // eslint-disable-line @typescript-eslint/no-unused-vars
45
+ export type Use<T> = {}; // oxlint-disable-line typescript/no-unused-vars
@@ -1,9 +1,8 @@
1
- import { describe, it } from 'vitest';
2
1
  import { tt } from '@noeldemartin/testing';
3
2
  import type { Expect } from '@noeldemartin/testing';
4
-
5
3
  import { arraySorted } from '@noeldemartin/utils/helpers';
6
4
  import type { Equals } from '@noeldemartin/utils/types';
5
+ import { describe, it } from 'vite-plus/test';
7
6
 
8
7
  import type { DeepKeyOf, DeepValue } from './objects';
9
8
 
@@ -33,7 +32,6 @@ const key = 'author' as DeepKeyOf<Post>;
33
32
  const sortedFields = arraySorted(posts, key);
34
33
 
35
34
  describe('Object types', () => {
36
-
37
35
  it(
38
36
  'Has correct types',
39
37
  tt<
@@ -77,5 +75,4 @@ describe('Object types', () => {
77
75
  | true
78
76
  >(),
79
77
  );
80
-
81
78
  });
@@ -12,7 +12,6 @@ export type DeepRequired<T> = T extends object
12
12
  }
13
13
  : T;
14
14
 
15
- /* eslint-disable max-len */
16
15
  export type DeepKeyOf<T, TDepth extends number = 5, TRequired extends DeepRequired<T> = DeepRequired<T>> = NonNullable<
17
16
  TRequired extends object
18
17
  ? {
@@ -35,7 +34,9 @@ export type DeepKeyOf<T, TDepth extends number = 5, TRequired extends DeepRequir
35
34
  : TRequired[K][KK] extends object
36
35
  ?
37
36
  | {
38
- [KKK in keyof TRequired[K][KK]]: TRequired[K][KK][KKK] extends Function
37
+ [
38
+ KKK in keyof TRequired[K][KK]
39
+ ]: TRequired[K][KK][KKK] extends Function
39
40
  ? never
40
41
  : `${Exclude<K, symbol>}.${Exclude<KK, symbol>}.${Exclude<KKK, symbol>}`;
41
42
  }[keyof TRequired[K][KK]]
@@ -47,7 +48,6 @@ export type DeepKeyOf<T, TDepth extends number = 5, TRequired extends DeepRequir
47
48
  }[keyof TRequired]
48
49
  : never
49
50
  >;
50
- /* eslint-enable max-len */
51
51
 
52
52
  export type DeepValue<T extends object, K> = K extends `${infer Key}.${infer Rest}`
53
53
  ? Key extends keyof T