@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,14 +1,12 @@
1
- import { describe, expect, 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, expect, it } from 'vite-plus/test';
6
5
 
7
6
  import FluentStringDefinition from './FluentString';
8
7
  import type { FluentString, FluentStringInstance } from './FluentString';
9
8
 
10
9
  describe('FluentString', () => {
11
-
12
10
  it('delegates to helper methods', () => {
13
11
  const fluentString = FluentStringDefinition.create('foo bar');
14
12
 
@@ -42,12 +40,10 @@ describe('FluentString', () => {
42
40
  expect(superFluentString.toUpperCase().toString()).toBe('nope');
43
41
  expect(superFluentString.powerUp().toString()).toBe('super-foobar');
44
42
  });
45
-
46
43
  });
47
44
 
48
45
  // TODO rewrite type assertions just with type declarations
49
46
  class SuperFluentString extends FluentStringDefinition {
50
-
51
47
  public toUpperCase(): this {
52
48
  // TODO this should be able to call super.toUpperCase()
53
49
 
@@ -57,14 +53,12 @@ class SuperFluentString extends FluentStringDefinition {
57
53
  public powerUp(): this {
58
54
  return this.create('super-' + this.value);
59
55
  }
60
-
61
56
  }
62
57
 
63
58
  const fluentString = FluentStringDefinition.create();
64
59
  const superFluentString = SuperFluentString.create();
65
60
 
66
61
  describe('FluentString types', () => {
67
-
68
62
  it(
69
63
  'has correct types',
70
64
  tt<
@@ -74,5 +68,4 @@ describe('FluentString types', () => {
74
68
  | true
75
69
  >(),
76
70
  );
77
-
78
71
  });
@@ -28,7 +28,6 @@ export type FluentStringInstance<FluentClass> = FluentInstance<
28
28
  export type FluentString = FluentStringInstance<FluentStringDefinition>;
29
29
 
30
30
  class FluentStringDefinition extends FluentObjectDefinition<string> {
31
-
32
31
  public static create<T>(this: T, value: string = ''): FluentStringInstance<ClassInstance<T>> {
33
32
  const { prototype } = this as unknown as {
34
33
  prototype: { create(value: string): FluentStringInstance<ClassInstance<T>> };
@@ -44,7 +43,6 @@ class FluentStringDefinition extends FluentObjectDefinition<string> {
44
43
  protected isPrimitive(value: unknown): value is string {
45
44
  return typeof value === 'string';
46
45
  }
47
-
48
46
  }
49
47
 
50
48
  addHelperMethodsToPrototype(FluentStringDefinition, fluentStringHelpers);
@@ -1,19 +1,16 @@
1
- import { describe, expect, it } from 'vitest';
1
+ import { describe, expect, it } from 'vite-plus/test';
2
2
 
3
3
  import { facade } from './facade';
4
4
 
5
5
  describe('Facades', () => {
6
-
7
6
  it('reset instances', () => {
8
7
  // Arrange
9
8
  class CounterService {
10
-
11
9
  public count: number = 0;
12
10
 
13
11
  public add(): void {
14
12
  this.count++;
15
13
  }
16
-
17
14
  }
18
15
 
19
16
  const counter = facade(CounterService);
@@ -27,4 +24,19 @@ describe('Facades', () => {
27
24
  expect(counter.count).toEqual(0);
28
25
  });
29
26
 
27
+ it('sets properties', () => {
28
+ // Arrange
29
+ class CounterService {
30
+ public count: number = 0;
31
+ }
32
+
33
+ const counter = facade(CounterService);
34
+
35
+ // Act
36
+ counter.count = 3;
37
+
38
+ // Assert
39
+ expect(counter.count).toEqual(3);
40
+ expect(counter.requireInstance().count).toEqual(3);
41
+ });
30
42
  });
@@ -1,6 +1,7 @@
1
1
  import type { Constructor } from '@noeldemartin/utils/types/classes';
2
2
 
3
3
  const extensions: FacadeExtension[] = [];
4
+ const facadeMarker = Symbol('facade');
4
5
 
5
6
  export type FacadeExtension = (facadeInstance: FacadeMethods) => void;
6
7
 
@@ -21,7 +22,7 @@ export function facade<TInstance extends object>(
21
22
  function newInstance(): TInstance {
22
23
  try {
23
24
  return (defaultClassOrFactory as () => TInstance)();
24
- } catch (error) {
25
+ } catch {
25
26
  return new (defaultClassOrFactory as Constructor<TInstance>)();
26
27
  }
27
28
  }
@@ -32,7 +33,8 @@ export function facade<TInstance extends object>(
32
33
  return _instance;
33
34
  }
34
35
 
35
- const facadeInstance: FacadeMethods<TInstance> = {
36
+ const facadeInstance: FacadeMethods<TInstance> & { [facadeMarker]: true } = {
37
+ [facadeMarker]: true,
36
38
  setInstance,
37
39
  newInstance,
38
40
  requireInstance: () => instance ?? setInstance(newInstance()),
@@ -50,7 +52,7 @@ export function facade<TInstance extends object>(
50
52
 
51
53
  return Reflect.get(t, property, receiver);
52
54
  },
53
- set: (_, property, receiver) => Reflect.set(facadeInstance.requireInstance(), property, receiver),
55
+ set: (_, property, value) => Reflect.set(facadeInstance.requireInstance(), property, value),
54
56
  }) as unknown as Facade<TInstance>;
55
57
  }
56
58
 
@@ -59,7 +61,7 @@ export function extendFacadeMethods(extension: FacadeExtension): void {
59
61
  }
60
62
 
61
63
  export function isFacade(value: unknown): value is Facade {
62
- return typeof value === 'object' && value !== null && 'requireInstance' in value;
64
+ return typeof value === 'object' && value !== null && facadeMarker in value;
63
65
  }
64
66
 
65
67
  export function getFacadeInstance<T extends object>(value: Facade<T>): T;
@@ -1,26 +1,22 @@
1
- import { describe, expect, 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, expect, it } from 'vite-plus/test';
6
5
 
7
- import { arr, fluent, pull, str } from './index';
8
6
  import FluentArrayDefinition from './FluentArray';
9
- import FluentStringDefinition from './FluentString';
10
7
  import type { FluentArray } from './FluentArray';
8
+ import FluentStringDefinition from './FluentString';
11
9
  import type { FluentString } from './FluentString';
10
+ import { arr, fluent, pull, str } from './index';
12
11
 
13
12
  describe('Fluency', () => {
14
-
15
13
  it('creates fluent objects', () => {
16
14
  expect(fluent([42])).toBeInstanceOf(FluentArrayDefinition);
17
15
  expect(fluent('foobar')).toBeInstanceOf(FluentStringDefinition);
18
16
  });
19
-
20
17
  });
21
18
 
22
19
  describe('Fluency types', () => {
23
-
24
20
  it('has correct types', () => {
25
21
  const fluentStringArray = arr(['foo', 'bar']);
26
22
  const fluentNumbersArray = arr([42]);
@@ -35,5 +31,4 @@ describe('Fluency types', () => {
35
31
  | true
36
32
  >();
37
33
  });
38
-
39
34
  });
@@ -2,8 +2,8 @@ import { ObjectsMap, arrayFrom, arrayPull, objectPull } from '@noeldemartin/util
2
2
  import type { Obj } from '@noeldemartin/utils/helpers/index';
3
3
 
4
4
  import FluentArray from './FluentArray';
5
- import FluentString from './FluentString';
6
5
  import type { FluentArrayInstance } from './FluentArray';
6
+ import FluentString from './FluentString';
7
7
  import type { FluentStringInstance } from './FluentString';
8
8
 
9
9
  const fluentClasses = [FluentArray, FluentString];
@@ -19,7 +19,6 @@ export function arr<T>(value: unknown = []): FluentArrayInstance<FluentArray<T>,
19
19
  return FluentArray.create(arrayFrom(value) as T[]);
20
20
  }
21
21
 
22
- /* eslint-disable max-len */
23
22
  export function map<T>(items: string[], getKey?: (item: string) => T): Record<string, T>;
24
23
  export function map<T extends object>(items: []): ObjectsMap<T>;
25
24
  export function map<T extends object>(items: Iterable<T>, getKey?: (item: T) => string): ObjectsMap<T>;
@@ -41,7 +40,6 @@ export function map<T extends object, S extends keyof T>(
41
40
  {} as Record<string, unknown>,
42
41
  );
43
42
  }
44
- /* eslint-enable max-len */
45
43
 
46
44
  export function pull<T>(items: T[], index: number): T;
47
45
  export function pull<T extends Obj, K extends keyof T>(obj: T, key: K): T[K];
@@ -58,7 +56,9 @@ export function fluent(value: unknown): unknown {
58
56
  }
59
57
 
60
58
  export * from './facade';
59
+ export * from './lazyFacade';
61
60
  export type { Facade, FacadeMethods } from './facade';
61
+ export type { LazyFacade, LazyFacadeMethods } from './lazyFacade';
62
62
 
63
63
  export { tap } from './tap';
64
64
  export type { Tapped } from './tap';
@@ -1,19 +1,15 @@
1
- import { describe, expect, it } from 'vitest';
1
+ import { describe, expect, it } from 'vite-plus/test';
2
2
 
3
3
  import { invade } from './invade';
4
4
 
5
5
  describe('invade', () => {
6
-
7
6
  it('invades private methods', () => {
8
7
  class Vault {
9
-
10
8
  private getSecret(): string {
11
9
  return '42';
12
10
  }
13
-
14
11
  }
15
12
 
16
13
  expect(invade(new Vault(), ['getSecret']).getSecret()).toBe('42');
17
14
  });
18
-
19
15
  });
@@ -3,7 +3,7 @@ import type { Override } from '@noeldemartin/utils/types';
3
3
  // @ts-expect-error Allow invades
4
4
  export type Invaded<TObject, TMethods extends string> = { [K in TMethods]: TObject[K] };
5
5
 
6
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
6
+ // oxlint-disable-next-line typescript/no-unused-vars
7
7
  export function invade<T, TMethods extends string>(object: T, methods: TMethods[]): Override<T, Invaded<T, TMethods>> {
8
8
  return object as Override<T, Invaded<T, TMethods>>;
9
9
  }
@@ -0,0 +1,191 @@
1
+ import { describe, expect, expectTypeOf, it, vi } from 'vite-plus/test';
2
+
3
+ import { isFacade } from './facade';
4
+ import { isLazyFacade, lazyFacade } from './lazyFacade';
5
+
6
+ class CounterService {
7
+ public count: number = 0;
8
+
9
+ public add(amount: number = 1): number {
10
+ return (this.count += amount);
11
+ }
12
+ }
13
+
14
+ describe('Lazy Facades', () => {
15
+ it('defers creating instances until methods are called', async () => {
16
+ // Arrange
17
+ const factory = vi.fn(async () => new CounterService());
18
+ const counter = lazyFacade(factory);
19
+
20
+ expect(factory).not.toHaveBeenCalled();
21
+
22
+ // Act
23
+ const count = await counter.add(2);
24
+
25
+ // Assert
26
+ expect(count).toEqual(2);
27
+ expect(factory).toHaveBeenCalledOnce();
28
+ expectTypeOf(counter.add).returns.toEqualTypeOf<Promise<number>>();
29
+ });
30
+
31
+ it('supports classes', async () => {
32
+ // Arrange
33
+ const counter = lazyFacade(CounterService);
34
+
35
+ // Act
36
+ await counter.add();
37
+ await counter.add();
38
+
39
+ // Assert
40
+ await expect(counter.getProperty('count')).resolves.toEqual(2);
41
+ });
42
+
43
+ it('creates instances only once', async () => {
44
+ // Arrange
45
+ const factory = vi.fn(async () => new CounterService());
46
+ const counter = lazyFacade(factory);
47
+
48
+ // Act
49
+ await Promise.all([counter.add(), counter.add(), counter.add()]);
50
+
51
+ // Assert
52
+ expect(factory).toHaveBeenCalledOnce();
53
+ await expect(counter.getProperty('count')).resolves.toEqual(3);
54
+ });
55
+
56
+ it('swaps instances', async () => {
57
+ // Arrange
58
+ const factory = vi.fn(async () => new CounterService());
59
+ const counter = lazyFacade(factory);
60
+ const mock = new CounterService();
61
+
62
+ // Act
63
+ counter.setInstance(mock);
64
+
65
+ await counter.add(5);
66
+
67
+ // Assert
68
+ expect(factory).not.toHaveBeenCalled();
69
+ expect(mock.count).toEqual(5);
70
+ });
71
+
72
+ it('swaps instances while loading', async () => {
73
+ // Arrange
74
+ const { promise, resolve } = Promise.withResolvers<CounterService>();
75
+ const factory = vi.fn(() => promise);
76
+ const counter = lazyFacade(factory);
77
+ const mock = new CounterService();
78
+ const pendingCall = counter.add();
79
+
80
+ await vi.waitFor(() => expect(factory).toHaveBeenCalled());
81
+
82
+ // Act
83
+ counter.setInstance(mock);
84
+ resolve(new CounterService());
85
+
86
+ await pendingCall;
87
+
88
+ // Assert
89
+ expect(mock.count).toEqual(1);
90
+ await expect(counter.getInstance()).resolves.toBe(mock);
91
+ });
92
+
93
+ it('swaps instances while loading fails', async () => {
94
+ // Arrange
95
+ const { promise, reject } = Promise.withResolvers<CounterService>();
96
+ const factory = vi.fn(() => promise);
97
+ const counter = lazyFacade(factory);
98
+ const mock = new CounterService();
99
+ const pendingCall = counter.add();
100
+
101
+ await vi.waitFor(() => expect(factory).toHaveBeenCalled());
102
+
103
+ // Act
104
+ counter.setInstance(mock);
105
+ reject(new Error('Network error'));
106
+
107
+ // Assert
108
+ await expect(pendingCall).resolves.toEqual(1);
109
+ expect(mock.count).toEqual(1);
110
+ });
111
+
112
+ it('resets instances', async () => {
113
+ // Arrange
114
+ const counter = lazyFacade(CounterService);
115
+
116
+ await counter.add(3);
117
+
118
+ // Act
119
+ counter.reset();
120
+
121
+ // Assert
122
+ await expect(counter.getProperty('count')).resolves.toEqual(0);
123
+ });
124
+
125
+ it('retries failed factories', async () => {
126
+ // Arrange
127
+ const factory = vi
128
+ .fn<() => Promise<CounterService>>()
129
+ .mockRejectedValueOnce(new Error('Network error'))
130
+ .mockResolvedValue(new CounterService());
131
+ const counter = lazyFacade(factory);
132
+
133
+ // Act
134
+ const failedCall = counter.add();
135
+ const retriedCall = failedCall.catch(() => counter.add());
136
+
137
+ // Assert
138
+ await expect(failedCall).rejects.toThrow('Network error');
139
+ await expect(retriedCall).resolves.toEqual(1);
140
+ expect(factory).toHaveBeenCalledTimes(2);
141
+ });
142
+
143
+ it('throws factory errors', async () => {
144
+ // Arrange
145
+ const counter = lazyFacade<CounterService>(() => {
146
+ throw new Error('Factory error');
147
+ });
148
+
149
+ // Act & Assert
150
+ await expect(counter.add()).rejects.toThrow('Factory error');
151
+ });
152
+
153
+ it('can be resolved by promises', async () => {
154
+ // Arrange
155
+ const counter = lazyFacade(CounterService);
156
+
157
+ // Act
158
+ const resolved = Promise.resolve(counter);
159
+
160
+ // Assert
161
+ await expect(resolved).resolves.toBe(counter);
162
+ });
163
+
164
+ it('rejects calling properties and setting values', async () => {
165
+ // Arrange
166
+ const counter = lazyFacade(CounterService);
167
+
168
+ // Act & Assert
169
+ await expect((counter as unknown as { count(): void }).count()).rejects.toThrow('is not a function');
170
+ expect(() => ((counter as unknown as { count: number }).count = 3)).toThrow("Can't set 'count'");
171
+ });
172
+
173
+ it('ignores symbol properties', () => {
174
+ // Arrange
175
+ const factory = vi.fn(async () => new CounterService());
176
+ const counter = lazyFacade(factory);
177
+
178
+ // Assert
179
+ expect((counter as unknown as Record<symbol, unknown>)[Symbol.toPrimitive]).toBeUndefined();
180
+ expect(factory).not.toHaveBeenCalled();
181
+ });
182
+
183
+ it('is not confused with eager facades', () => {
184
+ // Arrange
185
+ const counter = lazyFacade(CounterService);
186
+
187
+ // Assert
188
+ expect(isLazyFacade(counter)).toBe(true);
189
+ expect(isFacade(counter)).toBe(false);
190
+ });
191
+ });
@@ -0,0 +1,115 @@
1
+ import type { Constructor } from '@noeldemartin/utils/types/classes';
2
+ import type { GetObjectMethods, Override } from '@noeldemartin/utils/types/helpers';
3
+
4
+ const lazyFacadeMarker = Symbol('lazyFacade');
5
+
6
+ export interface LazyFacadeMethods<TInstance extends object = object> {
7
+ getInstance(): Promise<TInstance>;
8
+ getProperty<TProperty extends keyof TInstance>(property: TProperty): Promise<TInstance[TProperty]>;
9
+ setInstance(instance: TInstance): TInstance;
10
+ reset(): void;
11
+ }
12
+
13
+ export type LazyFacadeInstanceMethods<TInstance extends object> = {
14
+ [K in GetObjectMethods<TInstance>]: TInstance[K] extends (...args: infer TArgs) => infer TResult
15
+ ? (...args: TArgs) => Promise<Awaited<TResult>>
16
+ : never;
17
+ };
18
+
19
+ export type LazyFacade<TInstance extends object = object> = Override<
20
+ LazyFacadeInstanceMethods<TInstance>,
21
+ LazyFacadeMethods<TInstance>
22
+ >;
23
+
24
+ export function lazyFacade<TInstance extends object>(factory: () => Promise<TInstance>): LazyFacade<TInstance>;
25
+ export function lazyFacade<TInstance extends object>(
26
+ defaultClassOrFactory: (() => TInstance) | Constructor<TInstance>,
27
+ ): LazyFacade<TInstance>;
28
+ export function lazyFacade<TInstance extends object>(
29
+ defaultClassOrFactory: (() => TInstance | Promise<TInstance>) | Constructor<TInstance>,
30
+ ): LazyFacade<TInstance> {
31
+ let instance: TInstance | null = null;
32
+ let loadingInstance: Promise<TInstance> | null = null;
33
+
34
+ function newInstance(): TInstance | Promise<TInstance> {
35
+ try {
36
+ return (defaultClassOrFactory as () => TInstance | Promise<TInstance>)();
37
+ } catch (error) {
38
+ try {
39
+ return new (defaultClassOrFactory as Constructor<TInstance>)();
40
+ } catch {
41
+ throw error;
42
+ }
43
+ }
44
+ }
45
+
46
+ async function getInstance(): Promise<TInstance> {
47
+ while (!instance) {
48
+ const loading = (loadingInstance ??= Promise.resolve().then(newInstance));
49
+
50
+ try {
51
+ const loadedInstance = await loading;
52
+
53
+ if (loadingInstance === loading) {
54
+ instance = loadedInstance;
55
+ }
56
+ } catch (error) {
57
+ if (loadingInstance !== loading) {
58
+ continue;
59
+ }
60
+
61
+ loadingInstance = null;
62
+
63
+ throw error;
64
+ }
65
+ }
66
+
67
+ return instance;
68
+ }
69
+
70
+ const lazyFacadeInstance: LazyFacadeMethods<TInstance> & { [lazyFacadeMarker]: true } = {
71
+ [lazyFacadeMarker]: true,
72
+ getInstance,
73
+ getProperty: async (property) => (await getInstance())[property],
74
+ setInstance: (_instance) => {
75
+ instance = _instance;
76
+ loadingInstance = null;
77
+
78
+ return _instance;
79
+ },
80
+ reset: () => {
81
+ instance = null;
82
+ loadingInstance = null;
83
+ },
84
+ };
85
+
86
+ return new Proxy(lazyFacadeInstance, {
87
+ get: (_, property) => {
88
+ if (Object.hasOwn(lazyFacadeInstance, property)) {
89
+ return Reflect.get(lazyFacadeInstance, property);
90
+ }
91
+
92
+ if (property === 'then' || typeof property === 'symbol') {
93
+ return undefined;
94
+ }
95
+
96
+ return async (...args: unknown[]) => {
97
+ const target = await getInstance();
98
+ const method = Reflect.get(target, property);
99
+
100
+ if (typeof method !== 'function') {
101
+ throw new Error(`'${String(property)}' is not a function, use getProperty() to read properties`);
102
+ }
103
+
104
+ return method.apply(target, args);
105
+ };
106
+ },
107
+ set: (_, property) => {
108
+ throw new Error(`Can't set '${String(property)}' on a lazy facade, use getInstance() instead`);
109
+ },
110
+ }) as unknown as LazyFacade<TInstance>;
111
+ }
112
+
113
+ export function isLazyFacade(value: unknown): value is LazyFacade {
114
+ return typeof value === 'object' && value !== null && lazyFacadeMarker in value;
115
+ }
@@ -1,9 +1,8 @@
1
- import { describe, expect, it } from 'vitest';
1
+ import { describe, expect, it } from 'vite-plus/test';
2
2
 
3
3
  import { required } from './required';
4
4
 
5
5
  describe('required', () => {
6
-
7
6
  it('works with instanceof', () => {
8
7
  class Foo {}
9
8
 
@@ -15,11 +14,9 @@ describe('required', () => {
15
14
  const data: { instance?: Foo } = {};
16
15
 
17
16
  class Foo {
18
-
19
17
  public isDataInstance(): boolean {
20
18
  return this === data.instance;
21
19
  }
22
-
23
20
  }
24
21
 
25
22
  data.instance = new Foo();
@@ -30,5 +27,4 @@ describe('required', () => {
30
27
  // Assert
31
28
  expect(foo.isDataInstance()).toBe(true);
32
29
  });
33
-
34
30
  });
@@ -1,13 +1,12 @@
1
1
  import { fail } from '@noeldemartin/utils/helpers/error_helpers';
2
2
 
3
3
  class RequiredHandler<Target extends object> implements ProxyHandler<Target> {
4
-
5
4
  constructor(
6
5
  private errorMessage: string,
7
6
  private getValue: () => Target,
8
7
  ) {}
9
8
 
10
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
+ // oxlint-disable-next-line typescript/no-explicit-any
11
10
  public get(_: Target, property: string | symbol, receiver: any): any {
12
11
  const obj = this.getValue() ?? fail<Target>(this.errorMessage);
13
12
  const value = Reflect.get(obj, property, receiver);
@@ -15,7 +14,7 @@ class RequiredHandler<Target extends object> implements ProxyHandler<Target> {
15
14
  return typeof value === 'function' ? value.bind(obj) : value;
16
15
  }
17
16
 
18
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
+ // oxlint-disable-next-line typescript/no-explicit-any
19
18
  public set?(_: Target, property: string | symbol, value: any, receiver: any): boolean {
20
19
  const obj = this.getValue() ?? fail<Target>(this.errorMessage);
21
20
 
@@ -27,7 +26,6 @@ class RequiredHandler<Target extends object> implements ProxyHandler<Target> {
27
26
 
28
27
  return Object.getPrototypeOf(obj);
29
28
  }
30
-
31
29
  }
32
30
 
33
31
  export function required<T extends object | null | undefined>(getValue: () => T, errorMessage?: string): NonNullable<T>;
@@ -1,15 +1,13 @@
1
- import { describe, expect, it } from 'vitest';
2
1
  import { tt } from '@noeldemartin/testing';
3
2
  import type { Expect } from '@noeldemartin/testing';
4
-
5
- import { after } from '@noeldemartin/utils/helpers/time_helpers';
6
3
  import { noop } from '@noeldemartin/utils/fluent/noop';
4
+ import { after } from '@noeldemartin/utils/helpers/time_helpers';
7
5
  import type { Equals } from '@noeldemartin/utils/types';
6
+ import { describe, expect, it } from 'vite-plus/test';
8
7
 
9
8
  import { tap } from './tap';
10
9
 
11
10
  describe('tap helper', () => {
12
-
13
11
  it('taps using callbacks', () => {
14
12
  expect(tap({ foo: 'foo' }, (o) => (o.foo += 'bar')).foo).toBe('foobar');
15
13
  });
@@ -31,13 +29,11 @@ describe('tap helper', () => {
31
29
  it('taps using proxy', () => {
32
30
  // Arrange
33
31
  class Computer {
34
-
35
32
  public theMeaningOfLife = 42;
36
33
 
37
34
  public getTheMeaningOfLife(): number {
38
35
  return this.theMeaningOfLife;
39
36
  }
40
-
41
37
  }
42
38
 
43
39
  const computer = new Computer();
@@ -49,7 +45,6 @@ describe('tap helper', () => {
49
45
  expect(tappedComputer.theMeaningOfLife).toBe(42);
50
46
  expect(tappedComputer.getTheMeaningOfLife()).toBe(tappedComputer);
51
47
  });
52
-
53
48
  });
54
49
 
55
50
  const numberResult = tap(42, noop);
@@ -57,7 +52,6 @@ const stringResult = tap('42', noop);
57
52
  const promisedNumberResult = tap(42, () => Promise.resolve());
58
53
 
59
54
  describe('tap helper types', () => {
60
-
61
55
  it(
62
56
  'has correct types',
63
57
  tt<
@@ -67,5 +61,4 @@ describe('tap helper types', () => {
67
61
  | true
68
62
  >(),
69
63
  );
70
-
71
64
  });