@nest-yalc-2/utils 1.0.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 (148) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +9 -0
  3. package/package.json +29 -0
  4. package/src/__mocks__/enum.mock.ts +9 -0
  5. package/src/__tests__/class.helper.spec.ts +32 -0
  6. package/src/__tests__/command.helper.spec.ts +35 -0
  7. package/src/__tests__/config-manager.helper.spec.ts +93 -0
  8. package/src/__tests__/custom-validator.spec.ts +34 -0
  9. package/src/__tests__/data-structure.helper.spec.ts +30 -0
  10. package/src/__tests__/date.helper.spec.ts +78 -0
  11. package/src/__tests__/encryption.helper.spec.ts +45 -0
  12. package/src/__tests__/enum.helper.spec.ts +53 -0
  13. package/src/__tests__/env.helper.spec.ts +68 -0
  14. package/src/__tests__/error.helper.spec.ts +20 -0
  15. package/src/__tests__/faker-helper.spec.ts +187 -0
  16. package/src/__tests__/files.helper.spec.ts +22 -0
  17. package/src/__tests__/http.helper.spec.ts +15 -0
  18. package/src/__tests__/interval.helper.spec.ts +29 -0
  19. package/src/__tests__/math.helper.spec.ts +24 -0
  20. package/src/__tests__/nest.decorator.spec.ts +29 -0
  21. package/src/__tests__/nest.helper.spec.ts +26 -0
  22. package/src/__tests__/object-mapper.helper.spec.ts +167 -0
  23. package/src/__tests__/object-mapper.interceptor.spec.ts +104 -0
  24. package/src/__tests__/object.helper.spec.ts +181 -0
  25. package/src/__tests__/plugin.helper.spec.ts +85 -0
  26. package/src/__tests__/promise.helper.spec.ts +76 -0
  27. package/src/__tests__/returnValue.spec.ts +49 -0
  28. package/src/__tests__/rxjs.helper.spec.ts +79 -0
  29. package/src/__tests__/simple-mapper.interceptor.spec.ts +82 -0
  30. package/src/__tests__/tsconfig.json +3 -0
  31. package/src/__tests__/validator-helper.spec.ts +34 -0
  32. package/src/__tests__/zlib.helper.spec.ts +20 -0
  33. package/src/class.helper.d.ts +4 -0
  34. package/src/class.helper.js +19 -0
  35. package/src/class.helper.js.map +1 -0
  36. package/src/class.helper.ts +33 -0
  37. package/src/command.helper.d.ts +1 -0
  38. package/src/command.helper.js +16 -0
  39. package/src/command.helper.js.map +1 -0
  40. package/src/command.helper.ts +19 -0
  41. package/src/config-manager.helper.d.ts +12 -0
  42. package/src/config-manager.helper.js +60 -0
  43. package/src/config-manager.helper.js.map +1 -0
  44. package/src/config-manager.helper.ts +122 -0
  45. package/src/custom-validator.d.ts +3 -0
  46. package/src/custom-validator.js +13 -0
  47. package/src/custom-validator.js.map +1 -0
  48. package/src/custom-validator.ts +13 -0
  49. package/src/data-structure.helper.d.ts +1 -0
  50. package/src/data-structure.helper.js +13 -0
  51. package/src/data-structure.helper.js.map +1 -0
  52. package/src/data-structure.helper.ts +14 -0
  53. package/src/date.helper.d.ts +12 -0
  54. package/src/date.helper.js +47 -0
  55. package/src/date.helper.js.map +1 -0
  56. package/src/date.helper.ts +65 -0
  57. package/src/encryption.helper.d.ts +2 -0
  58. package/src/encryption.helper.js +58 -0
  59. package/src/encryption.helper.js.map +1 -0
  60. package/src/encryption.helper.ts +29 -0
  61. package/src/enum.helper.d.ts +8 -0
  62. package/src/enum.helper.js +21 -0
  63. package/src/enum.helper.js.map +1 -0
  64. package/src/enum.helper.ts +34 -0
  65. package/src/env.helper.d.ts +3 -0
  66. package/src/env.helper.js +23 -0
  67. package/src/env.helper.js.map +1 -0
  68. package/src/env.helper.ts +35 -0
  69. package/src/error.helper.d.ts +1 -0
  70. package/src/error.helper.js +12 -0
  71. package/src/error.helper.js.map +1 -0
  72. package/src/error.helper.ts +12 -0
  73. package/src/faker-helper.d.ts +16 -0
  74. package/src/faker-helper.js +73 -0
  75. package/src/faker-helper.js.map +1 -0
  76. package/src/faker-helper.ts +98 -0
  77. package/src/files.helper.d.ts +2 -0
  78. package/src/files.helper.js +13 -0
  79. package/src/files.helper.js.map +1 -0
  80. package/src/files.helper.ts +20 -0
  81. package/src/http.helper.d.ts +26 -0
  82. package/src/http.helper.js +84 -0
  83. package/src/http.helper.js.map +1 -0
  84. package/src/http.helper.ts +152 -0
  85. package/src/index.d.ts +21 -0
  86. package/src/index.js +38 -0
  87. package/src/index.js.map +1 -0
  88. package/src/index.ts +23 -0
  89. package/src/interval.helper.d.ts +5 -0
  90. package/src/interval.helper.js +16 -0
  91. package/src/interval.helper.js.map +1 -0
  92. package/src/interval.helper.ts +13 -0
  93. package/src/math.helper.d.ts +1 -0
  94. package/src/math.helper.js +8 -0
  95. package/src/math.helper.js.map +1 -0
  96. package/src/math.helper.ts +4 -0
  97. package/src/nest.decorator.d.ts +3 -0
  98. package/src/nest.decorator.js +10 -0
  99. package/src/nest.decorator.js.map +1 -0
  100. package/src/nest.decorator.ts +10 -0
  101. package/src/nestjs/nest.decorator.d.ts +3 -0
  102. package/src/nestjs/nest.decorator.js +10 -0
  103. package/src/nestjs/nest.decorator.js.map +1 -0
  104. package/src/nestjs/nest.decorator.ts +10 -0
  105. package/src/nestjs/nest.helper.d.ts +2 -0
  106. package/src/nestjs/nest.helper.js +7 -0
  107. package/src/nestjs/nest.helper.js.map +1 -0
  108. package/src/nestjs/nest.helper.ts +5 -0
  109. package/src/object-mapper.helper.d.ts +16 -0
  110. package/src/object-mapper.helper.js +47 -0
  111. package/src/object-mapper.helper.js.map +1 -0
  112. package/src/object-mapper.helper.ts +148 -0
  113. package/src/object-mapper.interceptor.d.ts +16 -0
  114. package/src/object-mapper.interceptor.js +36 -0
  115. package/src/object-mapper.interceptor.js.map +1 -0
  116. package/src/object-mapper.interceptor.ts +51 -0
  117. package/src/object.helper.d.ts +18 -0
  118. package/src/object.helper.js +91 -0
  119. package/src/object.helper.js.map +1 -0
  120. package/src/object.helper.ts +173 -0
  121. package/src/plugin.helper.d.ts +22 -0
  122. package/src/plugin.helper.js +26 -0
  123. package/src/plugin.helper.js.map +1 -0
  124. package/src/plugin.helper.ts +42 -0
  125. package/src/promise.helper.d.ts +13 -0
  126. package/src/promise.helper.js +71 -0
  127. package/src/promise.helper.js.map +1 -0
  128. package/src/promise.helper.ts +65 -0
  129. package/src/returnValue.d.ts +14 -0
  130. package/src/returnValue.js +17 -0
  131. package/src/returnValue.js.map +1 -0
  132. package/src/returnValue.ts +26 -0
  133. package/src/rxjs.helper.d.ts +7 -0
  134. package/src/rxjs.helper.js +33 -0
  135. package/src/rxjs.helper.js.map +1 -0
  136. package/src/rxjs.helper.ts +71 -0
  137. package/src/simple-mapper.interceptor.d.ts +7 -0
  138. package/src/simple-mapper.interceptor.js +29 -0
  139. package/src/simple-mapper.interceptor.js.map +1 -0
  140. package/src/simple-mapper.interceptor.ts +43 -0
  141. package/src/validator-helper.d.ts +2 -0
  142. package/src/validator-helper.js +22 -0
  143. package/src/validator-helper.js.map +1 -0
  144. package/src/validator-helper.ts +26 -0
  145. package/src/zlib.helper.d.ts +3 -0
  146. package/src/zlib.helper.js +25 -0
  147. package/src/zlib.helper.js.map +1 -0
  148. package/src/zlib.helper.ts +19 -0
