@atlantjs/arch 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 (176) hide show
  1. package/README.md +8 -0
  2. package/configs/env/env-vars.d.ts +3 -0
  3. package/configs/env/env-vars.js +20 -0
  4. package/external-files/biome.json +47 -0
  5. package/external-files/typescript/tsconfig-backend.json +3 -0
  6. package/external-files/typescript/tsconfig-default.json +42 -0
  7. package/external-files/typescript/tsconfig-frontend.json +3 -0
  8. package/external-files/typescript/tsconfig-lib.json +6 -0
  9. package/external-files/typescript/tsconfig-mobile.json +3 -0
  10. package/index.d.ts +51 -0
  11. package/index.js +106 -0
  12. package/objects/arch/application/errors/application-error.abstract.d.ts +8 -0
  13. package/objects/arch/application/errors/application-error.abstract.js +25 -0
  14. package/objects/arch/application/errors/application-error.abstract.type.d.ts +9 -0
  15. package/objects/arch/application/errors/application-error.abstract.type.js +2 -0
  16. package/objects/arch/application/errors/unknown-application-error.d.ts +15 -0
  17. package/objects/arch/application/errors/unknown-application-error.js +16 -0
  18. package/objects/arch/domain/entity.abstract.d.ts +3 -0
  19. package/objects/arch/domain/entity.abstract.js +6 -0
  20. package/objects/arch/domain/usecase.abstract.d.ts +4 -0
  21. package/objects/arch/domain/usecase.abstract.js +6 -0
  22. package/objects/arch/infrastructure/controller.abstract.d.ts +6 -0
  23. package/objects/arch/infrastructure/controller.abstract.js +6 -0
  24. package/objects/auth/entities/client/client-credentials.abstract.d.ts +13 -0
  25. package/objects/auth/entities/client/client-credentials.abstract.js +27 -0
  26. package/objects/auth/entities/client/client-credentials.abstract.type.d.ts +7 -0
  27. package/objects/auth/entities/client/client-credentials.abstract.type.js +2 -0
  28. package/objects/auth/entities/client/realm-credentials.abstract.d.ts +13 -0
  29. package/objects/auth/entities/client/realm-credentials.abstract.js +20 -0
  30. package/objects/auth/entities/token/access-token-signature.d.ts +10 -0
  31. package/objects/auth/entities/token/access-token-signature.js +58 -0
  32. package/objects/auth/entities/token/access-token.abstract.d.ts +10 -0
  33. package/objects/auth/entities/token/access-token.abstract.has-role.error.d.ts +4 -0
  34. package/objects/auth/entities/token/access-token.abstract.has-role.error.js +14 -0
  35. package/objects/auth/entities/token/access-token.abstract.js +25 -0
  36. package/objects/auth/entities/token/access-token.abstract.signature-verify.error.d.ts +4 -0
  37. package/objects/auth/entities/token/access-token.abstract.signature-verify.error.js +10 -0
  38. package/objects/auth/entities/token/access-token.abstract.type.d.ts +44 -0
  39. package/objects/auth/entities/token/access-token.abstract.type.js +2 -0
  40. package/objects/auth/entities/token/rotation.d.ts +12 -0
  41. package/objects/auth/entities/token/rotation.js +108 -0
  42. package/objects/auth/entities/token/rotation.type.d.ts +5 -0
  43. package/objects/auth/entities/token/rotation.type.js +2 -0
  44. package/objects/auth/entities/token/token.d.ts +16 -0
  45. package/objects/auth/entities/token/token.js +50 -0
  46. package/objects/auth/entities/token/token.type.d.ts +5 -0
  47. package/objects/auth/entities/token/token.type.js +2 -0
  48. package/objects/auth/entities/user/enums/user-status.enum.d.ts +4 -0
  49. package/objects/auth/entities/user/enums/user-status.enum.js +8 -0
  50. package/objects/auth/entities/user/password/password.abstract.d.ts +8 -0
  51. package/objects/auth/entities/user/password/password.abstract.js +41 -0
  52. package/objects/auth/entities/user/password/password.abstract.type.d.ts +5 -0
  53. package/objects/auth/entities/user/password/password.abstract.type.js +2 -0
  54. package/objects/auth/entities/user/password/steps/not-allowing-consecutive-chars-step.d.ts +11 -0
  55. package/objects/auth/entities/user/password/steps/not-allowing-consecutive-chars-step.js +34 -0
  56. package/objects/auth/entities/user/password/steps/not-allowing-date-step.d.ts +11 -0
  57. package/objects/auth/entities/user/password/steps/not-allowing-date-step.js +61 -0
  58. package/objects/auth/entities/user/password/steps/not-allowing-phone-step.d.ts +7 -0
  59. package/objects/auth/entities/user/password/steps/not-allowing-phone-step.js +27 -0
  60. package/objects/auth/entities/user/password/steps/not-allowing-repeated-chars-step.d.ts +11 -0
  61. package/objects/auth/entities/user/password/steps/not-allowing-repeated-chars-step.js +34 -0
  62. package/objects/auth/entities/user/password/steps/not-allowing-string-step.d.ts +7 -0
  63. package/objects/auth/entities/user/password/steps/not-allowing-string-step.js +19 -0
  64. package/objects/auth/entities/user/password/steps/not-allowing-these-chars-step.d.ts +10 -0
  65. package/objects/auth/entities/user/password/steps/not-allowing-these-chars-step.js +17 -0
  66. package/objects/auth/entities/user/password/steps/numeric-step.d.ts +5 -0
  67. package/objects/auth/entities/user/password/steps/numeric-step.js +13 -0
  68. package/objects/auth/entities/user/password/steps/with-at-least-one-letter-step.d.ts +5 -0
  69. package/objects/auth/entities/user/password/steps/with-at-least-one-letter-step.js +14 -0
  70. package/objects/auth/entities/user/password/steps/with-at-least-one-number-digit-step.d.ts +5 -0
  71. package/objects/auth/entities/user/password/steps/with-at-least-one-number-digit-step.js +14 -0
  72. package/objects/auth/entities/user/password/steps/with-at-least-one-special-char-step.d.ts +5 -0
  73. package/objects/auth/entities/user/password/steps/with-at-least-one-special-char-step.js +14 -0
  74. package/objects/auth/entities/user/password/steps/with-length-step.d.ts +11 -0
  75. package/objects/auth/entities/user/password/steps/with-length-step.js +18 -0
  76. package/objects/auth/entities/user/password/validation-step.d.ts +5 -0
  77. package/objects/auth/entities/user/password/validation-step.js +9 -0
  78. package/objects/auth/entities/user/password/validation-step.type.d.ts +4 -0
  79. package/objects/auth/entities/user/password/validation-step.type.js +2 -0
  80. package/objects/auth/entities/user/user-credentials.abstract.d.ts +9 -0
  81. package/objects/auth/entities/user/user-credentials.abstract.js +6 -0
  82. package/objects/auth/entities/user/user-tokens.abstract.d.ts +13 -0
  83. package/objects/auth/entities/user/user-tokens.abstract.js +27 -0
  84. package/objects/auth/entities/user/user-tokens.abstract.type.d.ts +5 -0
  85. package/objects/auth/entities/user/user-tokens.abstract.type.js +2 -0
  86. package/objects/auth/entities/user/user.abstract.d.ts +21 -0
  87. package/objects/auth/entities/user/user.abstract.js +29 -0
  88. package/objects/auth/entities/user/value-objects/email.d.ts +6 -0
  89. package/objects/auth/entities/user/value-objects/email.js +17 -0
  90. package/objects/auth/entities/user/value-objects/person-name.d.ts +7 -0
  91. package/objects/auth/entities/user/value-objects/person-name.js +17 -0
  92. package/objects/auth/entities/user/value-objects/user-id.d.ts +3 -0
  93. package/objects/auth/entities/user/value-objects/user-id.js +7 -0
  94. package/objects/auth/entities/user/value-objects/username.d.ts +3 -0
  95. package/objects/auth/entities/user/value-objects/username.js +7 -0
  96. package/objects/auth/enums/client.enum.d.ts +3 -0
  97. package/objects/auth/enums/client.enum.js +7 -0
  98. package/objects/auth/enums/realm.enum.d.ts +3 -0
  99. package/objects/auth/enums/realm.enum.js +7 -0
  100. package/objects/auth/enums/role-group.enum.d.ts +4 -0
  101. package/objects/auth/enums/role-group.enum.js +8 -0
  102. package/objects/auth/enums/role.enum.d.ts +17 -0
  103. package/objects/auth/enums/role.enum.js +21 -0
  104. package/objects/auth/enums/token-name-header.enum.d.ts +4 -0
  105. package/objects/auth/enums/token-name-header.enum.js +8 -0
  106. package/objects/configs/enums/node-envs.enum.d.ts +6 -0
  107. package/objects/configs/enums/node-envs.enum.js +10 -0
  108. package/objects/datatypes/entities/$boolean.abstract.d.ts +7 -0
  109. package/objects/datatypes/entities/$boolean.abstract.js +21 -0
  110. package/objects/datatypes/entities/$number.abstract.d.ts +13 -0
  111. package/objects/datatypes/entities/$number.abstract.js +60 -0
  112. package/objects/datatypes/entities/$string.abstract.d.ts +10 -0
  113. package/objects/datatypes/entities/$string.abstract.js +34 -0
  114. package/objects/datatypes/entities/uuid.abstract.d.ts +7 -0
  115. package/objects/datatypes/entities/uuid.abstract.js +20 -0
  116. package/package.json +66 -0
  117. package/tests/builders/auth/access-token-signature.builder.d.ts +6 -0
  118. package/tests/builders/auth/access-token-signature.builder.js +19 -0
  119. package/tests/builders/auth/access-token.builder.d.ts +6 -0
  120. package/tests/builders/auth/access-token.builder.js +15 -0
  121. package/tests/builders/auth/rotation.builder.d.ts +6 -0
  122. package/tests/builders/auth/rotation.builder.js +19 -0
  123. package/tests/builders/auth/token.builder.d.ts +7 -0
  124. package/tests/builders/auth/token.builder.js +15 -0
  125. package/tests/builders/auth/value-objects/client-credentials.builder.d.ts +6 -0
  126. package/tests/builders/auth/value-objects/client-credentials.builder.js +18 -0
  127. package/tests/builders/auth/value-objects/dummies/access-token.dummy.d.ts +3 -0
  128. package/tests/builders/auth/value-objects/dummies/access-token.dummy.js +7 -0
  129. package/tests/builders/auth/value-objects/dummies/client-credentials.dummy.d.ts +3 -0
  130. package/tests/builders/auth/value-objects/dummies/client-credentials.dummy.js +7 -0
  131. package/tests/builders/auth/value-objects/dummies/realm-credentials.dummy.d.ts +6 -0
  132. package/tests/builders/auth/value-objects/dummies/realm-credentials.dummy.js +12 -0
  133. package/tests/builders/auth/value-objects/dummies/user-tokens.dummy.d.ts +3 -0
  134. package/tests/builders/auth/value-objects/dummies/user-tokens.dummy.js +7 -0
  135. package/tests/builders/auth/value-objects/realm-credentials.builder.d.ts +6 -0
  136. package/tests/builders/auth/value-objects/realm-credentials.builder.js +17 -0
  137. package/tests/builders/auth/value-objects/user-tokens.builder.d.ts +7 -0
  138. package/tests/builders/auth/value-objects/user-tokens.builder.js +18 -0
  139. package/tests/builders/errors/application-error.builder.d.ts +6 -0
  140. package/tests/builders/errors/application-error.builder.js +21 -0
  141. package/tests/builders/errors/dummies/application-error.dummy.d.ts +3 -0
  142. package/tests/builders/errors/dummies/application-error.dummy.js +7 -0
  143. package/tests/builders/generic/dummies/uuid.dummy.d.ts +3 -0
  144. package/tests/builders/generic/dummies/uuid.dummy.js +7 -0
  145. package/tests/builders/generic/uuid.builder.d.ts +6 -0
  146. package/tests/builders/generic/uuid.builder.js +14 -0
  147. package/tests/builders/user/user-id.builder.d.ts +6 -0
  148. package/tests/builders/user/user-id.builder.js +15 -0
  149. package/tests/create-fake-stub.d.ts +8 -0
  150. package/tests/create-fake-stub.js +37 -0
  151. package/tests/entity-builder.util.d.ts +13 -0
  152. package/tests/entity-builder.util.js +39 -0
  153. package/tests/faker/faker.d.ts +2 -0
  154. package/tests/faker/faker.js +6 -0
  155. package/utils/arrays/extended-map.d.ts +5 -0
  156. package/utils/arrays/extended-map.js +23 -0
  157. package/utils/datatypes/generic-types.d.ts +7 -0
  158. package/utils/datatypes/generic-types.js +3 -0
  159. package/utils/datatypes/string-utils.d.ts +3 -0
  160. package/utils/datatypes/string-utils.js +4 -0
  161. package/utils/ducts/common.d.ts +9 -0
  162. package/utils/ducts/common.js +14 -0
  163. package/utils/ducts/optional-type.d.ts +25 -0
  164. package/utils/ducts/optional-type.js +50 -0
  165. package/utils/ducts/return-type.d.ts +22 -0
  166. package/utils/ducts/return-type.js +60 -0
  167. package/utils/randoms/random-enum-value.d.ts +2 -0
  168. package/utils/randoms/random-enum-value.js +20 -0
  169. package/utils/randoms/random-number.d.ts +6 -0
  170. package/utils/randoms/random-number.js +6 -0
  171. package/utils/type-guard/guardian-exception.d.ts +2 -0
  172. package/utils/type-guard/guardian-exception.js +6 -0
  173. package/utils/type-guard/guardian.d.ts +29 -0
  174. package/utils/type-guard/guardian.js +132 -0
  175. package/utils/type-guard/guardian.type.d.ts +1 -0
  176. package/utils/type-guard/guardian.type.js +2 -0
