@tsed/core 8.0.0 → 8.0.2

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 (110) hide show
  1. package/package.json +5 -4
  2. package/src/decorators/storeFn.ts +20 -0
  3. package/src/decorators/storeMerge.ts +8 -0
  4. package/src/decorators/storeSet.ts +8 -0
  5. package/src/domain/AnyToPromise.spec.ts +158 -0
  6. package/src/domain/AnyToPromise.ts +181 -0
  7. package/src/domain/DecoratorTypes.ts +10 -0
  8. package/src/domain/Env.ts +15 -0
  9. package/src/domain/Metadata.spec.ts +142 -0
  10. package/src/domain/Metadata.ts +523 -0
  11. package/src/domain/Store.spec.ts +164 -0
  12. package/src/domain/Store.ts +175 -0
  13. package/src/domain/Type.ts +20 -0
  14. package/src/errors/UnsupportedDecoratorType.spec.ts +81 -0
  15. package/src/errors/UnsupportedDecoratorType.ts +25 -0
  16. package/src/index.ts +75 -0
  17. package/src/interfaces/AnyDecorator.ts +1 -0
  18. package/src/interfaces/DecoratorParameters.ts +8 -0
  19. package/src/interfaces/MetadataTypes.ts +6 -0
  20. package/src/interfaces/Relation.ts +1 -0
  21. package/src/interfaces/ValueOf.ts +1 -0
  22. package/src/utils/catchError.spec.ts +17 -0
  23. package/src/utils/catchError.ts +15 -0
  24. package/src/utils/decorators/decorateMethodsOf.spec.ts +83 -0
  25. package/src/utils/decorators/decorateMethodsOf.ts +31 -0
  26. package/src/utils/decorators/decoratorArgs.spec.ts +15 -0
  27. package/src/utils/decorators/decoratorArgs.ts +6 -0
  28. package/src/utils/decorators/decoratorTypeOf.spec.ts +92 -0
  29. package/src/utils/decorators/decoratorTypeOf.ts +24 -0
  30. package/src/utils/decorators/inheritedDescriptorOf.ts +13 -0
  31. package/src/utils/decorators/useDecorators.spec.ts +61 -0
  32. package/src/utils/decorators/useDecorators.ts +21 -0
  33. package/src/utils/decorators/useMethodDecorator.spec.ts +45 -0
  34. package/src/utils/decorators/useMethodDecorators.ts +12 -0
  35. package/src/utils/getRandomId.spec.ts +7 -0
  36. package/src/utils/getRandomId.ts +3 -0
  37. package/src/utils/http/getHostInfoFromPort.spec.ts +25 -0
  38. package/src/utils/http/getHostInfoFromPort.ts +20 -0
  39. package/src/utils/objects/ObjectUtils.spec.ts +322 -0
  40. package/src/utils/objects/ancestorOf.ts +7 -0
  41. package/src/utils/objects/ancestorsOf.ts +21 -0
  42. package/src/utils/objects/classOf.ts +18 -0
  43. package/src/utils/objects/cleanObject.spec.ts +22 -0
  44. package/src/utils/objects/cleanObject.ts +20 -0
  45. package/src/utils/objects/constructorOf.ts +17 -0
  46. package/src/utils/objects/createInstance.ts +5 -0
  47. package/src/utils/objects/deepClone.spec.ts +130 -0
  48. package/src/utils/objects/deepClone.ts +85 -0
  49. package/src/utils/objects/deepMerge.spec.ts +196 -0
  50. package/src/utils/objects/deepMerge.ts +84 -0
  51. package/src/utils/objects/descriptorOf.spec.ts +16 -0
  52. package/src/utils/objects/descriptorOf.ts +13 -0
  53. package/src/utils/objects/getClassOrSymbol.ts +6 -0
  54. package/src/utils/objects/getConstructorArgNames.spec.ts +11 -0
  55. package/src/utils/objects/getConstructorArgNames.ts +10 -0
  56. package/src/utils/objects/getValue.spec.ts +62 -0
  57. package/src/utils/objects/getValue.ts +87 -0
  58. package/src/utils/objects/hasJsonMethod.spec.ts +11 -0
  59. package/src/utils/objects/hasJsonMethod.ts +3 -0
  60. package/src/utils/objects/isArray.ts +18 -0
  61. package/src/utils/objects/isArrowFn.ts +5 -0
  62. package/src/utils/objects/isBoolean.ts +12 -0
  63. package/src/utils/objects/isBuffer.spec.ts +14 -0
  64. package/src/utils/objects/isBuffer.ts +19 -0
  65. package/src/utils/objects/isClass.spec.ts +15 -0
  66. package/src/utils/objects/isClass.ts +28 -0
  67. package/src/utils/objects/isCollection.ts +20 -0
  68. package/src/utils/objects/isDate.ts +8 -0
  69. package/src/utils/objects/isEmpty.spec.ts +10 -0
  70. package/src/utils/objects/isEmpty.ts +10 -0
  71. package/src/utils/objects/isEnumerable.ts +21 -0
  72. package/src/utils/objects/isFunction.ts +3 -0
  73. package/src/utils/objects/isInheritedFrom.ts +26 -0
  74. package/src/utils/objects/isMomentObject.spec.ts +16 -0
  75. package/src/utils/objects/isMomentObject.ts +3 -0
  76. package/src/utils/objects/isMongooseObject.spec.ts +22 -0
  77. package/src/utils/objects/isMongooseObject.ts +9 -0
  78. package/src/utils/objects/isNil.spec.ts +10 -0
  79. package/src/utils/objects/isNil.ts +3 -0
  80. package/src/utils/objects/isNumber.ts +12 -0
  81. package/src/utils/objects/isObject.ts +3 -0
  82. package/src/utils/objects/isObservable.ts +3 -0
  83. package/src/utils/objects/isPlainObject.spec.ts +10 -0
  84. package/src/utils/objects/isPlainObject.ts +14 -0
  85. package/src/utils/objects/isPrimitive.spec.ts +17 -0
  86. package/src/utils/objects/isPrimitive.ts +26 -0
  87. package/src/utils/objects/isPromise.ts +13 -0
  88. package/src/utils/objects/isProtectedKey.spec.ts +14 -0
  89. package/src/utils/objects/isProtectedKey.ts +7 -0
  90. package/src/utils/objects/isRegExp.ts +3 -0
  91. package/src/utils/objects/isSerializable.spec.ts +14 -0
  92. package/src/utils/objects/isSerializable.ts +9 -0
  93. package/src/utils/objects/isStream.ts +3 -0
  94. package/src/utils/objects/isString.ts +11 -0
  95. package/src/utils/objects/isSymbol.spec.ts +11 -0
  96. package/src/utils/objects/isSymbol.ts +11 -0
  97. package/src/utils/objects/methodsOf.ts +25 -0
  98. package/src/utils/objects/nameOf.ts +27 -0
  99. package/src/utils/objects/objectKeys.spec.ts +10 -0
  100. package/src/utils/objects/objectKeys.ts +5 -0
  101. package/src/utils/objects/primitiveOf.ts +19 -0
  102. package/src/utils/objects/prototypeOf.ts +10 -0
  103. package/src/utils/objects/setValue.spec.ts +60 -0
  104. package/src/utils/objects/setValue.ts +35 -0
  105. package/src/utils/objects/toMap.spec.ts +211 -0
  106. package/src/utils/objects/toMap.ts +44 -0
  107. package/src/utils/objects/toStringConstructor.ts +9 -0
  108. package/src/utils/objects/toStringConstrutor.spec.ts +18 -0
  109. package/src/utils/objects/uniq.spec.ts +8 -0
  110. package/src/utils/objects/uniq.ts +16 -0