@@ -0,0 +1,181 @@
1
+ import { describe, expect, it } from '@jest/globals';
2
+ import {
3
+ _deepMerge,
4
+ deepMerge,
5
+ deepMergeWithoutArrayConcat,
6
+ getObjectId,
7
+ isObject,
8
+ isObjectStrict,
9
+ objectsHaveSameKeys,
10
+ objectSetProp,
11
+ } from '../object.helper.js';
12
+
13
+ describe('test object.helper.ts', () => {
14
+ it('should set a property correctly', () => {
15
+ const obj: any = {};
16
+ objectSetProp(obj, 'test', 1);
17
+ expect(obj.test).toBe(1);
18
+ });
19
+
20
+ it('should set a subproperty correctly', () => {
21
+ const obj: any = {};
22
+ const res = objectSetProp(obj, 'test.foo', 1);
23
+ expect(obj.test.foo).toBe(1);
24
+ expect(res.test.foo).toBe(1);
25
+ });
26
+
27
+ it('should set a subproperty on an existing parent object', () => {
28
+ const obj: any = { test: {} };
29
+ const res = objectSetProp(obj, 'test.foo', 1);
30
+ expect(res.test.foo).toBe(1);
31
+ });
32
+
33
+ it('should merge 2 objects correctly', () => {
34
+ const obj1 = { test: 1, sub: { test2: 2 }, array: [1, 2, 4] };
35
+ const obj2 = {
36
+ test: 2,
37
+ sub: { test3: 3 },
38
+ new: {
39
+ test4: 4,
40
+ },
41
+ array: [3, 4, 5],
42
+ };
43
+
44
+ const res = deepMerge(obj1, obj2);
45
+
46
+ expect(res).toStrictEqual({
47
+ test: 2,
48
+ sub: {
49
+ test2: 2,
50
+ test3: 3,
51
+ },
52
+ new: {
53
+ test4: 4,
54
+ },
55
+ array: [1, 2, 4, 3, 5],
56
+ });
57
+ });
58
+
59
+ it('should merge objects correctly (last undefined)', () => {
60
+ const obj1 = { test: 1, sub: { test2: 2 } };
61
+
62
+ const res = deepMerge(obj1);
63
+
64
+ expect(res).toStrictEqual({
65
+ test: 1,
66
+ sub: {
67
+ test2: 2,
68
+ },
69
+ });
70
+ });
71
+
72
+ it('should skip merge work for non-object targets and sources', () => {
73
+ const inherited = Object.create({ inherited: true });
74
+ expect(_deepMerge(true, null as any, { test: 1 })).toBeNull();
75
+ expect(deepMerge({}, undefined as any)).toStrictEqual({});
76
+ expect(deepMerge({}, inherited)).toStrictEqual({});
77
+ });
78
+
79
+ it('should merge objects correctly with array concat', () => {
80
+ const obj1 = { test: 1, sub: { test2: ['1', '2'] } };
81
+ const obj2 = { test: 2, sub: { test2: ['3','2'] } };
82
+
83
+ const res = deepMerge(obj1,obj2);
84
+
85
+ expect(res).toStrictEqual({
86
+ test: 2,
87
+ sub: {
88
+ test2: ['1', '2', '3'],
89
+ },
90
+ });
91
+ });
92
+
93
+ it('should merge objects correctly without array concat', () => {
94
+ const obj1 = { test: 1, sub: { test2: ['1', '2'] } };
95
+ const obj2 = { test: 2, sub: { test2: ['3','2'] } };
96
+
97
+ const res = deepMergeWithoutArrayConcat(obj1,obj2);
98
+
99
+ expect(res).toStrictEqual({
100
+ test: 2,
101
+ sub: {
102
+ test2: ['3', '2'],
103
+ },
104
+ });
105
+ });
106
+
107
+ it('should merge objects correctly with functions', () => {
108
+ const obj1 = { test: 1};
109
+ const obj2 = { test: 2, sub: { test2: () => {} } };
110
+
111
+ const res = deepMerge(obj1,obj2);
112
+
113
+ expect(res).toStrictEqual({
114
+ test: 2,
115
+ sub: {
116
+ test2: expect.any(Function),
117
+ },
118
+ });
119
+ });
120
+
121
+ it('should isObjectStrict return true', () => {
122
+ expect(isObjectStrict({})).toBe(true);
123
+ expect(isObjectStrict({ test: 1 })).toBe(true);
124
+ expect(isObjectStrict({ test: 1, sub: { test2: 2 } })).toBe(true);
125
+ expect(isObjectStrict(new Object('something'))).toBe(true);
126
+ });
127
+
128
+ it('should isObjectStrict return false', () => {
129
+ expect(isObjectStrict(null)).toBe(false);
130
+ expect(isObjectStrict(undefined)).toBe(false);
131
+ expect(isObjectStrict(1)).toBe(false);
132
+ expect(isObjectStrict('string')).toBe(false);
133
+ expect(isObjectStrict(true)).toBe(false);
134
+ expect(isObjectStrict(false)).toBe(false);
135
+ expect(isObjectStrict([1, 2, 3])).toBe(false);
136
+ expect(isObjectStrict(new Date())).toBe(false);
137
+ expect(isObjectStrict(() => {})).toBe(false);
138
+ });
139
+
140
+ it('should isObject return true', () => {
141
+ expect(isObject({})).toBe(true);
142
+ expect(isObject({ test: 1 })).toBe(true);
143
+ expect(isObject({ test: 1, sub: { test2: 2 } })).toBe(true);
144
+ expect(isObject(new Object('something'))).toBe(true);
145
+ expect(isObject(null)).toBe(false);
146
+ expect(isObject(() => {})).toBe(true);
147
+ });
148
+
149
+ it('should isObject return false', () => {
150
+ expect(isObject(undefined)).toBe(false);
151
+ expect(isObject(1)).toBe(false);
152
+ expect(isObject('string')).toBe(false);
153
+ expect(isObject(true)).toBe(false);
154
+ expect(isObject(false)).toBe(false);
155
+ expect(isObject([1, 2, 3])).toBe(false);
156
+ });
157
+
158
+
159
+ it('should getObjectId return the correct id', () => {
160
+ const obj = { id: 1 };
161
+ let res = getObjectId(obj);
162
+ expect(res).toBeGreaterThan(0);
163
+ // it should give the same result
164
+ expect(getObjectId(obj)).toBe(res);
165
+ // different objects instead should give different results
166
+ expect(getObjectId({ id: 1 })).not.toBe(1);
167
+ });
168
+
169
+
170
+ it('should objectsHaveSameKeys return true', () => {
171
+ const obj1 = { test: 1, sub: { test2: 2 } };
172
+ const obj2 = { test: 1, sub: { test2: 2 } };
173
+ expect(objectsHaveSameKeys(obj1, obj2)).toBe(true);
174
+ });
175
+
176
+ it('should objectsHaveSameKeys return false', () => {
177
+ const obj1 = { test: 1, sub: { test2: 2 } };
178
+ const obj2 = { test: 1, sub2: { test2: 2 } };
179
+ expect(objectsHaveSameKeys(obj1, obj2)).toBe(false);
180
+ });
181
+ });
@@ -0,0 +1,85 @@
1
+ import { WithPluginSystem, Plugin, PluginMethods } from '../plugin.helper.js';
2
+ import { describe, it, expect, beforeEach, jest } from '@jest/globals';
3
+
4
+ describe('WithPluginSystem', () => {
5
+ class TestMethods implements PluginMethods {
6
+ [key: string]: { (...args: any[]): void } | undefined;
7
+ testMethod1?: (...args: any[]) => void;
8
+ testMethod2?: (...args: any[]) => void;
9
+ }
10
+
11
+ class TestBase {}
12
+
13
+ it('should create a PluginSystemWrapper with default base class', () => {
14
+ const PluginSystemWrapper = WithPluginSystem<TestMethods>();
15
+ expect(new PluginSystemWrapper()).toBeInstanceOf(PluginSystemWrapper);
16
+ });
17
+
18
+ it('should create a PluginSystemWrapper with a custom base class', () => {
19
+ const PluginSystemWrapper = WithPluginSystem<TestMethods>(TestBase);
20
+ expect(new PluginSystemWrapper()).toBeInstanceOf(TestBase);
21
+ });
22
+
23
+ describe('PluginSystemWrapper', () => {
24
+ let PluginSystemWrapper: any;
25
+ let pluginSystem: any;
26
+
27
+ beforeEach(() => {
28
+ PluginSystemWrapper = WithPluginSystem<TestMethods>();
29
+ pluginSystem = new PluginSystemWrapper();
30
+ });
31
+
32
+ describe('registerPlugin', () => {
33
+ it('should register a plugin', () => {
34
+ const plugin: Plugin<TestMethods> = { pluginMethods: {} };
35
+ pluginSystem.registerPlugin(plugin);
36
+ expect(pluginSystem.plugins).toContain(plugin);
37
+ });
38
+ });
39
+
40
+ describe('unregisterPlugin', () => {
41
+ it('should unregister a plugin', () => {
42
+ const plugin: Plugin<TestMethods> = { pluginMethods: {} };
43
+ pluginSystem.registerPlugin(plugin);
44
+ pluginSystem.unregisterPlugin(plugin);
45
+ expect(pluginSystem.plugins).not.toContain(plugin);
46
+ });
47
+ });
48
+
49
+ describe('invokePlugins', () => {
50
+ it('should invoke the correct method on all plugins', () => {
51
+ const plugin1: Plugin<TestMethods> = {
52
+ pluginMethods: {
53
+ testMethod1: jest.fn(),
54
+ },
55
+ };
56
+ const plugin2: Plugin<TestMethods> = {
57
+ pluginMethods: {
58
+ testMethod1: jest.fn(),
59
+ },
60
+ };
61
+ pluginSystem.registerPlugin(plugin1);
62
+ pluginSystem.registerPlugin(plugin2);
63
+ pluginSystem.invokePlugins('testMethod1', 'arg1', 'arg2');
64
+
65
+ expect(plugin1.pluginMethods.testMethod1).toHaveBeenCalledWith(
66
+ 'arg1',
67
+ 'arg2',
68
+ );
69
+ expect(plugin2.pluginMethods.testMethod1).toHaveBeenCalledWith(
70
+ 'arg1',
71
+ 'arg2',
72
+ );
73
+ });
74
+
75
+ it('should not throw if a method is undefined', () => {
76
+ const plugin: Plugin<TestMethods> = { pluginMethods: {} };
77
+ pluginSystem.registerPlugin(plugin);
78
+
79
+ expect(() =>
80
+ pluginSystem.invokePlugins('testMethod1', 'arg1'),
81
+ ).not.toThrow();
82
+ });
83
+ });
84
+ });
85
+ });
@@ -0,0 +1,76 @@
1
+ import {
2
+ describe,
3
+ expect,
4
+ it,
5
+ jest,
6
+ beforeAll,
7
+ beforeEach,
8
+ test,
9
+ } from '@jest/globals';
10
+ import { PromiseTracker, promiseMap } from '../promise.helper.js';
11
+
12
+ describe('promise helper test', () => {
13
+ const elements = [1, 2];
14
+
15
+ it('It should resolve promises correctly with explicit options', async () => {
16
+ const result = await promiseMap(elements, async (element) => element, {
17
+ concurrency: 10,
18
+ stopOnError: false,
19
+ });
20
+
21
+ expect(result).toEqual(elements);
22
+ });
23
+
24
+ it('It should resolve promises correctly with implicit options', async () => {
25
+ const result = await promiseMap(elements, async (element) => element);
26
+
27
+ expect(result).toEqual(elements);
28
+ });
29
+ });
30
+
31
+ describe('promise tracker test', () => {
32
+ it('It should wait for all promises to resolve', async () => {
33
+ const promiseTracker = new PromiseTracker();
34
+
35
+ const promises = [Promise.resolve(1), Promise.resolve(2)];
36
+
37
+ promises.forEach((promise) => promiseTracker.add(promise));
38
+
39
+ await promiseTracker.waitForAll();
40
+
41
+ expect(promiseTracker['promises']).toHaveLength(0);
42
+ });
43
+
44
+ it('It should run deferred callbacks after tracked promises', async () => {
45
+ const promiseTracker = new PromiseTracker();
46
+ const calls: string[] = [];
47
+
48
+ promiseTracker.add(
49
+ Promise.resolve('ok').then((value) => {
50
+ calls.push(value);
51
+ return value;
52
+ }),
53
+ );
54
+
55
+ promiseTracker.addDeferred(() => {
56
+ calls.push('deferred');
57
+ });
58
+
59
+ await promiseTracker.waitForAll();
60
+
61
+ expect(calls).toEqual(['ok', 'deferred']);
62
+ expect(promiseTracker['promises']).toHaveLength(0);
63
+ });
64
+
65
+ it('It should clean up tracked promises when they reject', async () => {
66
+ const promiseTracker = new PromiseTracker();
67
+ const error = new Error('boom');
68
+
69
+ promiseTracker.add(Promise.reject(error));
70
+
71
+ await expect(promiseTracker.waitForAll()).rejects.toThrow(error);
72
+ await new Promise((resolve) => setTimeout(resolve, 0));
73
+
74
+ expect(promiseTracker['promises']).toHaveLength(0);
75
+ });
76
+ });
@@ -0,0 +1,49 @@
1
+ import {
2
+ expect,
3
+ jest,
4
+ describe,
5
+ it,
6
+ beforeEach,
7
+ beforeAll,
8
+ afterAll,
9
+ afterEach,
10
+ test,
11
+ } from '@jest/globals';
12
+
13
+ import returnValue, { returnAsyncValue, returnProperty } from '../returnValue.js';
14
+
15
+ test('returnValue should return a constant function', () => {
16
+ class TypeTest {}
17
+
18
+ const testArgSamples = [
19
+ 'pass',
20
+ [],
21
+ TypeTest,
22
+ undefined,
23
+ null,
24
+ () => 'Another Function',
25
+ ];
26
+
27
+ testArgSamples.forEach((value) => {
28
+ const returnTypeTest = returnValue(value);
29
+ expect(returnTypeTest).toBeInstanceOf(Function);
30
+ expect(returnTypeTest()).toEqual(value);
31
+ });
32
+ });
33
+
34
+ test('returnAsyncValue should resolve the provided value', async () => {
35
+ const fn = returnAsyncValue('async-value');
36
+ await expect(fn()).resolves.toBe('async-value');
37
+ });
38
+
39
+ test('returnProperty should return a property of a class', () => {
40
+ class TypeTest {
41
+ validProperty = 'something';
42
+ }
43
+
44
+ const returnTypeTest = returnProperty<TypeTest>('validProperty');
45
+ expect(returnTypeTest(new TypeTest())).toBe('something');
46
+
47
+ const returnTypeBadTest = returnProperty<any>('invalidProperty');
48
+ expect(returnTypeBadTest(new TypeTest())).toBeUndefined();
49
+ });
@@ -0,0 +1,79 @@
1
+ import {
2
+ expect,
3
+ jest,
4
+ describe,
5
+ it,
6
+ beforeEach,
7
+ beforeAll,
8
+ afterAll,
9
+ afterEach,
10
+ } from '@jest/globals';
11
+ import * as rxjs from 'rxjs';
12
+
13
+ import * as rxjsHelper from '../rxjs.helper.js';
14
+
15
+ describe('test rxjs helper', () => {
16
+ it('should execute switchTap', async () => {
17
+ const subject = rxjs.of(1).pipe(
18
+ rxjsHelper.switchTap(async (value: any, index: number) => {
19
+ return value;
20
+ }),
21
+ );
22
+
23
+ await expect(rxjs.lastValueFrom(subject)).resolves.toEqual(1);
24
+ });
25
+
26
+ it('should execute switchTap without return any values', async () => {
27
+ const subject = rxjs
28
+ .of(1)
29
+ .pipe(rxjsHelper.switchTap(async (value: any, index: number) => {}));
30
+
31
+ await expect(rxjs.lastValueFrom(subject)).resolves.toEqual(1);
32
+ });
33
+
34
+ it('should execute switchTap returning a null value', async () => {
35
+ const subject = rxjs
36
+ .of(1)
37
+ .pipe(rxjsHelper.switchTap(async (value: any, index: number) => null));
38
+
39
+ await expect(rxjs.lastValueFrom(subject)).resolves.toEqual(null);
40
+ });
41
+
42
+ it('should execute wrapIntoObservable', () => {
43
+ const subject = rxjsHelper.wrapIntoObservable(new rxjs.Observable());
44
+ expect(subject).toBeDefined();
45
+ });
46
+
47
+ it('should execute wrapIntoObservable with async function', () => {
48
+ const subject = rxjsHelper.wrapIntoObservable(async () => {});
49
+ expect(subject).toBeDefined();
50
+ });
51
+
52
+ it('should execute wrapIntoObservable with non-async function', () => {
53
+ const subject = rxjsHelper.wrapIntoObservable(() => {});
54
+ expect(subject).toBeDefined();
55
+ });
56
+
57
+ it('should execute wrapIntoObservable with null', () => {
58
+ const subject = rxjsHelper.wrapIntoObservable(null);
59
+ expect(subject).toBeDefined();
60
+ });
61
+
62
+ it('should execute wrapIntoAnOperator', () => {
63
+ const subject = rxjsHelper.wrapIntoAnOperator(
64
+ (value: any, index: number) => {
65
+ return value;
66
+ },
67
+ );
68
+ expect(subject(new rxjs.Observable())).toBeDefined();
69
+ });
70
+
71
+ it('should execute wrapIntoAnOperator with async function', () => {
72
+ const subject = rxjsHelper.wrapIntoAnOperator(
73
+ async (value: any, index: number) => {
74
+ return value;
75
+ },
76
+ );
77
+ expect(subject(new rxjs.Observable())).toBeDefined();
78
+ });
79
+ });
@@ -0,0 +1,82 @@
1
+ import { describe, expect, it, jest, beforeEach } from '@jest/globals';
2
+ import { ExecutionContext, CallHandler, NestInterceptor } from '@nestjs/common';
3
+ import { of } from 'rxjs';
4
+ import { buildSimpleMapperInterceptor } from '../simple-mapper.interceptor.js';
5
+
6
+ describe('SimpleMapperInterceptor', () => {
7
+ let interceptor: NestInterceptor;
8
+
9
+ class TestDto {
10
+ prop1: string;
11
+ prop2: number;
12
+
13
+ constructor(data: { prop1: string; prop2: number }) {
14
+ this.prop1 = data.prop1;
15
+ this.prop2 = data.prop2;
16
+ }
17
+ }
18
+
19
+ const rawTestObject = { prop1: 'test', prop2: 123 };
20
+
21
+ beforeEach(() => {
22
+ interceptor = new (buildSimpleMapperInterceptor(TestDto))();
23
+ });
24
+
25
+ it('should be defined', () => {
26
+ expect(interceptor).toBeDefined();
27
+ });
28
+
29
+ it('should map raw data to DTO', (done) => {
30
+ const executionContext = {
31
+ switchToHttp: jest.fn().mockReturnThis(),
32
+ getRequest: jest.fn(),
33
+ } as unknown as ExecutionContext;
34
+
35
+ const next: CallHandler = {
36
+ handle: () => of(rawTestObject),
37
+ };
38
+
39
+ interceptor.intercept(executionContext, next).subscribe((result) => {
40
+ expect(result).toBeInstanceOf(TestDto);
41
+ expect(result).toMatchObject(rawTestObject);
42
+ done();
43
+ });
44
+ });
45
+
46
+ it('should map arrays with transformer and callback', (done) => {
47
+ const transformer = jest.fn((data: any) =>
48
+ data.map((item: any) => ({ ...item, prop2: item.prop2 + 1 })),
49
+ );
50
+ const callback = jest.fn((input: any, output: any) => ({
51
+ input,
52
+ output,
53
+ }));
54
+ const ArrayInterceptor = buildSimpleMapperInterceptor(TestDto, {
55
+ transformer,
56
+ callback,
57
+ });
58
+ interceptor = new ArrayInterceptor();
59
+
60
+ const executionContext = {
61
+ switchToHttp: jest.fn().mockReturnThis(),
62
+ getRequest: jest.fn(),
63
+ } as unknown as ExecutionContext;
64
+
65
+ const next: CallHandler = {
66
+ handle: () =>
67
+ of([
68
+ rawTestObject,
69
+ { prop1: 'second', prop2: 321 },
70
+ ] as { prop1: string; prop2: number }[]),
71
+ };
72
+
73
+ interceptor.intercept(executionContext, next).subscribe((result) => {
74
+ expect(transformer).toHaveBeenCalled();
75
+ expect(callback).toHaveBeenCalled();
76
+ expect(result.input).toHaveLength(2);
77
+ expect(result.output[0]).toBeInstanceOf(TestDto);
78
+ expect(result.output[0]).toMatchObject({ prop1: 'test', prop2: 124 });
79
+ done();
80
+ });
81
+ });
82
+ });
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../../../tsconfig.test.json"
3
+ }
@@ -0,0 +1,34 @@
1
+ import { describe, expect, it } from '@jest/globals';
2
+ import {
3
+ stringIsInEnum,
4
+ stringIsInEnumOrThrow,
5
+ } from '../validator-helper.js';
6
+
7
+ enum TestEnum {
8
+ FOO = 'FOO',
9
+ BAR = 'BAR',
10
+ }
11
+
12
+ describe('validator-helper', () => {
13
+ it('should match enum values case-insensitively', () => {
14
+ expect(stringIsInEnum('foo', TestEnum)).toBe(true);
15
+ expect(stringIsInEnum('Bar', TestEnum)).toBe(true);
16
+ expect(stringIsInEnum('baz', TestEnum)).toBe(false);
17
+ });
18
+
19
+ it('should throw with default message when value is invalid', () => {
20
+ expect(() => stringIsInEnumOrThrow('missing', TestEnum)).toThrow(
21
+ 'Invalid value missing',
22
+ );
23
+ });
24
+
25
+ it('should allow custom error messages', () => {
26
+ expect(() =>
27
+ stringIsInEnumOrThrow('invalid', TestEnum, 'custom message'),
28
+ ).toThrow('custom message');
29
+ });
30
+
31
+ it('should return true when value is valid', () => {
32
+ expect(stringIsInEnumOrThrow('foo', TestEnum)).toBe(true);
33
+ });
34
+ });
@@ -0,0 +1,20 @@
1
+ import { describe, expect, it } from '@jest/globals';
2
+ import { inflate, deflate } from '../zlib.helper.js';
3
+
4
+ describe('zlib helpers', () => {
5
+ it('should return input when inflate/deflate fail', () => {
6
+ const plain = 'not-base64';
7
+ expect(inflate(plain)).toBe(plain);
8
+
9
+ const invalidInput: any = { invalid: true };
10
+ expect(deflate(invalidInput)).toBe(invalidInput);
11
+ });
12
+
13
+ it('should deflate and inflate valid payloads', () => {
14
+ const payload = 'hello';
15
+ const deflated = deflate(payload) as Buffer;
16
+ expect(deflated).toBeInstanceOf(Buffer);
17
+ const inflated = inflate(deflated.toString('base64'));
18
+ expect(inflated).toBe(payload);
19
+ });
20
+ });
@@ -0,0 +1,4 @@
1
+ import { ClassType } from '@nest-yalc-2/types/globals.d.js';
2
+ export declare function isNativeClass<T = any>(func: any, className?: string): func is ClassType<T>;
3
+ export declare function isES6Class<T = any>(func: any, className?: string): func is ClassType<T>;
4
+ export declare function isClass<T = any>(func: any, className?: string): func is ClassType<T>;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isNativeClass = isNativeClass;
4
+ exports.isES6Class = isES6Class;
5
+ exports.isClass = isClass;
6
+ function isNativeClass(func, className) {
7
+ return (typeof func === 'function' &&
8
+ func.prototype?.constructor === func &&
9
+ (className ? func.name === className : true));
10
+ }
11
+ function isES6Class(func, className) {
12
+ return (typeof func === 'function' &&
13
+ /^class\s/.test(func.toString()) &&
14
+ (className ? func.name === className : true));
15
+ }
16
+ function isClass(func, className) {
17
+ return isNativeClass(func, className) || isES6Class(func, className);
18
+ }
19
+ //# sourceMappingURL=class.helper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"class.helper.js","sourceRoot":"","sources":["class.helper.ts"],"names":[],"mappings":";;AAGA,sCASC;AAGD,gCASC;AAGD,0BAKC;AA7BD,SAAgB,aAAa,CAC3B,IAAS,EACT,SAAkB;IAElB,OAAO,CACL,OAAO,IAAI,KAAK,UAAU;QAC1B,IAAI,CAAC,SAAS,EAAE,WAAW,KAAK,IAAI;QACpC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAC7C,CAAC;AACJ,CAAC;AAGD,SAAgB,UAAU,CACxB,IAAS,EACT,SAAkB;IAElB,OAAO,CACL,OAAO,IAAI,KAAK,UAAU;QAC1B,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;QAChC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAC7C,CAAC;AACJ,CAAC;AAGD,SAAgB,OAAO,CACrB,IAAS,EACT,SAAkB;IAElB,OAAO,aAAa,CAAC,IAAI,EAAE,SAAS,CAAC,IAAI,UAAU,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;AACvE,CAAC"}
@@ -0,0 +1,33 @@
1
+ import { ClassType } from '@nest-yalc-2/types/globals.d.js';
2
+
3
+ // Check if the class is a native class (like Error, Array, etc.)
4
+ export function isNativeClass<T = any>(
5
+ func: any,
6
+ className?: string,
7
+ ): func is ClassType<T> {
8
+ return (
9
+ typeof func === 'function' &&
10
+ func.prototype?.constructor === func && // Native class constructors have this prototype constructor link
11
+ (className ? func.name === className : true)
12
+ );
13
+ }
14
+
15
+ // Check if the class is an ES6-style class
16
+ export function isES6Class<T = any>(
17
+ func: any,
18
+ className?: string,
19
+ ): func is ClassType<T> {
20
+ return (
21
+ typeof func === 'function' &&
22
+ /^class\s/.test(func.toString()) && // ES6 classes start with the keyword "class"
23
+ (className ? func.name === className : true)
24
+ );
25
+ }
26
+
27
+ // Check if the class is either native or ES6
28
+ export function isClass<T = any>(
29
+ func: any,
30
+ className?: string,
31
+ ): func is ClassType<T> {
32
+ return isNativeClass(func, className) || isES6Class(func, className);
33
+ }
@@ -0,0 +1 @@
1
+ export declare const commandWithErrors: (command: (...args: any[]) => Promise<any>) => (...args: any[]) => Promise<void>;