@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,187 @@
1
+ import {
2
+ describe,
3
+ expect,
4
+ it,
5
+ jest,
6
+ beforeAll,
7
+ beforeEach,
8
+ test,
9
+ } from '@jest/globals';
10
+
11
+ import {
12
+ DEF_FAKER_MAX_TIME,
13
+ FakerHelper,
14
+ } from '../faker-helper.js';
15
+ import { faker } from '@faker-js/faker';
16
+
17
+ enum testEnum {
18
+ KEY = 'value',
19
+ VALUE_KEY = 'test',
20
+ }
21
+ describe('faker helper test', () => {
22
+ // Don't do beforeEach, since we need a single instance
23
+ const fakerHelper = new FakerHelper();
24
+ it('should generate a person object, with first-, lastName, gender and email', () => {
25
+ jest.spyOn(faker.person, 'firstName').mockReturnValueOnce('Elon');
26
+ jest.spyOn(faker.person, 'lastName').mockReturnValueOnce('Musk');
27
+ jest
28
+ .spyOn(faker.internet, 'email')
29
+ .mockReturnValueOnce('Elon_Musk@gmail.test');
30
+ const person = fakerHelper.createPerson();
31
+ expect(person).toBeDefined();
32
+ expect(person.email).toBeDefined();
33
+ expect(person.firstName).toBeDefined();
34
+ expect(person.lastName).toBeDefined();
35
+ expect(person.gender).toBeDefined();
36
+ });
37
+
38
+ it('should not reuse already generated names', () => {
39
+ jest.spyOn(faker.person, 'firstName').mockReturnValueOnce('Elon');
40
+ jest.spyOn(faker.person, 'lastName').mockReturnValueOnce('Musk');
41
+ jest
42
+ .spyOn(faker.internet, 'email')
43
+ .mockReturnValueOnce('Elon_Musk@gmail.test');
44
+ const person = fakerHelper.createPerson();
45
+ expect(person).toBeDefined();
46
+ expect(person.email).not.toEqual('ElonMusk@google.test');
47
+ expect(person.firstName).toBeDefined();
48
+ expect(person.lastName).toBeDefined();
49
+ expect(person.gender).toBeDefined();
50
+ });
51
+
52
+ it('should be able to use first and lastname to generate email', () => {
53
+ const firstName = 'Elon';
54
+ const lastName = 'Musk';
55
+
56
+ const email = fakerHelper.generateNewEmail(firstName, lastName);
57
+ expect(email).toContain(firstName);
58
+
59
+ // Not validating lastName as the Faker library may not use it
60
+ // @url https://github.com/Marak/faker.js/issues/791
61
+ });
62
+
63
+ it('should be able to use first name, lastname and provider to generate email', () => {
64
+ const firstName = 'Elon';
65
+ const lastName = 'Musk';
66
+ const provider = 'tesla.com';
67
+
68
+ const email = fakerHelper.generateNewEmail(firstName, lastName, provider);
69
+ expect(email).toContain(firstName);
70
+ expect(email).toContain(provider);
71
+ });
72
+
73
+ it('should generate unique emails on multiple calls', () => {
74
+ const firstName = 'Elon';
75
+ const lastName = 'Musk';
76
+ const provider = 'dogecoin.lol';
77
+
78
+ const email1 = fakerHelper.generateNewEmail(firstName, lastName, provider);
79
+ const email2 = fakerHelper.generateNewEmail(firstName, lastName, provider);
80
+ const email3 = fakerHelper.generateNewEmail(firstName, lastName, provider);
81
+
82
+ expect(email1).not.toEqual(email2);
83
+ expect(email2).not.toEqual(email3);
84
+ });
85
+
86
+ it('should fall back when unique email generation exceeds the time limit', () => {
87
+ const helper = new FakerHelper();
88
+ jest
89
+ .spyOn(faker.internet, 'email')
90
+ .mockReturnValueOnce('same@gmail.test');
91
+ helper.generateNewEmail('Elon', 'Musk', 'gmail.test');
92
+
93
+ jest
94
+ .spyOn(Date, 'now')
95
+ .mockReturnValueOnce(0)
96
+ .mockReturnValueOnce(DEF_FAKER_MAX_TIME + 1);
97
+ jest
98
+ .spyOn(faker.internet, 'email')
99
+ .mockReturnValueOnce('same@gmail.test')
100
+ .mockReturnValueOnce('fallback@gmail.test');
101
+
102
+ expect(helper.generateNewEmail('Elon', 'Musk', 'gmail.test')).toBe(
103
+ 'fallback@gmail.test',
104
+ );
105
+ expect(
106
+ (
107
+ helper as unknown as { generatedEmails: Set<string> }
108
+ ).generatedEmails.has('fallback@gmail.test'),
109
+ ).toBe(true);
110
+ });
111
+
112
+ it('should be able to generate a valid birthDate between age 18 and 100 (YYYY-MM-DD) (min age)', () => {
113
+ const currentDate = new Date();
114
+ // make a copy instead of reusing pointer
115
+ const returnDate = new Date(currentDate.toUTCString());
116
+ jest.spyOn(faker.date, 'past').mockReturnValue(returnDate);
117
+
118
+ const currentYear = currentDate.getUTCFullYear();
119
+ const currentMonth = currentDate.getUTCMonth() + 1;
120
+ const currentDay = currentDate.getUTCDate();
121
+
122
+ const birthDate = fakerHelper.randomBirthDate();
123
+ const splitted = birthDate.split('-');
124
+
125
+ expect(parseInt(splitted[0])).toEqual(currentYear - 18);
126
+
127
+ // TODO: fix it for the edge cases (february, 00:00 etc.)
128
+
129
+ // expect(parseInt(splitted[1])).toEqual(currentMonth);
130
+ // expect(parseInt(splitted[2])).toEqual(currentDay);
131
+ });
132
+
133
+ it('should be able to generate a valid birthDate between age 18 and 100 (YYYY-MM-DD) (max age)', () => {
134
+ const currentDate = new Date();
135
+ // make a copy instead of reusing pointer
136
+ const returnDate = new Date(currentDate.valueOf());
137
+ returnDate.setUTCFullYear(returnDate.getUTCFullYear() - 82);
138
+ jest.spyOn(faker.date, 'past').mockReturnValue(returnDate);
139
+
140
+ const currentYear = currentDate.getUTCFullYear();
141
+ const currentMonth = currentDate.getUTCMonth() + 1;
142
+ // const currentDay = currentDate.getUTCDate();
143
+
144
+ const birthDate = fakerHelper.randomBirthDate();
145
+ const splitted = birthDate.split('-');
146
+
147
+ expect(parseInt(splitted[0])).toEqual(currentYear - 100);
148
+
149
+ // TODO: fix it for the edge cases (february, 00:00 etc.)
150
+
151
+ // expect(parseInt(splitted[1])).toEqual(currentMonth);
152
+ // expect(parseInt(splitted[2])).toEqual(currentDay);
153
+ });
154
+
155
+ it('should generate valid lock dates in the past', () => {
156
+ jest.clearAllMocks();
157
+ jest.spyOn(faker.number, 'int').mockReturnValue(0 as never);
158
+ expect(fakerHelper.randomLockDate().valueOf()).toBeLessThan(Date.now());
159
+ });
160
+
161
+ it('should generate valid lock dates in the future', () => {
162
+ jest.clearAllMocks();
163
+ jest.spyOn(faker.number, 'int').mockReturnValueOnce(1 as never);
164
+ jest
165
+ .spyOn(faker.date, 'future')
166
+ .mockReturnValueOnce(new Date(Date.now() + 1000));
167
+ expect(fakerHelper.randomLockDate().valueOf()).toBeGreaterThan(Date.now());
168
+ });
169
+
170
+ it('should return undefined when no lock is present', () => {
171
+ jest.clearAllMocks();
172
+ jest.spyOn(faker.number, 'int').mockReturnValue(2 as never);
173
+ expect(fakerHelper.randomLockDate()).toEqual(undefined);
174
+ });
175
+
176
+ it('should return the first element of the enum', () => {
177
+ jest.clearAllMocks();
178
+ jest.spyOn(faker.number, 'int').mockReturnValue(0 as never);
179
+ expect(fakerHelper.randomFromEnum(testEnum)).toEqual(testEnum.KEY);
180
+ });
181
+
182
+ it('should return a decimal', () => {
183
+ jest.clearAllMocks();
184
+ jest.spyOn(faker.number, 'float').mockReturnValue(3.14 as never);
185
+ expect(fakerHelper.randomDecimal(0, 42, 2)).toEqual('3.14');
186
+ });
187
+ });
@@ -0,0 +1,22 @@
1
+ import {
2
+ describe,
3
+ expect,
4
+ it,
5
+ jest,
6
+ beforeAll,
7
+ beforeEach,
8
+ test,
9
+ } from '@jest/globals';
10
+
11
+ import { ___dirname, __filename } from '../files.helper.js';
12
+
13
+ describe('Test files.helper.ts', () => {
14
+ it('should run __filename', () => {
15
+ expect(__filename('file:///C:/Users/nn/Desktop/code/nestjs-yalc/utils/src/__tests__/files.helper.spec.ts')).toBeDefined();
16
+ });
17
+
18
+ it('should run ___dirname', () => {
19
+ const dir = ___dirname('file:///C:/Users/nn/Desktop/code/nestjs-yalc/utils/src/__tests__/files.helper.spec.ts');
20
+ expect(dir).toContain('utils');
21
+ });
22
+ });
@@ -0,0 +1,15 @@
1
+ import { describe, expect, it } from '@jest/globals';
2
+
3
+ import { getHttpStatusDescription } from '../http.helper.js';
4
+
5
+ describe('Test http.helper.ts', () => {
6
+ it('should run getHttpStatusDescription', () => {
7
+ expect(getHttpStatusDescription(200)).toBe(
8
+ '200: Request successful and response provided.',
9
+ );
10
+ });
11
+
12
+ it('should work with the fallback', () => {
13
+ expect(getHttpStatusDescription(999)).toBe('Unknown status code');
14
+ });
15
+ });
@@ -0,0 +1,29 @@
1
+ import {
2
+ describe,
3
+ expect,
4
+ it,
5
+ jest,
6
+ beforeAll,
7
+ beforeEach,
8
+ test,
9
+ } from '@jest/globals';
10
+ import { IntervalHelper } from '../interval.helper.js';
11
+
12
+ describe('interval helper test', () => {
13
+ it('Generates interval of 1 day with miliseconds', () => {
14
+ const interval = IntervalHelper.createOneDayInterval();
15
+
16
+ expect(interval).toEqual(86400000);
17
+ });
18
+
19
+ it('Generates 1 second interval', () => {
20
+ const interval = IntervalHelper.createOneSecondInterval();
21
+
22
+ expect(interval).toEqual(1000);
23
+ });
24
+ it('Generates interval of 5 mins with miliseconds', () => {
25
+ const interval = IntervalHelper.createFiveMinutesInterval();
26
+
27
+ expect(interval).toEqual(300000);
28
+ });
29
+ });
@@ -0,0 +1,24 @@
1
+ import {
2
+ describe,
3
+ expect,
4
+ it,
5
+ jest,
6
+ beforeAll,
7
+ beforeEach,
8
+ test,
9
+ } from '@jest/globals';
10
+ import { isScientificNotation } from '../math.helper.js';
11
+
12
+ describe('math helper test', () => {
13
+ it('should return true for scientific notation', () => {
14
+ const result = isScientificNotation(0.000000002);
15
+
16
+ expect(result).toBeTruthy();
17
+ });
18
+
19
+ it('should return false for non scientific notation', () => {
20
+ const result = isScientificNotation(0.002);
21
+
22
+ expect(result).toBeFalsy();
23
+ });
24
+ });
@@ -0,0 +1,29 @@
1
+ import {
2
+ describe,
3
+ expect,
4
+ it,
5
+ jest,
6
+ beforeAll,
7
+ beforeEach,
8
+ test,
9
+ } from '@jest/globals';
10
+ import {
11
+ GetContext,
12
+ paramDecoratorToCreate,
13
+ } from '../nestjs/nest.decorator.js';
14
+ import { createMock } from '@golevelup/ts-jest';
15
+ import { ExecutionContext } from '@nestjs/common';
16
+
17
+ describe('Role decoratortest', () => {
18
+ const mockedCtx = createMock<ExecutionContext>();
19
+
20
+ it('Check GetContext', async () => {
21
+ const testData = GetContext(null, mockedCtx);
22
+ expect(testData).toBeDefined();
23
+ });
24
+
25
+ it('Check paramDecoratorToCreate ', async () => {
26
+ const testData = paramDecoratorToCreate(null, mockedCtx);
27
+ expect(testData).toEqual(mockedCtx);
28
+ });
29
+ });
@@ -0,0 +1,26 @@
1
+ import { describe, expect, it } from '@jest/globals';
2
+ import { isProviderObject } from '../nestjs/nest.helper.js';
3
+
4
+ describe('isProviderObject', () => {
5
+ it('should return true for a provider object', () => {
6
+ const provider = {
7
+ provide: 'PROVIDER',
8
+ useValue: 'VALUE',
9
+ };
10
+
11
+ expect(isProviderObject(provider)).toBe(true);
12
+ });
13
+
14
+ it('should return false for a non-provider object', () => {
15
+ const provider = {
16
+ provide: 'PROVIDER',
17
+ useValue: 'VALUE',
18
+ };
19
+
20
+ expect(isProviderObject(provider)).toBe(true);
21
+ });
22
+
23
+ it('should return false for a non-object', () => {
24
+ expect(isProviderObject('string')).toBe(false);
25
+ });
26
+ });
@@ -0,0 +1,167 @@
1
+ import { describe, expect, it } from '@jest/globals';
2
+
3
+ import { objectMapper } from '../object-mapper.helper.js';
4
+
5
+ describe('objectMapper', () => {
6
+ it('should map properties with the specified mapper', () => {
7
+ const input = {
8
+ a: 'A',
9
+ b: 'B',
10
+ c: 'C',
11
+ };
12
+
13
+ const mapper = {
14
+ a: 'x',
15
+ b: 'y',
16
+ c: false,
17
+ };
18
+
19
+ const output = objectMapper(input, mapper);
20
+
21
+ expect(output).toEqual({
22
+ x: 'A',
23
+ y: 'B',
24
+ });
25
+ });
26
+
27
+ it('should map properties with the specified mapper using transformer', () => {
28
+ const input = {
29
+ a: 'A',
30
+ b: 'B',
31
+ };
32
+
33
+ const mapper = {
34
+ a: {
35
+ x: {
36
+ transformer: (
37
+ inputObject: any,
38
+ propertyName: keyof typeof inputObject,
39
+ ) => inputObject[propertyName].toLowerCase(),
40
+ },
41
+ },
42
+ b: {
43
+ y: {
44
+ transformer: (
45
+ inputObject: any,
46
+ propertyName: keyof typeof inputObject,
47
+ ) => inputObject[propertyName].toLowerCase(),
48
+ },
49
+ },
50
+ };
51
+
52
+ const output = objectMapper(input, mapper);
53
+
54
+ expect(output).toEqual({
55
+ x: 'a',
56
+ y: 'b',
57
+ });
58
+ });
59
+
60
+ it('should not map excluded properties in object type mapProperty', () => {
61
+ const input = {
62
+ a: 'A',
63
+ b: 'B',
64
+ };
65
+
66
+ const mapper = {
67
+ a: {
68
+ x: {
69
+ exclude: true,
70
+ },
71
+ y: true,
72
+ },
73
+ };
74
+
75
+ const output = objectMapper(input, mapper);
76
+
77
+ expect(output).toEqual({
78
+ y: 'A',
79
+ });
80
+ });
81
+
82
+ it('should exclude properties with false value in object type mapProperty', () => {
83
+ const input = {
84
+ a: 'A',
85
+ b: 'B',
86
+ };
87
+
88
+ const mapper = {
89
+ a: {
90
+ x: false,
91
+ y: true,
92
+ },
93
+ };
94
+
95
+ const output = objectMapper(input, mapper);
96
+
97
+ expect(output).toEqual({
98
+ y: 'A',
99
+ });
100
+ });
101
+
102
+ it('should copy non-mapped properties when copyNonMappedProperties option is true', () => {
103
+ const input = {
104
+ a: 'A',
105
+ b: 'B',
106
+ };
107
+
108
+ const mapper = {
109
+ a: 'x',
110
+ z: {
111
+ y: null,
112
+ },
113
+ };
114
+
115
+ const output = objectMapper(input, mapper, {
116
+ copyNonMappedProperties: true,
117
+ });
118
+
119
+ expect(output).toEqual({
120
+ x: 'A',
121
+ b: 'B',
122
+ });
123
+ });
124
+
125
+ it('should not copy non-mapped properties when copyNonMappedProperties option is false', () => {
126
+ const input = {
127
+ a: 'A',
128
+ b: 'B',
129
+ };
130
+
131
+ const mapper = {
132
+ a: 'x',
133
+ };
134
+
135
+ const output = objectMapper(input, mapper, {
136
+ copyNonMappedProperties: false,
137
+ });
138
+
139
+ expect(output).toEqual({
140
+ x: 'A',
141
+ });
142
+ });
143
+
144
+ it('should call the $transformer function if defined in the input object', () => {
145
+ const input = {
146
+ a: 'A',
147
+ b: 'B',
148
+ $transformer: (
149
+ inputObject: { a: string; b: string },
150
+ outputObject: { x: string; y: string },
151
+ ) => {
152
+ outputObject.y = inputObject.b.toLowerCase();
153
+ },
154
+ };
155
+
156
+ const mapper = {
157
+ a: 'x',
158
+ };
159
+
160
+ const output = objectMapper(input, mapper);
161
+
162
+ expect(output).toEqual({
163
+ x: 'A',
164
+ y: 'b',
165
+ });
166
+ });
167
+ });
@@ -0,0 +1,104 @@
1
+ import { describe, expect, it } from '@jest/globals';
2
+
3
+ import { CallHandler, ExecutionContext } from '@nestjs/common';
4
+ import { objectMapperInterceptor } from '../object-mapper.interceptor.js';
5
+ import { ObjectMapperType } from '../object-mapper.helper.js';
6
+ import { of } from 'rxjs';
7
+ import { createMock } from '@golevelup/ts-jest';
8
+
9
+ describe('objectMapperInterceptor', () => {
10
+ const input = {
11
+ a: 'A',
12
+ b: 'B',
13
+ };
14
+
15
+ const mapper: ObjectMapperType<typeof input, { x: string; y: string }> = {
16
+ a: 'x',
17
+ b: 'y',
18
+ };
19
+
20
+ const mockExecutionContext = createMock<ExecutionContext>();
21
+
22
+ const mockCallHandler: CallHandler = {
23
+ handle: () => of(input),
24
+ };
25
+
26
+ it('should map properties with the specified mapper', (done) => {
27
+ const interceptor = objectMapperInterceptor(mapper);
28
+ const instance = new interceptor();
29
+ instance
30
+ .intercept(mockExecutionContext, mockCallHandler)
31
+ .subscribe((result) => {
32
+ expect(result).toEqual({
33
+ x: 'A',
34
+ y: 'B',
35
+ });
36
+ done();
37
+ });
38
+ });
39
+
40
+ it('should handle array data and map each item', (done) => {
41
+ const interceptor = objectMapperInterceptor(mapper);
42
+ const instance = new interceptor();
43
+
44
+ const arrayData = [input, input];
45
+ const mockCallHandlerWithArray: CallHandler = {
46
+ handle: () => of(arrayData),
47
+ };
48
+
49
+ instance
50
+ .intercept(mockExecutionContext, mockCallHandlerWithArray)
51
+ .subscribe((result) => {
52
+ expect(result).toEqual([
53
+ {
54
+ x: 'A',
55
+ y: 'B',
56
+ },
57
+ {
58
+ x: 'A',
59
+ y: 'B',
60
+ },
61
+ ]);
62
+ done();
63
+ });
64
+ });
65
+
66
+ it('should use transformData if provided', (done) => {
67
+ const interceptor = objectMapperInterceptor(mapper, {
68
+ transformData: (data: any) => {
69
+ return { ...data, a: data.a.toLowerCase() };
70
+ },
71
+ });
72
+
73
+ const instance = new interceptor();
74
+ instance
75
+ .intercept(mockExecutionContext, mockCallHandler)
76
+ .subscribe((result) => {
77
+ expect(result).toEqual({
78
+ x: 'a',
79
+ y: 'B',
80
+ });
81
+ done();
82
+ });
83
+ });
84
+
85
+ it('should use callback if provided', (done) => {
86
+ const interceptor = objectMapperInterceptor(mapper, {
87
+ callback: (inputData: any, outputData: any) => {
88
+ return { ...outputData, z: inputData.a + inputData.b };
89
+ },
90
+ });
91
+
92
+ const instance = new interceptor();
93
+ instance
94
+ .intercept(mockExecutionContext, mockCallHandler)
95
+ .subscribe((result) => {
96
+ expect(result).toEqual({
97
+ x: 'A',
98
+ y: 'B',
99
+ z: 'AB',
100
+ });
101
+ done();
102
+ });
103
+ });
104
+ });