@@ -0,0 +1,87 @@
1
+ import {isFunction} from "./isFunction.js";
2
+ import {isObject} from "./isObject.js";
3
+ import {isString} from "./isString.js";
4
+
5
+ const mapOptions = (args: any[]) => {
6
+ const [expression, scope, defaultValue, separator = "."] = args;
7
+
8
+ if (isObject(scope) || isString(expression)) {
9
+ return {
10
+ expression,
11
+ scope,
12
+ defaultValue,
13
+ separator
14
+ };
15
+ }
16
+
17
+ return {
18
+ expression: scope,
19
+ scope: expression,
20
+ defaultValue,
21
+ separator
22
+ };
23
+ };
24
+
25
+ /**
26
+ * Get value from scope
27
+ * @param {string} expression
28
+ * @param scope
29
+ * @returns {any}
30
+ */
31
+ export function getValue<T = any>(scope: any, expression: string | undefined): T | undefined;
32
+ /**
33
+ * Get value from scope
34
+ * @param {string} expression
35
+ * @param scope
36
+ * @param defaultValue
37
+ * @param separator
38
+ * @returns {any}
39
+ */
40
+ export function getValue<T = any>(scope: any, expression: string | undefined, defaultValue: T, separator?: string): T;
41
+ /**
42
+ * Get value from scope
43
+ * @param {string} expression
44
+ * @param scope
45
+ * @returns {any}
46
+ */
47
+ export function getValue<T = any>(expression: string | undefined, scope: any): T | undefined;
48
+ /**
49
+ * Get value from scope
50
+ * @param {string} expression
51
+ * @param scope
52
+ * @param defaultValue
53
+ * @param separator
54
+ * @returns {any}
55
+ */
56
+ export function getValue<T = any>(expression: string | undefined, scope: any, defaultValue: T, separator?: string): T;
57
+ export function getValue(...args: any[]) {
58
+ const options = mapOptions(args);
59
+ let scope = options.scope;
60
+ const {expression, defaultValue, separator} = options;
61
+
62
+ if (!expression) {
63
+ return scope;
64
+ }
65
+
66
+ if (!scope) {
67
+ return defaultValue;
68
+ }
69
+
70
+ const keys: string[] = expression.split(separator);
71
+
72
+ const getValue = (key: string) => {
73
+ if (scope) {
74
+ if (scope[key] !== undefined || (isObject(scope) && key in scope)) {
75
+ return scope[key];
76
+ }
77
+
78
+ if (isFunction(scope.get)) {
79
+ return scope.get(key);
80
+ }
81
+ }
82
+ };
83
+
84
+ while ((scope = getValue(keys.shift()!)) && keys.length) {}
85
+
86
+ return scope === undefined ? defaultValue : scope;
87
+ }
@@ -0,0 +1,11 @@
1
+ import {hasJsonMethod} from "./hasJsonMethod.js";
2
+
3
+ describe("hasJsonMethod", () => {
4
+ it("should return true if object has toJSON method", () => {
5
+ expect(hasJsonMethod({toJSON: () => {}})).toEqual(true);
6
+ });
7
+
8
+ it("should return false if object hasn't toJSON method", () => {
9
+ expect(hasJsonMethod({})).toEqual(false);
10
+ });
11
+ });
@@ -0,0 +1,3 @@
1
+ export function hasJsonMethod(obj: any) {
2
+ return obj && typeof obj.toJSON === "function";
3
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Return true if value is an array
3
+ * @param target
4
+ * @returns {boolean}
5
+ */
6
+ export function isArray<T = any>(target: unknown): target is T[] {
7
+ return Array.isArray(target);
8
+ }
9
+
10
+ /**
11
+ * Return true if the clazz is an array.
12
+ * @param target
13
+ * @returns {boolean}
14
+ * @ignore
15
+ */
16
+ export function isArrayOrArrayClass<T = any>(target: unknown): target is T[] {
17
+ return target === Array || isArray(target);
18
+ }
@@ -0,0 +1,5 @@
1
+ import {isFunction} from "./isFunction.js";
2
+
3
+ export function isArrowFn(target: any): target is Function {
4
+ return target && isFunction(target) && !target.prototype;
5
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ *
3
+ * @param target
4
+ * @returns {boolean}
5
+ */
6
+ export function isBoolean(target: any): target is boolean {
7
+ return typeof target === "boolean" || target instanceof Boolean || target === Boolean;
8
+ }
9
+
10
+ export function isBooleanOrBooleanClass(target: any): target is boolean {
11
+ return typeof target === "boolean" || target instanceof Boolean || target === Boolean;
12
+ }
@@ -0,0 +1,14 @@
1
+ import {isBuffer, isUint8Array} from "./isBuffer.js";
2
+
3
+ describe("isBuffer", () => {
4
+ it("should return true", () => {
5
+ expect(isBuffer(Buffer.from(""))).toBeTruthy();
6
+ expect(isBuffer(Buffer)).toBeTruthy();
7
+ expect(isUint8Array(Uint8Array)).toBeTruthy();
8
+ expect(isUint8Array(Uint8Array.from([0, 1]))).toBeTruthy();
9
+ });
10
+ it("should return false", () => {
11
+ expect(isBuffer({})).toBeFalsy();
12
+ expect(isBuffer(undefined)).toBeFalsy();
13
+ });
14
+ });
@@ -0,0 +1,19 @@
1
+ import {nameOf} from "./nameOf.js";
2
+
3
+ /**
4
+ * Tests to see if the object is Buffer
5
+ * @param target
6
+ * @returns {boolean}
7
+ */
8
+ export function isBuffer(target: any): target is Buffer {
9
+ // is Class
10
+ if (target && "isBuffer" in target && typeof target.isBuffer === "function") {
11
+ return true;
12
+ }
13
+
14
+ return isUint8Array(target);
15
+ }
16
+
17
+ export function isUint8Array(target: any): target is Uint8Array {
18
+ return !!(target && (target === Uint8Array || target instanceof Uint8Array));
19
+ }
@@ -0,0 +1,15 @@
1
+ import {isClass} from "./isClass.js";
2
+
3
+ describe("isClass", () => {
4
+ it("should test if a value is a class", () => {
5
+ expect(isClass(class Test {})).toEqual(true);
6
+ expect(isClass(Date)).toEqual(false);
7
+ expect(isClass(undefined)).toEqual(false);
8
+ expect(isClass(Symbol("zertyuio"))).toEqual(false);
9
+ expect(isClass(Number)).toEqual(false);
10
+ expect(isClass(Promise)).toEqual(false);
11
+ expect(isClass(Array)).toEqual(false);
12
+ expect(isClass(Buffer)).toEqual(false);
13
+ expect(isClass(() => {})).toEqual(false);
14
+ });
15
+ });
@@ -0,0 +1,28 @@
1
+ import {isArrayOrArrayClass} from "./isArray.js";
2
+ import {isArrowFn} from "./isArrowFn.js";
3
+ import {isBuffer} from "./isBuffer.js";
4
+ import {isDate} from "./isDate.js";
5
+ import {isClassObject} from "./isPlainObject.js";
6
+ import {isPrimitiveOrPrimitiveClass} from "./isPrimitive.js";
7
+ import {isPromise} from "./isPromise.js";
8
+ import {isSymbol} from "./isSymbol.js";
9
+
10
+ export function isClass(target: any) {
11
+ if (!target) {
12
+ return false;
13
+ }
14
+
15
+ if (isArrowFn(target)) {
16
+ return false;
17
+ }
18
+
19
+ return !(
20
+ isSymbol(target) ||
21
+ isPrimitiveOrPrimitiveClass(target) ||
22
+ isClassObject(target) ||
23
+ isDate(target) ||
24
+ isPromise(target) ||
25
+ isArrayOrArrayClass(target) ||
26
+ isBuffer(target)
27
+ );
28
+ }
@@ -0,0 +1,20 @@
1
+ import {isArrayOrArrayClass} from "./isArray.js";
2
+
3
+ /**
4
+ * Return true if the target.
5
+ * @param target
6
+ * @returns {boolean}
7
+ */
8
+ export function isCollection(target: any): boolean {
9
+ return (
10
+ isArrayOrArrayClass(target) ||
11
+ target === Map ||
12
+ target instanceof Map ||
13
+ target === Set ||
14
+ target instanceof Set ||
15
+ target === WeakMap ||
16
+ target instanceof WeakMap ||
17
+ target === WeakSet ||
18
+ target instanceof WeakSet
19
+ );
20
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ *
3
+ * @param target
4
+ * @returns {boolean}
5
+ */
6
+ export function isDate(target: any): target is Date {
7
+ return target === Date || (target instanceof Date && !isNaN(+target));
8
+ }
@@ -0,0 +1,10 @@
1
+ import {isEmpty} from "./isEmpty.js";
2
+
3
+ describe("isEmpty", () => {
4
+ it("should return true", () => {
5
+ expect(isEmpty(null)).toBe(true);
6
+ expect(isEmpty(undefined)).toBe(true);
7
+ expect(isEmpty(1)).toBe(false);
8
+ expect(isEmpty("")).toBe(true);
9
+ });
10
+ });
@@ -0,0 +1,10 @@
1
+ import {isNil} from "./isNil.js";
2
+
3
+ /**
4
+ * Return true if the value is an empty string, null or undefined.
5
+ * @param value
6
+ * @returns {boolean}
7
+ */
8
+ export function isEmpty(value: any): boolean {
9
+ return value === "" || isNil(value);
10
+ }
@@ -0,0 +1,21 @@
1
+ import {inheritedDescriptorOf} from "../decorators/inheritedDescriptorOf.js";
2
+ import {classOf} from "./classOf.js";
3
+
4
+ /**
5
+ *
6
+ * @param obj
7
+ * @param key
8
+ */
9
+ export function isEnumerable(obj: any, key: string) {
10
+ const klass = classOf(obj);
11
+
12
+ if (klass) {
13
+ const descriptor = inheritedDescriptorOf(klass, key);
14
+
15
+ if (descriptor) {
16
+ return descriptor.enumerable;
17
+ }
18
+ }
19
+
20
+ return Object.prototype.propertyIsEnumerable.call(obj, key);
21
+ }
@@ -0,0 +1,3 @@
1
+ export function isFunction(target: any): target is Function {
2
+ return typeof target === "function";
3
+ }
@@ -0,0 +1,26 @@
1
+ import {ancestorOf} from "./ancestorOf.js";
2
+ import {classOf} from "./classOf.js";
3
+ import {nameOf} from "./nameOf.js";
4
+
5
+ export function isInheritedFrom(target: any, from: any, deep = 5): boolean {
6
+ if (!target || !from) {
7
+ return false;
8
+ }
9
+
10
+ target = classOf(target);
11
+ from = classOf(from);
12
+
13
+ while (nameOf(target) !== "") {
14
+ if (!deep) {
15
+ return false;
16
+ }
17
+ if (target === from) {
18
+ return true;
19
+ }
20
+
21
+ target = ancestorOf(target);
22
+ deep--;
23
+ }
24
+
25
+ return false;
26
+ }
@@ -0,0 +1,16 @@
1
+ import {isMomentObject} from "./isMomentObject.js";
2
+
3
+ describe("isMomentObject", () => {
4
+ it("should return true if object is moment object", () => {
5
+ expect(
6
+ isMomentObject({
7
+ _isAMomentObject: true
8
+ })
9
+ ).toEqual(true);
10
+ });
11
+
12
+ it("should return false if object is not moment object", () => {
13
+ expect(isMomentObject({})).toEqual(false);
14
+ expect(isMomentObject(null)).toEqual(false);
15
+ });
16
+ });
@@ -0,0 +1,3 @@
1
+ export function isMomentObject(obj: any) {
2
+ return !!(obj && obj?._isAMomentObject);
3
+ }
@@ -0,0 +1,22 @@
1
+ import {isMongooseObject} from "./isMongooseObject.js";
2
+
3
+ describe("isMongooseObject", () => {
4
+ it("should return true if object is mongoose object", () => {
5
+ expect(
6
+ isMongooseObject({
7
+ toJSON: () => {},
8
+ $isMongooseModelPrototype: true
9
+ })
10
+ ).toEqual(true);
11
+ expect(isMongooseObject({_bsontype: true})).toEqual(true);
12
+ });
13
+ it("should return false if object is not mongoose object", () => {
14
+ expect(
15
+ isMongooseObject({
16
+ toJSON: () => {}
17
+ })
18
+ ).toEqual(false);
19
+ expect(isMongooseObject({})).toEqual(false);
20
+ expect(isMongooseObject(null)).toEqual(false);
21
+ });
22
+ });
@@ -0,0 +1,9 @@
1
+ import {hasJsonMethod} from "./hasJsonMethod.js";
2
+
3
+ export function isObjectID(obj: any) {
4
+ return obj && obj._bsontype;
5
+ }
6
+
7
+ export function isMongooseObject(obj: any) {
8
+ return !!((hasJsonMethod(obj) && obj.$isMongooseModelPrototype) || isObjectID(obj));
9
+ }
@@ -0,0 +1,10 @@
1
+ import {isNil} from "./isNil.js";
2
+
3
+ describe("isNil", () => {
4
+ it("should return true", () => {
5
+ expect(isNil(null)).toBe(true);
6
+ expect(isNil(undefined)).toBe(true);
7
+ expect(isNil(1)).toBe(false);
8
+ expect(isNil("")).toBe(false);
9
+ });
10
+ });
@@ -0,0 +1,3 @@
1
+ export function isNil(value: any): boolean {
2
+ return value === undefined || value === null;
3
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ *
3
+ * @param target
4
+ * @returns {boolean}
5
+ */
6
+ export function isNumber(target: any): target is number {
7
+ return typeof target === "number";
8
+ }
9
+
10
+ export function isNumberOrNumberClass(target: any): target is number {
11
+ return typeof target === "number" || target instanceof Number || target === Number;
12
+ }
@@ -0,0 +1,3 @@
1
+ export function isObject(target: any): target is object {
2
+ return typeof target === "object";
3
+ }
@@ -0,0 +1,3 @@
1
+ export function isObservable<T>(obj: any): boolean {
2
+ return !!obj && typeof obj.lift === "function" && typeof obj.subscribe === "function";
3
+ }
@@ -0,0 +1,10 @@
1
+ import {isPlainObject} from "./isPlainObject.js";
2
+
3
+ describe("isPlainObject", () => {
4
+ it("should test if the value is a plain object", () => {
5
+ expect(isPlainObject({})).toEqual(true);
6
+ expect(isPlainObject(class {})).toEqual(false);
7
+ expect(isPlainObject(new (class {})())).toEqual(false);
8
+ expect(isPlainObject(Object)).toEqual(false);
9
+ });
10
+ });
@@ -0,0 +1,14 @@
1
+ import {classOf} from "./classOf.js";
2
+
3
+ /**
4
+ *
5
+ * @param target
6
+ * @returns {boolean}
7
+ */
8
+ export function isClassObject(target: any): target is Object {
9
+ return target === Object;
10
+ }
11
+
12
+ export function isPlainObject(target: any): boolean {
13
+ return isClassObject(classOf(target)) && target !== Object;
14
+ }
@@ -0,0 +1,17 @@
1
+ import {isPrimitive, isPrimitiveClass} from "./isPrimitive.js";
2
+
3
+ describe("isPrimitive", () => {
4
+ it("should test if an variable content is a primitive", () => {
5
+ expect(isPrimitive("1")).toEqual(true);
6
+ expect(isPrimitive(1)).toEqual(true);
7
+ expect(isPrimitive(true)).toEqual(true);
8
+ expect(isPrimitive({})).toEqual(false);
9
+ });
10
+
11
+ it("should test if a class is String, Number or Boolean", () => {
12
+ expect(isPrimitiveClass(String)).toEqual(true);
13
+ expect(isPrimitiveClass(Number)).toEqual(true);
14
+ expect(isPrimitiveClass(Boolean)).toEqual(true);
15
+ expect(isPrimitiveClass(Date)).toEqual(false);
16
+ });
17
+ });
@@ -0,0 +1,26 @@
1
+ import {isBoolean, isBooleanOrBooleanClass} from "./isBoolean.js";
2
+ import {isNumber, isNumberOrNumberClass} from "./isNumber.js";
3
+ import {isString, isStringOrStringClass} from "./isString.js";
4
+
5
+ /**
6
+ * Return true if the given obj is a primitive.
7
+ * @param target
8
+ * @returns {boolean}
9
+ * @ignore
10
+ */
11
+ export function isPrimitiveOrPrimitiveClass(target: any): boolean {
12
+ return isStringOrStringClass(target) || isNumberOrNumberClass(target) || isBooleanOrBooleanClass(target);
13
+ }
14
+
15
+ /**
16
+ * Return true if the given obj is a primitive.
17
+ * @param target
18
+ * @returns {boolean}
19
+ */
20
+ export function isPrimitive(target: any): boolean {
21
+ return isString(target) || isNumber(target) || isBoolean(target);
22
+ }
23
+
24
+ export function isPrimitiveClass(target: any) {
25
+ return [String, Number, Boolean].includes(target);
26
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Tests to see if the object is an ES2015 (ES6) Promise
3
+ * @see {@link https://www.ecma-international.org/ecma-262/6.0/#sec-promise-objects}
4
+ * @param target
5
+ * @returns {boolean}
6
+ */
7
+ export function isPromise<T = any>(target: any): target is Promise<T> {
8
+ return (
9
+ target === Promise ||
10
+ target instanceof Promise ||
11
+ (!!target && typeof target.subscribe !== "function" && typeof target.then === "function")
12
+ );
13
+ }
@@ -0,0 +1,14 @@
1
+ import {isProtectedKey} from "./isProtectedKey.js";
2
+
3
+ describe("isProtectedKey", () => {
4
+ it("should return true", () => {
5
+ expect(isProtectedKey("__proto__")).toBe(true);
6
+ expect(isProtectedKey("prototype")).toBe(true);
7
+ expect(isProtectedKey("constructor")).toBe(true);
8
+ });
9
+ it("should return false", () => {
10
+ expect(isProtectedKey("test")).toBe(false);
11
+ expect(isProtectedKey("pro")).toBe(false);
12
+ expect(isProtectedKey("const")).toBe(false);
13
+ });
14
+ });
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Prevent prototype pollution vulnerability
3
+ * @param key
4
+ */
5
+ export function isProtectedKey(key: string) {
6
+ return ["__proto__", "constructor", "prototype"].includes(key);
7
+ }
@@ -0,0 +1,3 @@
1
+ export function isRegExp(target: any): target is RegExp {
2
+ return target instanceof RegExp;
3
+ }
@@ -0,0 +1,14 @@
1
+ import {isSerializable} from "./isSerializable.js";
2
+
3
+ describe("isSerializable()", () => {
4
+ it("should return the expected value", () => {
5
+ expect(isSerializable(true)).toBe(false);
6
+ expect(isSerializable(null)).toBe(false);
7
+ expect(isSerializable(undefined)).toBe(false);
8
+ expect(isSerializable("")).toBe(false);
9
+ expect(isSerializable("test")).toBe(false);
10
+ expect(isSerializable(0)).toBe(false);
11
+ expect(isSerializable(1)).toBe(false);
12
+ expect(isSerializable({})).toBe(true);
13
+ });
14
+ });
@@ -0,0 +1,9 @@
1
+ import {isBoolean} from "./isBoolean.js";
2
+ import {isNil} from "./isNil.js";
3
+ import {isNumber} from "./isNumber.js";
4
+ import {isStream} from "./isStream.js";
5
+ import {isString} from "./isString.js";
6
+
7
+ export function isSerializable(data: any) {
8
+ return !(Buffer.isBuffer(data) || isStream(data) || isBoolean(data) || isNumber(data) || isString(data) || isNil(data));
9
+ }
@@ -0,0 +1,3 @@
1
+ export function isStream(obj: any): boolean {
2
+ return obj !== null && typeof obj === "object" && typeof obj.pipe === "function";
3
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @param target
3
+ * @returns {boolean}
4
+ */
5
+ export function isString(target: any): target is string {
6
+ return typeof target === "string";
7
+ }
8
+
9
+ export function isStringOrStringClass(target: any): target is string {
10
+ return typeof target === "string" || target instanceof String || target === String;
11
+ }
@@ -0,0 +1,11 @@
1
+ import {isSymbol, isSymbolOrSymbolClass} from "./isSymbol.js";
2
+
3
+ describe("isSymbol()", () => {
4
+ it("should validate value", () => {
5
+ expect(isSymbol(Symbol.for("test"))).toBe(true);
6
+ expect(isSymbol("test")).toBe(false);
7
+ expect(isSymbolOrSymbolClass(Symbol.for("test"))).toBe(true);
8
+ expect(isSymbolOrSymbolClass(Symbol)).toBe(true);
9
+ expect(isSymbolOrSymbolClass("test")).toBe(false);
10
+ });
11
+ });
@@ -0,0 +1,11 @@
1
+ /**
2
+ *
3
+ * @param target
4
+ */
5
+ export function isSymbol(target: any): target is symbol {
6
+ return typeof target === "symbol";
7
+ }
8
+
9
+ export function isSymbolOrSymbolClass(target: any): target is symbol {
10
+ return typeof target === "symbol" || target instanceof Symbol || target === Symbol;
11
+ }
@@ -0,0 +1,25 @@
1
+ import {Type} from "../../domain/Type.js";
2
+ import {ancestorsOf} from "./ancestorsOf.js";
3
+ import {classOf} from "./classOf.js";
4
+ import {prototypeOf} from "./prototypeOf.js";
5
+
6
+ /**
7
+ * Return all methods for a given class.
8
+ * @param target
9
+ */
10
+ export function methodsOf(target: any): {target: Type; propertyKey: string}[] {
11
+ const methods = new Map();
12
+ target = classOf(target);
13
+
14
+ ancestorsOf(target).forEach((target) => {
15
+ const keys = Reflect.ownKeys(prototypeOf(target));
16
+
17
+ keys.forEach((propertyKey: string) => {
18
+ if (propertyKey !== "constructor") {
19
+ methods.set(propertyKey, {target, propertyKey});
20
+ }
21
+ });
22
+ });
23
+
24
+ return Array.from(methods.values());
25
+ }