@@ -0,0 +1,7 @@
1
+ declare const emptyObject: {};
2
+ export type Newable<T> = new (...args: any[]) => T;
3
+ export type ObjectType = typeof emptyObject;
4
+ export interface Abstract<T> {
5
+ prototype: T;
6
+ }
7
+ export {};
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const emptyObject = {};
@@ -0,0 +1,3 @@
1
+ interface String {
2
+ toKebab(): string;
3
+ }
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ String.prototype.toKebab = function () {
3
+ return this.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
4
+ };
@@ -0,0 +1,9 @@
1
+ export declare const T: unique symbol;
2
+ export declare const Val: unique symbol;
3
+ export declare const FnVal: unique symbol;
4
+ export declare const EmptyArray: readonly any[];
5
+ export type FalseyValues = false | null | undefined | 0 | 0n | "";
6
+ export declare function isTruthy(val: unknown): boolean;
7
+ export type IterType<T> = T extends {
8
+ [Symbol.iterator](): infer I;
9
+ } ? I : unknown;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EmptyArray = exports.FnVal = exports.Val = exports.T = void 0;
4
+ exports.isTruthy = isTruthy;
5
+ const guardian_1 = require("../type-guard/guardian");
6
+ exports.T = Symbol("T");
7
+ exports.Val = Symbol("Val");
8
+ exports.FnVal = Symbol("FnVal");
9
+ exports.EmptyArray = Object.freeze([]);
10
+ function isTruthy(val) {
11
+ return val instanceof Date
12
+ ? guardian_1.Guardian.isEqual(val.getTime(), val.getTime())
13
+ : !!val;
14
+ }
@@ -0,0 +1,25 @@
1
+ import { FalseyValues, IterType, T, Val } from "./common";
2
+ export type Something<T> = OptionalType<T> & {
3
+ [T]: true;
4
+ };
5
+ export type Nothing = OptionalType<never> & {
6
+ [T]: false;
7
+ };
8
+ export type Optional<T> = OptionalType<T>;
9
+ type From<T> = Exclude<T, Error | FalseyValues>;
10
+ declare class OptionalType<T> {
11
+ readonly [T]: boolean;
12
+ readonly [Val]: T;
13
+ constructor(val: T, something: boolean);
14
+ [Symbol.iterator](this: Optional<T>): IterType<T>;
15
+ isSomething(this: Optional<T>): this is Something<T>;
16
+ isNothing(this: Optional<T>): this is Nothing;
17
+ unwrap(this: Optional<T>): T;
18
+ unwrapUnchecked(this: Optional<T>): T | undefined;
19
+ unwrapOr(this: Optional<T>, def: T): T;
20
+ unwrapOrElse(this: Optional<T>, f: () => T): T;
21
+ }
22
+ export declare function Optional<T>(val: T): Optional<From<T>>;
23
+ export declare function Something<T>(val: T): Something<T>;
24
+ export declare const Nothing: Readonly<OptionalType<never>>;
25
+ export {};
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Nothing = void 0;
4
+ exports.Optional = Optional;
5
+ exports.Something = Something;
6
+ const common_1 = require("./common");
7
+ class OptionalType {
8
+ constructor(val, something) {
9
+ this[common_1.T] = something;
10
+ this[common_1.Val] = val;
11
+ }
12
+ [Symbol.iterator]() {
13
+ return this[common_1.T]
14
+ ? this[common_1.Val][Symbol.iterator]()
15
+ : common_1.EmptyArray[Symbol.iterator]();
16
+ }
17
+ isSomething() {
18
+ return this[common_1.T];
19
+ }
20
+ isNothing() {
21
+ return !this[common_1.T];
22
+ }
23
+ unwrap() {
24
+ if (this[common_1.T]) {
25
+ return this[common_1.Val];
26
+ }
27
+ throw new Error("Failed to unwrap Option (found Nothing)");
28
+ }
29
+ unwrapUnchecked() {
30
+ return this[common_1.Val];
31
+ }
32
+ unwrapOr(def) {
33
+ return this[common_1.T] ? this[common_1.Val] : def;
34
+ }
35
+ unwrapOrElse(f) {
36
+ return this[common_1.T] ? this[common_1.Val] : f();
37
+ }
38
+ }
39
+ function Optional(val) {
40
+ return from(val);
41
+ }
42
+ function Something(val) {
43
+ return new OptionalType(val, true);
44
+ }
45
+ exports.Nothing = Object.freeze(new OptionalType(undefined, false));
46
+ function from(val) {
47
+ return (0, common_1.isTruthy)(val) && !(val instanceof Error)
48
+ ? Something(val)
49
+ : exports.Nothing;
50
+ }
@@ -0,0 +1,22 @@
1
+ import { FalseyValues, IterType, T, Val } from "./common";
2
+ export type Success<T> = ReturnType<T, never>;
3
+ export type Failure<E> = ReturnType<never, E>;
4
+ export type Return<T, E> = ReturnType<T, E>;
5
+ type From<T> = Exclude<T, Error | FalseyValues>;
6
+ export declare class ReturnType<T, E> {
7
+ readonly [T]: boolean;
8
+ readonly [Val]: T | E;
9
+ constructor(val: T | E, ok: boolean);
10
+ [Symbol.iterator](this: Return<T, E>): IterType<T>;
11
+ isSuccess(this: Return<T, E>): this is Success<T>;
12
+ isFailure(this: Return<T, E>): this is Failure<E>;
13
+ flatten<U, F>(this: Return<Return<U, F>, E>): Return<U, E | F>;
14
+ unwrap(this: Return<T, E>): T;
15
+ unwrapErr(this: Return<T, E>): E;
16
+ unwrapOr(this: Return<T, E>, def: T): T;
17
+ unwrapUnchecked(this: Return<T, E>): T | E;
18
+ }
19
+ export declare function Result<T>(val: T): Return<From<T>, (T extends Error ? T : never) | (Extract<FalseyValues, T> extends never ? never : null)>;
20
+ export declare function Success<T>(val: T): Success<T>;
21
+ export declare function Failure<E>(val: E): Failure<E>;
22
+ export {};
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ReturnType = void 0;
4
+ exports.Result = Result;
5
+ exports.Success = Success;
6
+ exports.Failure = Failure;
7
+ const common_1 = require("./common");
8
+ class ReturnType {
9
+ constructor(val, ok) {
10
+ this[common_1.Val] = val;
11
+ this[common_1.T] = ok;
12
+ }
13
+ [Symbol.iterator]() {
14
+ return this[common_1.T]
15
+ ? this[common_1.Val][Symbol.iterator]()
16
+ : common_1.EmptyArray[Symbol.iterator]();
17
+ }
18
+ isSuccess() {
19
+ return this[common_1.T];
20
+ }
21
+ isFailure() {
22
+ return !this[common_1.T];
23
+ }
24
+ flatten() {
25
+ return this[common_1.T] ? this[common_1.Val] : this;
26
+ }
27
+ unwrap() {
28
+ if (this[common_1.T]) {
29
+ return this[common_1.Val];
30
+ }
31
+ throw new Error("Failed to unwrap Result (found Err)");
32
+ }
33
+ unwrapErr() {
34
+ if (this[common_1.T]) {
35
+ throw new Error("Failed to unwrapErr Result (found Ok)");
36
+ }
37
+ return this[common_1.Val];
38
+ }
39
+ unwrapOr(def) {
40
+ return this[common_1.T] ? this[common_1.Val] : def;
41
+ }
42
+ unwrapUnchecked() {
43
+ return this[common_1.Val];
44
+ }
45
+ }
46
+ exports.ReturnType = ReturnType;
47
+ function Result(val) {
48
+ return from(val);
49
+ }
50
+ function Success(val) {
51
+ return new ReturnType(val, true);
52
+ }
53
+ function Failure(val) {
54
+ return new ReturnType(val, false);
55
+ }
56
+ function from(val) {
57
+ return (0, common_1.isTruthy)(val)
58
+ ? new ReturnType(val, !(val instanceof Error))
59
+ : Failure(null);
60
+ }
@@ -0,0 +1,2 @@
1
+ export declare function randomEnumValue<T extends Record<string, unknown>>(anEnum: T): T[keyof T];
2
+ export declare function randomEnumValueExcept<T extends Record<string, unknown>>(anEnum: T, excepts: Array<T[keyof T]>): T[keyof T];
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.randomEnumValue = randomEnumValue;
4
+ exports.randomEnumValueExcept = randomEnumValueExcept;
5
+ function randomEnumValue(anEnum) {
6
+ const keys = Object.keys(anEnum).filter((x) => Number.isNaN(Number(x)));
7
+ const randomKeyIndex = Math.floor(Math.random() * keys.length);
8
+ const randomKey = keys[randomKeyIndex];
9
+ return anEnum[randomKey];
10
+ }
11
+ function randomEnumValueExcept(anEnum, excepts) {
12
+ const exceptKeys = excepts.map((value) => Object.keys(anEnum).find((key) => anEnum[key] === value));
13
+ const validKeys = Object.keys(anEnum).filter((key) => Number.isNaN(Number(key)) && !exceptKeys.includes(key));
14
+ if (validKeys.length === 0) {
15
+ throw new Error("No valid enum keys available after excluding 'excepts'.");
16
+ }
17
+ const randomKeyIndex = Math.floor(Math.random() * validKeys.length);
18
+ const randomKey = validKeys[randomKeyIndex];
19
+ return anEnum[randomKey];
20
+ }
@@ -0,0 +1,6 @@
1
+ interface RandomValue {
2
+ min: number;
3
+ max: number;
4
+ }
5
+ export declare function randomNumber(value: RandomValue): number;
6
+ export {};
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.randomNumber = randomNumber;
4
+ function randomNumber(value) {
5
+ return Math.floor(Math.random() * (value.max - value.min + 1)) + value.min;
6
+ }
@@ -0,0 +1,2 @@
1
+ export declare class GuardianException extends Error {
2
+ }
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GuardianException = void 0;
4
+ class GuardianException extends Error {
5
+ }
6
+ exports.GuardianException = GuardianException;
@@ -0,0 +1,29 @@
1
+ import { PrimitiveType } from "./guardian.type";
2
+ export declare class Guardian {
3
+ static and(...args: boolean[]): boolean;
4
+ static or(...args: boolean[]): boolean;
5
+ static greaterThan(value: number, reference: number): boolean;
6
+ static lessThan(value: number, reference: number): boolean;
7
+ static isEmpty<T>(value: unknown): value is null | undefined | T[];
8
+ static isNotEmpty(value: unknown): boolean;
9
+ static isUndefined(value: unknown): value is undefined;
10
+ static isNotUndefined(value: unknown): value is boolean;
11
+ static isNull(value: unknown): value is null;
12
+ static isNotNull(value: unknown): value is boolean;
13
+ static isEqual(valueA: unknown, valueB: unknown): boolean;
14
+ static isAnyOf(reference: unknown, anyValues: unknown[]): boolean;
15
+ static isDifferent(valueA: unknown, valueB: unknown): boolean;
16
+ static isTruthy(value: boolean): boolean;
17
+ static isFalsy(value: boolean | undefined): boolean;
18
+ static ternaryCondition<TTrue, TFalse>(condition: boolean, returnIfTrue: TTrue, returnIfFalse: TFalse): TTrue | TFalse;
19
+ static isBetween(value: number | string, min: number, max: number): boolean;
20
+ static isLengthEqual(value: unknown, length: number): boolean;
21
+ static isNullOrUndefined(value: unknown): value is null | undefined;
22
+ static isNotNullOrUndefined(value: unknown): value is boolean;
23
+ static isNumber(value: unknown): value is number;
24
+ static isString(value: unknown): value is string;
25
+ static isArray(value: unknown): value is unknown[];
26
+ static isObject(value: unknown): value is object;
27
+ static isTypeOf<T>(value: unknown, type: PrimitiveType): value is T;
28
+ static isNotTypeOf<T>(value: unknown, type: PrimitiveType): value is T;
29
+ }
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Guardian = void 0;
4
+ const guardian_exception_1 = require("./guardian-exception");
5
+ class Guardian {
6
+ static and(...args) {
7
+ return args.every((func) => func);
8
+ }
9
+ static or(...args) {
10
+ return args.some((func) => func);
11
+ }
12
+ static greaterThan(value, reference) {
13
+ return value > reference;
14
+ }
15
+ static lessThan(value, reference) {
16
+ return value < reference;
17
+ }
18
+ static isEmpty(value) {
19
+ if (Guardian.isNullOrUndefined(value)) {
20
+ return true;
21
+ }
22
+ if (Guardian.isString(value) || Guardian.isArray(value)) {
23
+ return value.length === 0;
24
+ }
25
+ if (Guardian.isObject(value)) {
26
+ return Object.keys(value).length === 0;
27
+ }
28
+ return false;
29
+ }
30
+ static isNotEmpty(value) {
31
+ if (Guardian.isNullOrUndefined(value)) {
32
+ return false;
33
+ }
34
+ if ((Guardian.isString(value) || Guardian.isArray(value)) &&
35
+ value.length === 0) {
36
+ return false;
37
+ }
38
+ if (Guardian.isObject(value) && Object.keys(value).length === 0) {
39
+ return false;
40
+ }
41
+ return true;
42
+ }
43
+ static isUndefined(value) {
44
+ return value === undefined;
45
+ }
46
+ static isNotUndefined(value) {
47
+ return value !== undefined;
48
+ }
49
+ static isNull(value) {
50
+ return value === null;
51
+ }
52
+ static isNotNull(value) {
53
+ return value !== null;
54
+ }
55
+ static isEqual(valueA, valueB) {
56
+ return valueA === valueB;
57
+ }
58
+ static isAnyOf(reference, anyValues) {
59
+ for (const value of anyValues) {
60
+ if (Guardian.isEqual(reference, value)) {
61
+ return true;
62
+ }
63
+ }
64
+ return false;
65
+ }
66
+ static isDifferent(valueA, valueB) {
67
+ return valueA !== valueB;
68
+ }
69
+ static isTruthy(value) {
70
+ return value;
71
+ }
72
+ static isFalsy(value) {
73
+ return value === false;
74
+ }
75
+ static ternaryCondition(condition, returnIfTrue, returnIfFalse) {
76
+ return condition ? returnIfTrue : returnIfFalse;
77
+ }
78
+ static isBetween(value, min, max) {
79
+ if (min > max)
80
+ throw new guardian_exception_1.GuardianException(`Max ${max} should be greater than min ${min}.`);
81
+ if (Guardian.isEmpty(value)) {
82
+ throw new guardian_exception_1.GuardianException("Cannot check length of a value. Provided value is empty");
83
+ }
84
+ const valueLength = Guardian.isNumber(value) ? value : Number(value);
85
+ return valueLength >= min && valueLength <= max;
86
+ }
87
+ static isLengthEqual(value, length) {
88
+ if (Guardian.isEmpty(value)) {
89
+ throw new guardian_exception_1.GuardianException("Cannot check length of a value. Provided value is empty");
90
+ }
91
+ let valueLength;
92
+ if (Guardian.isNumber(value)) {
93
+ valueLength = length;
94
+ }
95
+ else if (Guardian.isString(value) || Guardian.isArray(value)) {
96
+ valueLength = value.length;
97
+ }
98
+ else {
99
+ throw new guardian_exception_1.GuardianException("Unsupported value type");
100
+ }
101
+ return valueLength === length;
102
+ }
103
+ static isNullOrUndefined(value) {
104
+ if (value === null || value === undefined)
105
+ return true;
106
+ return false;
107
+ }
108
+ static isNotNullOrUndefined(value) {
109
+ if (value === undefined || value === null)
110
+ return false;
111
+ return true;
112
+ }
113
+ static isNumber(value) {
114
+ return typeof value === "number";
115
+ }
116
+ static isString(value) {
117
+ return typeof value === "string";
118
+ }
119
+ static isArray(value) {
120
+ return Array.isArray(value);
121
+ }
122
+ static isObject(value) {
123
+ return typeof value === "object";
124
+ }
125
+ static isTypeOf(value, type) {
126
+ return Guardian.isEqual(typeof value, type);
127
+ }
128
+ static isNotTypeOf(value, type) {
129
+ return Guardian.isDifferent(typeof value, type);
130
+ }
131
+ }
132
+ exports.Guardian = Guardian;
@@ -0,0 +1 @@
1
+ export type PrimitiveType = "string" | "number" | "boolean" | "object" | "undefined" | "function" | "symbol" | "bigint";
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });