@tsed/core 8.11.1 → 8.12.0-rc.1

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 (259) hide show
  1. package/lib/esm/decorators/storeFn.js +0 -1
  2. package/lib/esm/decorators/storeMerge.js +0 -1
  3. package/lib/esm/decorators/storeSet.js +0 -1
  4. package/lib/esm/errors/UnsupportedDecoratorType.js +0 -1
  5. package/lib/esm/index.js +0 -1
  6. package/lib/esm/types/AnyDecorator.js +0 -1
  7. package/lib/esm/types/DecoratorParameters.js +0 -1
  8. package/lib/esm/types/DecoratorTypes.js +0 -1
  9. package/lib/esm/types/Env.js +0 -1
  10. package/lib/esm/types/Metadata.js +0 -1
  11. package/lib/esm/types/MetadataTypes.js +0 -1
  12. package/lib/esm/types/Relation.js +0 -1
  13. package/lib/esm/types/Store.js +0 -1
  14. package/lib/esm/types/Type.js +0 -1
  15. package/lib/esm/types/ValueOf.js +0 -1
  16. package/lib/esm/utils/AnyToPromise.js +0 -1
  17. package/lib/esm/utils/ancestorOf.js +0 -1
  18. package/lib/esm/utils/ancestorsOf.js +0 -1
  19. package/lib/esm/utils/catchError.js +0 -1
  20. package/lib/esm/utils/classOf.js +0 -1
  21. package/lib/esm/utils/cleanObject.js +0 -1
  22. package/lib/esm/utils/constructorOf.js +0 -1
  23. package/lib/esm/utils/createInstance.js +0 -1
  24. package/lib/esm/utils/decorateMethodsOf.js +0 -1
  25. package/lib/esm/utils/decoratorArgs.js +0 -1
  26. package/lib/esm/utils/decoratorTypeOf.js +0 -1
  27. package/lib/esm/utils/deepClone.js +0 -1
  28. package/lib/esm/utils/deepMerge.js +0 -1
  29. package/lib/esm/utils/descriptorOf.js +0 -1
  30. package/lib/esm/utils/getClassOrSymbol.js +0 -1
  31. package/lib/esm/utils/getConstructorArgNames.js +0 -1
  32. package/lib/esm/utils/getRandomId.js +0 -1
  33. package/lib/esm/utils/getValue.js +0 -1
  34. package/lib/esm/utils/hasJsonMethod.js +0 -1
  35. package/lib/esm/utils/http/getHostInfoFromPort.js +0 -1
  36. package/lib/esm/utils/inheritedDescriptorOf.js +0 -1
  37. package/lib/esm/utils/isArray.js +0 -1
  38. package/lib/esm/utils/isArrowFn.js +0 -1
  39. package/lib/esm/utils/isBoolean.js +0 -1
  40. package/lib/esm/utils/isBuffer.js +0 -1
  41. package/lib/esm/utils/isClass.js +0 -1
  42. package/lib/esm/utils/isCollection.js +0 -1
  43. package/lib/esm/utils/isDate.js +0 -1
  44. package/lib/esm/utils/isEmpty.js +0 -1
  45. package/lib/esm/utils/isEnumerable.js +0 -1
  46. package/lib/esm/utils/isFunction.js +0 -1
  47. package/lib/esm/utils/isInheritedFrom.js +0 -1
  48. package/lib/esm/utils/isMomentObject.js +0 -1
  49. package/lib/esm/utils/isMongooseObject.js +0 -1
  50. package/lib/esm/utils/isNil.js +0 -1
  51. package/lib/esm/utils/isNumber.js +0 -1
  52. package/lib/esm/utils/isObject.js +0 -1
  53. package/lib/esm/utils/isObservable.js +0 -1
  54. package/lib/esm/utils/isPlainObject.js +0 -1
  55. package/lib/esm/utils/isPrimitive.js +0 -1
  56. package/lib/esm/utils/isPromise.js +0 -1
  57. package/lib/esm/utils/isProtectedKey.js +0 -1
  58. package/lib/esm/utils/isRegExp.js +0 -1
  59. package/lib/esm/utils/isSerializable.js +0 -1
  60. package/lib/esm/utils/isStream.js +0 -1
  61. package/lib/esm/utils/isString.js +0 -1
  62. package/lib/esm/utils/isSymbol.js +0 -1
  63. package/lib/esm/utils/methodsOf.js +0 -1
  64. package/lib/esm/utils/nameOf.js +0 -1
  65. package/lib/esm/utils/objectKeys.js +0 -1
  66. package/lib/esm/utils/primitiveOf.js +0 -1
  67. package/lib/esm/utils/prototypeOf.js +0 -1
  68. package/lib/esm/utils/setValue.js +0 -1
  69. package/lib/esm/utils/toMap.js +0 -1
  70. package/lib/esm/utils/toStringConstructor.js +0 -1
  71. package/lib/esm/utils/uniq.js +0 -1
  72. package/lib/esm/utils/useDecorators.js +0 -1
  73. package/lib/esm/utils/useMethodDecorators.js +0 -1
  74. package/package.json +8 -5
  75. package/.barrelsby.json +0 -5
  76. package/lib/esm/decorators/storeFn.js.map +0 -1
  77. package/lib/esm/decorators/storeMerge.js.map +0 -1
  78. package/lib/esm/decorators/storeSet.js.map +0 -1
  79. package/lib/esm/errors/UnsupportedDecoratorType.js.map +0 -1
  80. package/lib/esm/index.js.map +0 -1
  81. package/lib/esm/types/AnyDecorator.js.map +0 -1
  82. package/lib/esm/types/DecoratorParameters.js.map +0 -1
  83. package/lib/esm/types/DecoratorTypes.js.map +0 -1
  84. package/lib/esm/types/Env.js.map +0 -1
  85. package/lib/esm/types/Metadata.js.map +0 -1
  86. package/lib/esm/types/MetadataTypes.js.map +0 -1
  87. package/lib/esm/types/Relation.js.map +0 -1
  88. package/lib/esm/types/Store.js.map +0 -1
  89. package/lib/esm/types/Type.js.map +0 -1
  90. package/lib/esm/types/ValueOf.js.map +0 -1
  91. package/lib/esm/utils/AnyToPromise.js.map +0 -1
  92. package/lib/esm/utils/ancestorOf.js.map +0 -1
  93. package/lib/esm/utils/ancestorsOf.js.map +0 -1
  94. package/lib/esm/utils/catchError.js.map +0 -1
  95. package/lib/esm/utils/classOf.js.map +0 -1
  96. package/lib/esm/utils/cleanObject.js.map +0 -1
  97. package/lib/esm/utils/constructorOf.js.map +0 -1
  98. package/lib/esm/utils/createInstance.js.map +0 -1
  99. package/lib/esm/utils/decorateMethodsOf.js.map +0 -1
  100. package/lib/esm/utils/decoratorArgs.js.map +0 -1
  101. package/lib/esm/utils/decoratorTypeOf.js.map +0 -1
  102. package/lib/esm/utils/deepClone.js.map +0 -1
  103. package/lib/esm/utils/deepMerge.js.map +0 -1
  104. package/lib/esm/utils/descriptorOf.js.map +0 -1
  105. package/lib/esm/utils/getClassOrSymbol.js.map +0 -1
  106. package/lib/esm/utils/getConstructorArgNames.js.map +0 -1
  107. package/lib/esm/utils/getRandomId.js.map +0 -1
  108. package/lib/esm/utils/getValue.js.map +0 -1
  109. package/lib/esm/utils/hasJsonMethod.js.map +0 -1
  110. package/lib/esm/utils/http/getHostInfoFromPort.js.map +0 -1
  111. package/lib/esm/utils/inheritedDescriptorOf.js.map +0 -1
  112. package/lib/esm/utils/isArray.js.map +0 -1
  113. package/lib/esm/utils/isArrowFn.js.map +0 -1
  114. package/lib/esm/utils/isBoolean.js.map +0 -1
  115. package/lib/esm/utils/isBuffer.js.map +0 -1
  116. package/lib/esm/utils/isClass.js.map +0 -1
  117. package/lib/esm/utils/isCollection.js.map +0 -1
  118. package/lib/esm/utils/isDate.js.map +0 -1
  119. package/lib/esm/utils/isEmpty.js.map +0 -1
  120. package/lib/esm/utils/isEnumerable.js.map +0 -1
  121. package/lib/esm/utils/isFunction.js.map +0 -1
  122. package/lib/esm/utils/isInheritedFrom.js.map +0 -1
  123. package/lib/esm/utils/isMomentObject.js.map +0 -1
  124. package/lib/esm/utils/isMongooseObject.js.map +0 -1
  125. package/lib/esm/utils/isNil.js.map +0 -1
  126. package/lib/esm/utils/isNumber.js.map +0 -1
  127. package/lib/esm/utils/isObject.js.map +0 -1
  128. package/lib/esm/utils/isObservable.js.map +0 -1
  129. package/lib/esm/utils/isPlainObject.js.map +0 -1
  130. package/lib/esm/utils/isPrimitive.js.map +0 -1
  131. package/lib/esm/utils/isPromise.js.map +0 -1
  132. package/lib/esm/utils/isProtectedKey.js.map +0 -1
  133. package/lib/esm/utils/isRegExp.js.map +0 -1
  134. package/lib/esm/utils/isSerializable.js.map +0 -1
  135. package/lib/esm/utils/isStream.js.map +0 -1
  136. package/lib/esm/utils/isString.js.map +0 -1
  137. package/lib/esm/utils/isSymbol.js.map +0 -1
  138. package/lib/esm/utils/methodsOf.js.map +0 -1
  139. package/lib/esm/utils/nameOf.js.map +0 -1
  140. package/lib/esm/utils/objectKeys.js.map +0 -1
  141. package/lib/esm/utils/primitiveOf.js.map +0 -1
  142. package/lib/esm/utils/prototypeOf.js.map +0 -1
  143. package/lib/esm/utils/setValue.js.map +0 -1
  144. package/lib/esm/utils/toMap.js.map +0 -1
  145. package/lib/esm/utils/toStringConstructor.js.map +0 -1
  146. package/lib/esm/utils/uniq.js.map +0 -1
  147. package/lib/esm/utils/useDecorators.js.map +0 -1
  148. package/lib/esm/utils/useMethodDecorators.js.map +0 -1
  149. package/src/decorators/storeFn.ts +0 -20
  150. package/src/decorators/storeMerge.ts +0 -8
  151. package/src/decorators/storeSet.ts +0 -8
  152. package/src/errors/UnsupportedDecoratorType.spec.ts +0 -81
  153. package/src/errors/UnsupportedDecoratorType.ts +0 -25
  154. package/src/index.ts +0 -75
  155. package/src/types/AnyDecorator.ts +0 -1
  156. package/src/types/DecoratorParameters.ts +0 -8
  157. package/src/types/DecoratorTypes.ts +0 -10
  158. package/src/types/Env.ts +0 -15
  159. package/src/types/Metadata.spec.ts +0 -142
  160. package/src/types/Metadata.ts +0 -523
  161. package/src/types/MetadataTypes.ts +0 -6
  162. package/src/types/Relation.ts +0 -1
  163. package/src/types/Store.spec.ts +0 -164
  164. package/src/types/Store.ts +0 -175
  165. package/src/types/Type.ts +0 -20
  166. package/src/types/ValueOf.ts +0 -1
  167. package/src/utils/AnyToPromise.spec.ts +0 -159
  168. package/src/utils/AnyToPromise.ts +0 -181
  169. package/src/utils/ObjectUtils.spec.ts +0 -322
  170. package/src/utils/ancestorOf.ts +0 -7
  171. package/src/utils/ancestorsOf.ts +0 -21
  172. package/src/utils/catchError.spec.ts +0 -17
  173. package/src/utils/catchError.ts +0 -15
  174. package/src/utils/classOf.ts +0 -18
  175. package/src/utils/cleanObject.spec.ts +0 -22
  176. package/src/utils/cleanObject.ts +0 -20
  177. package/src/utils/constructorOf.ts +0 -17
  178. package/src/utils/createInstance.ts +0 -5
  179. package/src/utils/decorateMethodsOf.spec.ts +0 -83
  180. package/src/utils/decorateMethodsOf.ts +0 -31
  181. package/src/utils/decoratorArgs.spec.ts +0 -15
  182. package/src/utils/decoratorArgs.ts +0 -6
  183. package/src/utils/decoratorTypeOf.spec.ts +0 -92
  184. package/src/utils/decoratorTypeOf.ts +0 -24
  185. package/src/utils/deepClone.spec.ts +0 -130
  186. package/src/utils/deepClone.ts +0 -85
  187. package/src/utils/deepMerge.spec.ts +0 -223
  188. package/src/utils/deepMerge.ts +0 -85
  189. package/src/utils/descriptorOf.spec.ts +0 -16
  190. package/src/utils/descriptorOf.ts +0 -13
  191. package/src/utils/getClassOrSymbol.ts +0 -6
  192. package/src/utils/getConstructorArgNames.spec.ts +0 -11
  193. package/src/utils/getConstructorArgNames.ts +0 -10
  194. package/src/utils/getRandomId.spec.ts +0 -7
  195. package/src/utils/getRandomId.ts +0 -3
  196. package/src/utils/getValue.spec.ts +0 -62
  197. package/src/utils/getValue.ts +0 -87
  198. package/src/utils/hasJsonMethod.spec.ts +0 -11
  199. package/src/utils/hasJsonMethod.ts +0 -3
  200. package/src/utils/http/getHostInfoFromPort.spec.ts +0 -25
  201. package/src/utils/http/getHostInfoFromPort.ts +0 -20
  202. package/src/utils/inheritedDescriptorOf.ts +0 -13
  203. package/src/utils/isArray.ts +0 -18
  204. package/src/utils/isArrowFn.ts +0 -5
  205. package/src/utils/isBoolean.ts +0 -12
  206. package/src/utils/isBuffer.spec.ts +0 -14
  207. package/src/utils/isBuffer.ts +0 -19
  208. package/src/utils/isClass.spec.ts +0 -15
  209. package/src/utils/isClass.ts +0 -28
  210. package/src/utils/isCollection.ts +0 -20
  211. package/src/utils/isDate.ts +0 -8
  212. package/src/utils/isEmpty.spec.ts +0 -10
  213. package/src/utils/isEmpty.ts +0 -10
  214. package/src/utils/isEnumerable.ts +0 -21
  215. package/src/utils/isFunction.ts +0 -3
  216. package/src/utils/isInheritedFrom.ts +0 -26
  217. package/src/utils/isMomentObject.spec.ts +0 -16
  218. package/src/utils/isMomentObject.ts +0 -3
  219. package/src/utils/isMongooseObject.spec.ts +0 -22
  220. package/src/utils/isMongooseObject.ts +0 -9
  221. package/src/utils/isNil.spec.ts +0 -10
  222. package/src/utils/isNil.ts +0 -3
  223. package/src/utils/isNumber.ts +0 -12
  224. package/src/utils/isObject.ts +0 -3
  225. package/src/utils/isObservable.ts +0 -3
  226. package/src/utils/isPlainObject.spec.ts +0 -10
  227. package/src/utils/isPlainObject.ts +0 -14
  228. package/src/utils/isPrimitive.spec.ts +0 -17
  229. package/src/utils/isPrimitive.ts +0 -26
  230. package/src/utils/isPromise.ts +0 -13
  231. package/src/utils/isProtectedKey.spec.ts +0 -14
  232. package/src/utils/isProtectedKey.ts +0 -7
  233. package/src/utils/isRegExp.ts +0 -3
  234. package/src/utils/isSerializable.spec.ts +0 -14
  235. package/src/utils/isSerializable.ts +0 -9
  236. package/src/utils/isStream.ts +0 -3
  237. package/src/utils/isString.ts +0 -11
  238. package/src/utils/isSymbol.spec.ts +0 -11
  239. package/src/utils/isSymbol.ts +0 -11
  240. package/src/utils/methodsOf.spec.ts +0 -33
  241. package/src/utils/methodsOf.ts +0 -26
  242. package/src/utils/nameOf.ts +0 -27
  243. package/src/utils/objectKeys.spec.ts +0 -10
  244. package/src/utils/objectKeys.ts +0 -5
  245. package/src/utils/primitiveOf.ts +0 -19
  246. package/src/utils/prototypeOf.ts +0 -10
  247. package/src/utils/setValue.spec.ts +0 -60
  248. package/src/utils/setValue.ts +0 -35
  249. package/src/utils/toMap.spec.ts +0 -211
  250. package/src/utils/toMap.ts +0 -44
  251. package/src/utils/toStringConstructor.ts +0 -9
  252. package/src/utils/toStringConstrutor.spec.ts +0 -18
  253. package/src/utils/uniq.spec.ts +0 -8
  254. package/src/utils/uniq.ts +0 -16
  255. package/src/utils/useDecorators.spec.ts +0 -61
  256. package/src/utils/useDecorators.ts +0 -21
  257. package/src/utils/useMethodDecorator.spec.ts +0 -45
  258. package/src/utils/useMethodDecorators.ts +0 -12
  259. package/vitest.config.mts +0 -21
@@ -1,92 +0,0 @@
1
- import {decoratorTypeOf} from "./decoratorTypeOf.js";
2
-
3
- class Test {}
4
-
5
- describe("decoratorTypeOf()", () => {
6
- it("should return class", () => {
7
- expect(decoratorTypeOf([Test])).toBe("class");
8
- });
9
-
10
- it("should return property (static)", () => {
11
- expect(decoratorTypeOf([Test, "props"])).toBe("property.static");
12
- });
13
-
14
- it("should return property (instance)", () => {
15
- expect(decoratorTypeOf([Test.prototype, "props"])).toBe("property");
16
- });
17
-
18
- it("should return property (instance with descriptor from babel)", () => {
19
- expect(
20
- decoratorTypeOf([
21
- Test.prototype,
22
- "props",
23
- {
24
- configurable: true,
25
- enumerable: true,
26
- writable: true,
27
- initializer: null
28
- }
29
- ])
30
- ).toBe("property");
31
- });
32
-
33
- it("should return method (instance, getter)", () => {
34
- expect(
35
- decoratorTypeOf([
36
- Test.prototype,
37
- "props",
38
- {
39
- get: () => {}
40
- }
41
- ])
42
- ).toBe("property");
43
- });
44
-
45
- it("should return method (instance, setter)", () => {
46
- expect(
47
- decoratorTypeOf([
48
- Test.prototype,
49
- "props",
50
- {
51
- set: () => {}
52
- }
53
- ])
54
- ).toBe("property");
55
- });
56
-
57
- it("should return method (static)", () => {
58
- expect(
59
- decoratorTypeOf([
60
- Test,
61
- "props",
62
- {
63
- value: () => {}
64
- }
65
- ])
66
- ).toBe("method.static");
67
- });
68
-
69
- it("should return method (instance)", () => {
70
- expect(
71
- decoratorTypeOf([
72
- Test.prototype,
73
- "props",
74
- {
75
- value: () => {}
76
- }
77
- ])
78
- ).toBe("method");
79
- });
80
-
81
- it("should return params (static)", () => {
82
- expect(decoratorTypeOf([Test, "props", 0])).toBe("parameter.static");
83
- });
84
-
85
- it("should return params (instance)", () => {
86
- expect(decoratorTypeOf([Test.prototype, "props", 0])).toBe("parameter");
87
- });
88
-
89
- it("should return params (constructor)", () => {
90
- expect(decoratorTypeOf([Test.prototype, undefined, 0])).toBe("parameter.constructor");
91
- });
92
- });
@@ -1,24 +0,0 @@
1
- import {DecoratorTypes} from "../types/DecoratorTypes.js";
2
- import {classOf} from "./classOf.js";
3
-
4
- export function decoratorTypeOf(args: any[]): DecoratorTypes {
5
- const [target, propertyKey, descriptor] = args;
6
-
7
- const staticType = (type: string): any => {
8
- return target !== classOf(target) ? type : `${type}.static`;
9
- };
10
-
11
- if (typeof descriptor === "number") {
12
- return propertyKey ? staticType("parameter") : "parameter.constructor";
13
- }
14
-
15
- if (descriptor && descriptor.value) {
16
- return staticType("method");
17
- }
18
-
19
- if ((propertyKey && descriptor === undefined) || descriptor) {
20
- return staticType("property");
21
- }
22
-
23
- return DecoratorTypes.CLASS;
24
- }
@@ -1,130 +0,0 @@
1
- import {deepClone} from "./deepClone.js";
2
-
3
- describe("deepClone", () => {
4
- class Base {
5
- public test2: string;
6
-
7
- constructor(test: string) {
8
- this.test2 = test + "2";
9
- }
10
-
11
- get test3(): string {
12
- return this.test2;
13
- }
14
- }
15
-
16
- class Test extends Base {
17
- constructor(private _test: string) {
18
- super(_test);
19
- }
20
-
21
- get test(): string {
22
- return this._test;
23
- }
24
- }
25
-
26
- it("should clone object", () => {
27
- const externalObject = {
28
- color: "red"
29
- };
30
- const symbol = Symbol("test");
31
- const original = {
32
- a: new Date(),
33
- b: NaN,
34
- c: new Function(),
35
- d: undefined,
36
- e: function name() {},
37
- f: Number,
38
- g: false,
39
- h: Infinity,
40
- i: externalObject,
41
- j: new Test("test"),
42
- k: [new Test("test"), false, 1, Test],
43
- l: new RegExp(/test/i),
44
- m: null,
45
- n: symbol
46
- };
47
-
48
- const cloned = deepClone(original);
49
-
50
- expect(cloned).toEqual(original);
51
- expect(cloned).not.toBe(original);
52
-
53
- expect(cloned.j).toBeInstanceOf(Test);
54
- expect(cloned.j.test).toBe("test");
55
- expect(cloned.j.test2).toBe("test2");
56
-
57
- expect(cloned.k).toBeInstanceOf(Array);
58
- expect(cloned.k.length).toBe(4);
59
- expect(cloned.k[0]).toBeInstanceOf(Test);
60
- expect(cloned.k[3]).toBe(Test);
61
-
62
- expect(cloned.l).toBeInstanceOf(RegExp);
63
- expect(cloned.l.source).toBe("test");
64
- expect(cloned.l.flags).toBe("i");
65
-
66
- expect(cloned.m).toBeNull();
67
- expect(cloned.n).toBe(symbol);
68
-
69
- expect(deepClone(new Test("test"))).toBeInstanceOf(Test);
70
- });
71
-
72
- it("should clone object with circular obj", () => {
73
- const original = {
74
- posts: [
75
- {
76
- id: 0,
77
- user: {}
78
- }
79
- ],
80
- user: {
81
- id: 1,
82
- posts: [] as any[]
83
- }
84
- };
85
-
86
- original.posts[0].user = original.user;
87
- original.user.posts.push(original.posts[0]);
88
-
89
- expect(deepClone(original)).toEqual(original);
90
- });
91
-
92
- it("should clone a buffer", () => {
93
- class FakeStorage {
94
- testBuffer: Buffer;
95
-
96
- constructor() {
97
- this.testBuffer = Buffer.from("Hello");
98
- }
99
-
100
- get() {
101
- return this.testBuffer.toString("utf-8");
102
- }
103
- }
104
-
105
- const result = deepClone({storage: new FakeStorage()});
106
- expect(result.storage.get()).toEqual("Hello");
107
- });
108
-
109
- it("should clone Map, Set, and Array", () => {
110
- class FakeStorage {
111
- set = new Set([new Test("test")]);
112
- map = new Map([["test", Test]]);
113
- array = [new Test("test"), Test, new Map([["test", new Test("test")]])];
114
- }
115
-
116
- const expected = {storage: new FakeStorage()};
117
- const result = deepClone(expected);
118
-
119
- expect(result).toEqual(expected);
120
- expect(result).not.toBe(expected);
121
- });
122
-
123
- it("should clone typed arrays", () => {
124
- const original = new Int8Array([1, 2, 3]);
125
- const result = deepClone(original);
126
-
127
- expect(result).toEqual(original);
128
- expect(result).toBeInstanceOf(Int8Array);
129
- });
130
- });
@@ -1,85 +0,0 @@
1
- import {classOf} from "./classOf.js";
2
- import {isDate} from "./isDate.js";
3
- import {isFunction} from "./isFunction.js";
4
- import {isNil} from "./isNil.js";
5
- import {isPrimitive} from "./isPrimitive.js";
6
- import {isRegExp} from "./isRegExp.js";
7
- import {isSymbol} from "./isSymbol.js";
8
-
9
- const isBasicType = (source: any) => isNil(source) || isPrimitive(source) || isSymbol(source) || isFunction(source);
10
-
11
- /**
12
- * Return a cloned value
13
- * @param source
14
- * @param stack
15
- */
16
- export const deepClone = (source: any, stack = new WeakMap()): any => {
17
- // provides an early exit for simple cases
18
- if (isBasicType(source)) {
19
- return source;
20
- }
21
-
22
- const stacked = stack.get(source);
23
-
24
- if (stacked) {
25
- // See issue #1619
26
- return stacked;
27
- }
28
-
29
- if (ArrayBuffer.isView(source)) {
30
- return Buffer.isBuffer(source)
31
- ? Buffer.from(source)
32
- : // adds support for all kind of TypedArray such as Int8Array, Uint8Array, etc
33
- new (classOf(source))(source.buffer.slice(0), source.byteOffset, source.byteLength);
34
- }
35
-
36
- if (isDate(source)) {
37
- return new Date(source);
38
- }
39
-
40
- if (isRegExp(source)) {
41
- return new RegExp(source);
42
- }
43
-
44
- if (Array.isArray(source)) {
45
- const clone: unknown[] = [];
46
- stack.set(source, clone);
47
- source.forEach((item, idx) => (clone[idx] = deepClone(item, stack)));
48
- return clone;
49
- }
50
-
51
- if (source instanceof Map) {
52
- const clone = new Map();
53
- stack.set(source, clone);
54
- source.forEach((value, key) => clone.set(deepClone(key, stack), deepClone(value, stack)));
55
- return clone;
56
- }
57
-
58
- if (source instanceof Set) {
59
- const clone = new Set();
60
- stack.set(source, clone);
61
- source.forEach((value) => clone.add(deepClone(value, stack)));
62
- return clone;
63
- }
64
-
65
- const clone = Object.create(Reflect.getPrototypeOf(source));
66
- stack.set(source, clone);
67
-
68
- Reflect.ownKeys(source).forEach((key) => {
69
- // respects property descriptors and the prototype chain more explicitly, which is important for objects with getter/setter.
70
- const descriptor = Object.getOwnPropertyDescriptor(source, key);
71
-
72
- if (descriptor) {
73
- if (!isFunction(descriptor.value)) {
74
- Object.defineProperty(clone, key, {
75
- ...descriptor,
76
- value: deepClone(descriptor.value, stack)
77
- });
78
- } else {
79
- Object.defineProperty(clone, key, descriptor);
80
- }
81
- }
82
- });
83
-
84
- return clone;
85
- };
@@ -1,223 +0,0 @@
1
- import {deepMerge} from "./deepMerge.js";
2
-
3
- describe("deepMerge", () => {
4
- describe("without reducers", () => {
5
- describe("when is an object", () => {
6
- it("should merge data (1)", () => {
7
- expect(
8
- deepMerge(
9
- {
10
- security: ["o"]
11
- },
12
- undefined
13
- )
14
- ).toEqual({
15
- security: ["o"]
16
- });
17
- });
18
- it("should merge data (2)", () => {
19
- const klass = class {
20
- test = "test";
21
- };
22
- const result = deepMerge(
23
- {
24
- security: ["o"]
25
- },
26
- {
27
- security: ["o", "o1"],
28
- withClass: new klass()
29
- }
30
- );
31
- expect(result).toEqual({
32
- security: ["o", "o1"],
33
- withClass: {
34
- test: "test"
35
- }
36
- });
37
-
38
- expect(result.withClass).toBeInstanceOf(klass);
39
- });
40
- it("should merge data (3)", () => {
41
- expect(
42
- deepMerge(
43
- {
44
- security: [{"1": "o"}]
45
- },
46
- {
47
- security: [{"1": "o"}, {"2": "o1"}]
48
- }
49
- )
50
- ).toEqual({
51
- security: [{"1": "o"}, {"1": "o"}, {"2": "o1"}]
52
- });
53
- });
54
- it("should merge data (4)", () => {
55
- const alter = vi.fn().mockImplementation((key, value) => {
56
- return value;
57
- });
58
- expect(
59
- deepMerge(
60
- {
61
- prop: {}
62
- },
63
- {
64
- prop: ""
65
- },
66
- {
67
- alter
68
- }
69
- )
70
- ).toEqual({
71
- prop: {}
72
- });
73
-
74
- expect(alter).toHaveBeenCalledWith("prop", {});
75
- });
76
- it("should alter nested properties", () => {
77
- const alter = vi.fn().mockImplementation((key, value) => {
78
- return key === "nested" ? {...value, extra: true} : value;
79
- });
80
- expect(
81
- deepMerge(
82
- {
83
- prop: {nested: {a: 1}}
84
- },
85
- {
86
- prop: {nested: {b: 2}}
87
- },
88
- {alter}
89
- )
90
- ).toEqual({
91
- prop: {nested: {a: 1, b: 2, extra: true}}
92
- });
93
- expect(alter).toHaveBeenCalledWith("nested", {a: 1, b: 2});
94
- });
95
- it("should merge data and prevent prototype pollution", () => {
96
- const obj = JSON.parse('{"__proto__": {"a": "vulnerable"}, "security": [{"1": "o"}, {"2": "o1"}]}');
97
-
98
- expect(
99
- deepMerge(
100
- {
101
- security: [{"1": "o"}]
102
- },
103
- obj
104
- )
105
- ).toEqual({
106
- security: [{"1": "o"}, {"1": "o"}, {"2": "o1"}]
107
- });
108
-
109
- expect(({} as any).a).toBeUndefined();
110
- });
111
- });
112
-
113
- describe("when is an array", () => {
114
- it("should merge data (1)", () => {
115
- expect(deepMerge(["1", "2", "4"], ["1", "2", "3"])).toEqual(["1", "2", "4", "3"]);
116
- });
117
-
118
- it("should merge data (2)", () => {
119
- expect(deepMerge([{"1": "1"}, {"2": "2"}, {"4": "4"}], [{"1": "1"}, {"2": "2"}, {"3": "3"}])).toEqual([
120
- {"1": "1"},
121
- {"2": "2"},
122
- {"4": "4"},
123
- {"1": "1"},
124
- {"2": "2"},
125
- {"3": "3"}
126
- ]);
127
- });
128
- });
129
- });
130
-
131
- describe("with reducers", () => {
132
- describe("when is an object", () => {
133
- it("should merge data", () => {
134
- expect(
135
- deepMerge(
136
- {
137
- security: ["o"]
138
- },
139
- {
140
- security: ["o", "o1"]
141
- },
142
- {
143
- reducers: {
144
- default: (collection, value) => {
145
- if (collection.indexOf(value) === -1) {
146
- collection.push(value);
147
- }
148
-
149
- return collection;
150
- }
151
- }
152
- }
153
- )
154
- ).toEqual({
155
- security: ["o", "o1"]
156
- });
157
- });
158
- it("should merge data (2)", () => {
159
- expect(
160
- deepMerge(
161
- {
162
- parameters: [
163
- {in: "test", name: "get", description: "test"},
164
- {
165
- in: "test",
166
- name: "post",
167
- description: "test"
168
- }
169
- ]
170
- },
171
- {
172
- parameters: [
173
- {in: "test", name: "get", description: "test2"},
174
- {
175
- in: "test",
176
- name: "util",
177
- description: "test2"
178
- }
179
- ]
180
- },
181
- {
182
- reducers: {
183
- parameters: (collection, value, options) => {
184
- const index = collection.findIndex((current) => current.in === value.in && current.name === value.name);
185
-
186
- if (index === -1) {
187
- return [...collection, value];
188
- }
189
-
190
- collection[index] = deepMerge(collection[index], value, options);
191
-
192
- return collection;
193
- }
194
- }
195
- }
196
- )
197
- ).toEqual({
198
- parameters: [
199
- {in: "test", name: "get", description: "test2"},
200
- {in: "test", name: "post", description: "test"},
201
- {in: "test", name: "util", description: "test2"}
202
- ]
203
- });
204
- });
205
- });
206
-
207
- describe("when is an array", () => {
208
- it("should merge data", () => {
209
- expect(deepMerge(["1", "2", "4"], ["1", "2", "3"])).toEqual(["1", "2", "4", "3"]);
210
- });
211
- it("should merge data (2)", () => {
212
- expect(deepMerge([{"1": "1"}, {"2": "2"}, {"4": "4"}], [{"1": "1"}, {"2": "2"}, {"3": "3"}])).toEqual([
213
- {"1": "1"},
214
- {"2": "2"},
215
- {"4": "4"},
216
- {"1": "1"},
217
- {"2": "2"},
218
- {"3": "3"}
219
- ]);
220
- });
221
- });
222
- });
223
- });
@@ -1,85 +0,0 @@
1
- import {createInstance} from "./createInstance.js";
2
- import {isFunction} from "./isFunction.js";
3
- import {isPrimitive} from "./isPrimitive.js";
4
- import {isSymbol} from "./isSymbol.js";
5
- import {objectKeys} from "./objectKeys.js";
6
-
7
- export type DeepMergeReducerCB = (collection: any[], value: any, options?: DeepMergeOptions) => any;
8
- export type DeepMergeComparatorCB = (item: any, ref: any) => boolean;
9
-
10
- export interface DeepMergeOptions {
11
- reducers?: Record<string, DeepMergeReducerCB>;
12
- parentKey?: string;
13
- cleanUndefinedProps?: boolean;
14
- alter?: (key: string, value: any) => any;
15
- }
16
-
17
- export function mergeReducerBuilder(cb: DeepMergeComparatorCB) {
18
- return (collection: any[], value: any, options: DeepMergeOptions) => {
19
- const index = collection.findIndex((item) => cb(item, value));
20
-
21
- if (index === -1) {
22
- return [...collection, value];
23
- }
24
-
25
- collection[index] = deepMerge(collection[index], value, options);
26
-
27
- return collection;
28
- };
29
- }
30
-
31
- const defaultReducer = mergeReducerBuilder((a, b) => a === b);
32
-
33
- function getReducer({reducers, parentKey}: DeepMergeOptions) {
34
- if (!reducers) {
35
- return defaultReducer;
36
- }
37
-
38
- if (parentKey && reducers[parentKey]) {
39
- return reducers[parentKey];
40
- }
41
-
42
- return reducers["default"] || defaultReducer;
43
- }
44
-
45
- function shouldReturnObj(obj: any, source: any) {
46
- return isPrimitive(obj) || isSymbol(obj) || isFunction(obj) || source === undefined;
47
- }
48
-
49
- function shouldReturnSource(obj: any, source: any) {
50
- return obj === undefined || obj === null || (obj === "" && source !== "");
51
- }
52
-
53
- export function deepMerge<T = any, C = any>(source: T & any, obj: C & any, options: DeepMergeOptions = {}): (T & C) | undefined | null {
54
- if (shouldReturnSource(obj, source)) {
55
- return source as any;
56
- }
57
-
58
- if (shouldReturnObj(obj, source)) {
59
- return obj;
60
- }
61
-
62
- if (Array.isArray(source)) {
63
- const reducer = getReducer(options);
64
-
65
- return [].concat(obj).reduce((out: any[], value: any) => reducer(out, value, options), [...source]);
66
- }
67
-
68
- const newObj = createInstance(source);
69
-
70
- return [...objectKeys(source), ...objectKeys(obj)].reduce((out: any, key: string) => {
71
- const src = source && source[key];
72
- const value = deepMerge(src, obj && obj[key], {
73
- ...options,
74
- parentKey: key
75
- });
76
-
77
- if (options.cleanUndefinedProps && value === undefined) {
78
- return out;
79
- }
80
-
81
- out[key] = options.alter?.(key, value) ?? value;
82
-
83
- return out;
84
- }, newObj);
85
- }
@@ -1,16 +0,0 @@
1
- import {isMethodDescriptor} from "./descriptorOf.js";
2
-
3
- describe("isMethodDescriptor()", () => {
4
- it("should return true", () => {
5
- class Test {
6
- test() {}
7
- }
8
- expect(isMethodDescriptor(Test, "test")).toBeTruthy();
9
- });
10
- it("should return false", () => {
11
- class Test {
12
- test: string;
13
- }
14
- expect(isMethodDescriptor(Test, "test")).toBeFalsy();
15
- });
16
- });
@@ -1,13 +0,0 @@
1
- /**
2
- * Return the descriptor for a given class and propertyKey
3
- * @param target
4
- * @param {string} propertyKey
5
- * @returns {PropertyDescriptor}
6
- */
7
- export function descriptorOf(target: any, propertyKey: string | symbol): PropertyDescriptor {
8
- return Reflect.getOwnPropertyDescriptor((target && target.prototype) || target, propertyKey)!;
9
- }
10
-
11
- export function isMethodDescriptor(target: any, propertyKey: string | symbol) {
12
- return descriptorOf(target, propertyKey)?.value;
13
- }
@@ -1,6 +0,0 @@
1
- import {classOf} from "./classOf.js";
2
- import {isClass} from "./isClass.js";
3
-
4
- export function getClassOrSymbol(target: any): any {
5
- return isClass(target) ? classOf(target) : target;
6
- }
@@ -1,11 +0,0 @@
1
- import {getConstructorArgNames} from "./getConstructorArgNames.js";
2
-
3
- describe("getConstructorArgNames", () => {
4
- it("should return the constructor args names", () => {
5
- class Test {
6
- constructor(test: string) {}
7
- }
8
-
9
- expect(getConstructorArgNames(Test)).toEqual(["test"]);
10
- });
11
- });
@@ -1,10 +0,0 @@
1
- import {toStringConstructor} from "./toStringConstructor.js";
2
-
3
- export function getConstructorArgNames(target: any) {
4
- return toStringConstructor(target)
5
- .split("constructor(")[1]
6
- .split(")")[0]
7
- .split(", ")
8
- .filter(Boolean)
9
- .map((s) => s.trim());
10
- }