drimion 0.1.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.
- package/LICENSE +7 -0
- package/README.md +955 -0
- package/dist/cli/index.js +1045 -0
- package/dist/cli/templates/aggregate.ts.hbs +22 -0
- package/dist/cli/templates/entity.ts.hbs +16 -0
- package/dist/cli/templates/repository.ts.hbs +24 -0
- package/dist/cli/templates/use-case.ts.hbs +20 -0
- package/dist/cli/templates/value-object.ts.hbs +16 -0
- package/dist/kernel/core/aggregate.ts +234 -0
- package/dist/kernel/core/entity.ts +348 -0
- package/dist/kernel/core/id.ts +207 -0
- package/dist/kernel/core/index.ts +5 -0
- package/dist/kernel/core/repository.ts +81 -0
- package/dist/kernel/core/value-object.ts +309 -0
- package/dist/kernel/events/browser-event-manager.ts +241 -0
- package/dist/kernel/events/domain-event.ts +76 -0
- package/dist/kernel/events/event-bus.ts +158 -0
- package/dist/kernel/events/event-context.ts +95 -0
- package/dist/kernel/events/event-manager.ts +20 -0
- package/dist/kernel/events/event-utils.ts +19 -0
- package/dist/kernel/events/index.ts +7 -0
- package/dist/kernel/events/server-event-manager.ts +169 -0
- package/dist/kernel/helpers/auto-mapper.ts +222 -0
- package/dist/kernel/helpers/domain-classes.ts +162 -0
- package/dist/kernel/helpers/domain-error.ts +52 -0
- package/dist/kernel/helpers/getters-setters.ts +385 -0
- package/dist/kernel/helpers/index.ts +7 -0
- package/dist/kernel/index.ts +73 -0
- package/dist/kernel/libs/crypto.ts +33 -0
- package/dist/kernel/libs/index.ts +5 -0
- package/dist/kernel/libs/iterator.ts +298 -0
- package/dist/kernel/libs/result.ts +252 -0
- package/dist/kernel/libs/utils.ts +260 -0
- package/dist/kernel/libs/validator.ts +353 -0
- package/dist/kernel/types/adapter.types.ts +26 -0
- package/dist/kernel/types/command.types.ts +37 -0
- package/dist/kernel/types/entity.types.ts +60 -0
- package/dist/kernel/types/event.types.ts +129 -0
- package/dist/kernel/types/index.ts +26 -0
- package/dist/kernel/types/iterator.types.ts +39 -0
- package/dist/kernel/types/result.types.ts +122 -0
- package/dist/kernel/types/uid.types.ts +18 -0
- package/dist/kernel/types/utils.types.ts +120 -0
- package/dist/kernel/types/value-object.types.ts +20 -0
- package/dist/kernel/utils/date.utils.ts +111 -0
- package/dist/kernel/utils/index.ts +32 -0
- package/dist/kernel/utils/number.utils.ts +341 -0
- package/dist/kernel/utils/object.utils.ts +61 -0
- package/dist/kernel/utils/string.utils.ts +128 -0
- package/dist/kernel/utils/type.utils.ts +33 -0
- package/package.json +59 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import type { CalcOpt } from "../types/utils.types";
|
|
2
|
+
import { DecrementTime, IncrementTime } from "../utils/date.utils";
|
|
3
|
+
import { Divide, Multiply, Subtract, Sum } from "../utils/number.utils";
|
|
4
|
+
import { RemoveChars, RemoveSpaces, ReplaceChars } from "../utils/string.utils";
|
|
5
|
+
import validator, { type Validator } from "./validator";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @description
|
|
9
|
+
* Utility class providing various helper methods for date, number, and string manipulations.
|
|
10
|
+
*/
|
|
11
|
+
export class Utils {
|
|
12
|
+
private static instance: Utils;
|
|
13
|
+
private static validator: Validator = validator;
|
|
14
|
+
|
|
15
|
+
private constructor() {
|
|
16
|
+
Utils.validator = validator;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @description
|
|
21
|
+
* Creates a singleton instance of the Utils class.
|
|
22
|
+
*
|
|
23
|
+
* @returns {Utils} An instance of the Utils class.
|
|
24
|
+
*/
|
|
25
|
+
public static create(): Utils {
|
|
26
|
+
if (!Utils.instance) Utils.instance = new Utils();
|
|
27
|
+
return Utils.instance;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @description
|
|
32
|
+
* Provides date manipulation utilities for adding or removing units of time.
|
|
33
|
+
*
|
|
34
|
+
* @param target The date to manipulate.
|
|
35
|
+
* @returns An object with methods for adding or removing units of time (days, hours, etc.).
|
|
36
|
+
*/
|
|
37
|
+
public date(target: Date) {
|
|
38
|
+
return {
|
|
39
|
+
add: (value: number) => ({
|
|
40
|
+
/**
|
|
41
|
+
* @description
|
|
42
|
+
* add days to a date.
|
|
43
|
+
*
|
|
44
|
+
* @returns a new date with incremented days.
|
|
45
|
+
*/
|
|
46
|
+
days: () => new Date(IncrementTime(target, value, "day")),
|
|
47
|
+
/**
|
|
48
|
+
* @description
|
|
49
|
+
* add hours to a date.
|
|
50
|
+
*
|
|
51
|
+
* @returns a new date with incremented hours.
|
|
52
|
+
*/
|
|
53
|
+
hours: () => new Date(IncrementTime(target, value, "hour")),
|
|
54
|
+
/**
|
|
55
|
+
* @description
|
|
56
|
+
* add minutes to a date.
|
|
57
|
+
*
|
|
58
|
+
* @returns a new date with incremented minutes.
|
|
59
|
+
*/
|
|
60
|
+
minutes: () => new Date(IncrementTime(target, value, "minute")),
|
|
61
|
+
/**
|
|
62
|
+
* @description
|
|
63
|
+
* add weeks to a date.
|
|
64
|
+
*
|
|
65
|
+
* @returns a new date with incremented weeks.
|
|
66
|
+
*/
|
|
67
|
+
weeks: () => new Date(IncrementTime(target, value, "week")),
|
|
68
|
+
/**
|
|
69
|
+
* @description
|
|
70
|
+
* add months to a date.
|
|
71
|
+
*
|
|
72
|
+
* @returns a new date with incremented months.
|
|
73
|
+
*/
|
|
74
|
+
months: () => new Date(IncrementTime(target, value, "month")),
|
|
75
|
+
/**
|
|
76
|
+
* @description
|
|
77
|
+
* add years to a date.
|
|
78
|
+
*
|
|
79
|
+
* @returns a new date with incremented years.
|
|
80
|
+
*/
|
|
81
|
+
years: () => new Date(IncrementTime(target, value, "year")),
|
|
82
|
+
}),
|
|
83
|
+
subtract: (value: number) => ({
|
|
84
|
+
/**
|
|
85
|
+
* @description
|
|
86
|
+
* remove days from a date.
|
|
87
|
+
*
|
|
88
|
+
* @returns a new date with removed days.
|
|
89
|
+
*/
|
|
90
|
+
days: () => new Date(DecrementTime(target, value, "day")),
|
|
91
|
+
/**
|
|
92
|
+
* @description
|
|
93
|
+
* remove hours from a date.
|
|
94
|
+
*
|
|
95
|
+
* @returns a new date with removed hours.
|
|
96
|
+
*/
|
|
97
|
+
hours: () => new Date(DecrementTime(target, value, "hour")),
|
|
98
|
+
/**
|
|
99
|
+
* @description
|
|
100
|
+
* remove minutes from a date.
|
|
101
|
+
*
|
|
102
|
+
* @returns a new date with removed minutes.
|
|
103
|
+
*/
|
|
104
|
+
minutes: () => new Date(DecrementTime(target, value, "minute")),
|
|
105
|
+
/**
|
|
106
|
+
* @description
|
|
107
|
+
* remove weeks from a date.
|
|
108
|
+
*
|
|
109
|
+
* @returns a new date with removed weeks.
|
|
110
|
+
*/
|
|
111
|
+
weeks: () => new Date(DecrementTime(target, value, "week")),
|
|
112
|
+
/**
|
|
113
|
+
* @description
|
|
114
|
+
* remove months from a date.
|
|
115
|
+
*
|
|
116
|
+
* @returns a new date with removed months.
|
|
117
|
+
*/
|
|
118
|
+
months: () => new Date(DecrementTime(target, value, "month")),
|
|
119
|
+
/**
|
|
120
|
+
* @description
|
|
121
|
+
* remove years from a date.
|
|
122
|
+
*
|
|
123
|
+
* @returns a new date with removed years.
|
|
124
|
+
*/
|
|
125
|
+
years: () => new Date(DecrementTime(target, value, "year")),
|
|
126
|
+
}),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @description
|
|
132
|
+
* Provides number manipulation utilities, including mathematical operations.
|
|
133
|
+
*
|
|
134
|
+
* @param target The number to manipulate.
|
|
135
|
+
* @returns An object with methods for multiplication, division, subtraction, and addition.
|
|
136
|
+
*/
|
|
137
|
+
number(target: number) {
|
|
138
|
+
return {
|
|
139
|
+
/**
|
|
140
|
+
* @description
|
|
141
|
+
* multiply a value for another one.
|
|
142
|
+
*
|
|
143
|
+
* @param value number or string (number)
|
|
144
|
+
* @param options as object with fractionDigits option
|
|
145
|
+
* @default fractionDigits 5
|
|
146
|
+
* @returns result as number
|
|
147
|
+
* @sumary If you provide a string NAN (not a number) 0 will be considered as value.
|
|
148
|
+
*/
|
|
149
|
+
multiplyBy: (value: number, opt?: CalcOpt): number =>
|
|
150
|
+
Multiply(target, value, opt?.fractionDigits),
|
|
151
|
+
/**
|
|
152
|
+
* @description
|
|
153
|
+
* divide a value for another one.
|
|
154
|
+
*
|
|
155
|
+
* @param value number or string
|
|
156
|
+
* @param options as object with fractionDigits option
|
|
157
|
+
* @default fractionDigits 5
|
|
158
|
+
* @returns result as number
|
|
159
|
+
* @sumary If you provide a string NAN (not a number) 0 will be considered as value.
|
|
160
|
+
*/
|
|
161
|
+
divideBy: (value: number, opt?: CalcOpt): number =>
|
|
162
|
+
Divide(target, value, opt?.fractionDigits),
|
|
163
|
+
/**
|
|
164
|
+
* @description
|
|
165
|
+
* subtract a value for another one.
|
|
166
|
+
*
|
|
167
|
+
* @param value number or string
|
|
168
|
+
* @param options as object with fractionDigits option
|
|
169
|
+
* @default fractionDigits 5
|
|
170
|
+
* @returns result as number
|
|
171
|
+
* @sumary If you provide a string NAN (not a number) 0 will be considered as value.
|
|
172
|
+
*/
|
|
173
|
+
subtract: (value: number, opt?: CalcOpt): number =>
|
|
174
|
+
Subtract(target, value, opt?.fractionDigits),
|
|
175
|
+
/**
|
|
176
|
+
* @description
|
|
177
|
+
* sum a value with another one.
|
|
178
|
+
*
|
|
179
|
+
* @param value number or string
|
|
180
|
+
* @param options as object with fractionDigits option
|
|
181
|
+
* @default fractionDigits 5
|
|
182
|
+
* @returns result as number
|
|
183
|
+
* @sumary If you provide a string NAN (not a number) 0 will be considered as value.
|
|
184
|
+
*/
|
|
185
|
+
sum: (value: number, opt?: CalcOpt): number =>
|
|
186
|
+
Sum(target, value, opt?.fractionDigits),
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* @description
|
|
192
|
+
* Provides string manipulation utilities, including removing or replacing characters.
|
|
193
|
+
*
|
|
194
|
+
* @param target The string to manipulate.
|
|
195
|
+
* @returns An object with methods for character removal, space removal, and replacement.
|
|
196
|
+
*/
|
|
197
|
+
string(target: string) {
|
|
198
|
+
return {
|
|
199
|
+
/**
|
|
200
|
+
* @description
|
|
201
|
+
* remove all special chars from a string.
|
|
202
|
+
*
|
|
203
|
+
* @returns string with no special chars.
|
|
204
|
+
* @summary special chars: based on ascii table
|
|
205
|
+
* @example
|
|
206
|
+
* (asciiCode >= 33 && asciiCode <= 47) ||
|
|
207
|
+
* (asciiCode >= 58 && asciiCode <= 64) ||
|
|
208
|
+
* (asciiCode >= 91 && asciiCode <= 96) ||
|
|
209
|
+
* (asciiCode >= 123 && asciiCode <= 126)
|
|
210
|
+
*/
|
|
211
|
+
removeSpecialChars: () =>
|
|
212
|
+
RemoveChars(target, (char: string) =>
|
|
213
|
+
Utils.validator.string(char).isSpecialChar(),
|
|
214
|
+
),
|
|
215
|
+
/**
|
|
216
|
+
* @description
|
|
217
|
+
* remove all spaces from text.
|
|
218
|
+
*
|
|
219
|
+
* @returns string with no spaces
|
|
220
|
+
*/
|
|
221
|
+
removeSpaces: (): string => RemoveSpaces(target),
|
|
222
|
+
/**
|
|
223
|
+
* @description
|
|
224
|
+
* remove all numbers from a text
|
|
225
|
+
*
|
|
226
|
+
* @returns string with no numbers.
|
|
227
|
+
*/
|
|
228
|
+
removeNumbers: (): string =>
|
|
229
|
+
RemoveChars(target, (char: string) =>
|
|
230
|
+
Utils.validator.string(char).hasOnlyNumbers(),
|
|
231
|
+
),
|
|
232
|
+
/**
|
|
233
|
+
* @description
|
|
234
|
+
* remove only specific character from a text.
|
|
235
|
+
*
|
|
236
|
+
* @param char character to be removed.
|
|
237
|
+
* @returns string without char.
|
|
238
|
+
* @memberof replace. If you need to remove a word or more than one character from a text use replace function.
|
|
239
|
+
* @see replace
|
|
240
|
+
*/
|
|
241
|
+
removeChar: (char: string) =>
|
|
242
|
+
RemoveChars(target, (val: string) => val === char),
|
|
243
|
+
/**
|
|
244
|
+
* @description
|
|
245
|
+
* replace any character or word from an string for some provided value.
|
|
246
|
+
*
|
|
247
|
+
* @param char character or word to be replaced.
|
|
248
|
+
* @param value character or word to be aplied.
|
|
249
|
+
* @returns text with replaced value.
|
|
250
|
+
* @example
|
|
251
|
+
* string("hello world").replace("world").to("dear");
|
|
252
|
+
*/
|
|
253
|
+
replace: (char: string) => ({
|
|
254
|
+
to: (value: string | number) => ReplaceChars(target, char, value),
|
|
255
|
+
}),
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export default Utils.create();
|
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import type { Aggregate, Entity, ID, ValueObject } from "../core";
|
|
2
|
+
import type { AnyObject } from "../types/utils.types";
|
|
3
|
+
import { Stringify } from "../utils/string.utils";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @description
|
|
7
|
+
* A utility class for validating various data types, including numbers, strings, objects, dates, and more.
|
|
8
|
+
* Provides methods to check the type and properties of values, as well as to perform specific validations.
|
|
9
|
+
*/
|
|
10
|
+
export class Validator {
|
|
11
|
+
private static instance: Validator = null as unknown as Validator;
|
|
12
|
+
|
|
13
|
+
private constructor() {}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @description
|
|
17
|
+
* Creates or retrieves the singleton instance of the `Validator` class.
|
|
18
|
+
*
|
|
19
|
+
* @returns {Validator} The instance of the `Validator` class.
|
|
20
|
+
*/
|
|
21
|
+
public static create(): Validator {
|
|
22
|
+
if (!Validator.instance) Validator.instance = new Validator();
|
|
23
|
+
return Validator.instance;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @description
|
|
28
|
+
* Checks if the provided value is an aggregate.
|
|
29
|
+
*
|
|
30
|
+
* @param props The value to check.
|
|
31
|
+
* @returns {boolean} True if the value is an aggregate, false otherwise.
|
|
32
|
+
*/
|
|
33
|
+
public isAggregate(props: unknown): props is Aggregate<never> {
|
|
34
|
+
if (props === null || typeof props !== "object") return false;
|
|
35
|
+
return (props as AnyObject).__aggregate === true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @description
|
|
40
|
+
* Checks if the provided value is an array.
|
|
41
|
+
*
|
|
42
|
+
* @param props The value to check.
|
|
43
|
+
* @returns {boolean} True if the value is an array, false otherwise.
|
|
44
|
+
*/
|
|
45
|
+
public isArray(props: unknown): props is unknown[] {
|
|
46
|
+
return Array.isArray(props);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @description
|
|
51
|
+
* Checks if the provided value is a boolean.
|
|
52
|
+
*
|
|
53
|
+
* @param props The value to check.
|
|
54
|
+
* @returns {boolean} True if the value is a boolean, false otherwise.
|
|
55
|
+
*/
|
|
56
|
+
public isBoolean(props: unknown): props is boolean {
|
|
57
|
+
return typeof props === "boolean";
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @description
|
|
62
|
+
* Checks if the provided value is a date.
|
|
63
|
+
*
|
|
64
|
+
* @param props The value to check.
|
|
65
|
+
* @returns {boolean} True if the value is a date, false otherwise.
|
|
66
|
+
*/
|
|
67
|
+
public isDate(props: unknown): props is Date {
|
|
68
|
+
return props instanceof Date;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @description
|
|
73
|
+
* Checks if the provided value is an entity (but not an aggregate).
|
|
74
|
+
*
|
|
75
|
+
* @param props The value to check.
|
|
76
|
+
* @returns {boolean} True if the value is an entity, false otherwise.
|
|
77
|
+
*/
|
|
78
|
+
public isEntity(props: unknown): props is Entity<never> {
|
|
79
|
+
if (props === null || typeof props !== "object") return false;
|
|
80
|
+
return (props as AnyObject).__kind === "Entity";
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @description
|
|
85
|
+
* Checks if the provided value is a function.
|
|
86
|
+
*
|
|
87
|
+
* @param props The value to check.
|
|
88
|
+
* @returns {boolean} True if the value is a function, false otherwise.
|
|
89
|
+
*/
|
|
90
|
+
public isFunction(props: unknown): props is (...args: unknown[]) => unknown {
|
|
91
|
+
return typeof props === "function";
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @description
|
|
96
|
+
* Checks if the provided value is an ID instance.
|
|
97
|
+
*
|
|
98
|
+
* @param props The value to check.
|
|
99
|
+
* @returns {boolean} True if the value is an ID, false otherwise.
|
|
100
|
+
*/
|
|
101
|
+
public isID(props: unknown): props is ID {
|
|
102
|
+
if (props === null || typeof props !== "object") return false;
|
|
103
|
+
return (props as AnyObject).__kind === "ID";
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @description
|
|
108
|
+
* Checks if the provided value is null.
|
|
109
|
+
*
|
|
110
|
+
* @param props The value to check.
|
|
111
|
+
* @returns {boolean} True if the value is null, false otherwise.
|
|
112
|
+
*/
|
|
113
|
+
public isNull(props: unknown): props is null {
|
|
114
|
+
return props === null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @description
|
|
119
|
+
* Checks if the provided value is a number.
|
|
120
|
+
*
|
|
121
|
+
* @param props The value to check.
|
|
122
|
+
* @returns {boolean} True if the value is a number, false otherwise.
|
|
123
|
+
*/
|
|
124
|
+
public isNumber(props: unknown): props is number {
|
|
125
|
+
return typeof props === "number";
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* @description
|
|
130
|
+
* Checks if the provided value is an object, excluding arrays, entities, aggregates, and value objects.
|
|
131
|
+
*
|
|
132
|
+
* @param props The value to check.
|
|
133
|
+
* @returns {boolean} True if the value is an object, false otherwise.
|
|
134
|
+
*/
|
|
135
|
+
public isObject(props: unknown): props is Record<string, unknown> {
|
|
136
|
+
const isObj = typeof props === "object";
|
|
137
|
+
if (!isObj || props === null) return false;
|
|
138
|
+
if (Stringify(props) === Stringify({})) return true;
|
|
139
|
+
const hasKeys = Object.keys(props).length > 0;
|
|
140
|
+
const isNotArray = !Validator.instance.isArray(props);
|
|
141
|
+
const isNotEntity = !Validator.instance.isEntity(props);
|
|
142
|
+
const isNotAggregate = !Validator.instance.isAggregate(props);
|
|
143
|
+
const isNotValueObject = !Validator.instance.isValueObject(props);
|
|
144
|
+
const isNotId = !Validator.instance.isID(props);
|
|
145
|
+
return (
|
|
146
|
+
hasKeys &&
|
|
147
|
+
isNotAggregate &&
|
|
148
|
+
isNotArray &&
|
|
149
|
+
isNotEntity &&
|
|
150
|
+
isNotValueObject &&
|
|
151
|
+
isNotId
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* @description
|
|
157
|
+
* Checks if a character at a specified index is a special character based on ASCII codes.
|
|
158
|
+
*
|
|
159
|
+
* @param char The character to check.
|
|
160
|
+
* @param index The index of the character.
|
|
161
|
+
* @returns {boolean} True if the character is a special character, false otherwise.
|
|
162
|
+
*/
|
|
163
|
+
private static isSpecialChar(char: string, index: number): boolean {
|
|
164
|
+
const asciiCode = char.charCodeAt(index);
|
|
165
|
+
return (
|
|
166
|
+
(asciiCode >= 33 && asciiCode <= 47) ||
|
|
167
|
+
(asciiCode >= 58 && asciiCode <= 64) ||
|
|
168
|
+
(asciiCode >= 91 && asciiCode <= 96) ||
|
|
169
|
+
(asciiCode >= 123 && asciiCode <= 126)
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* @description
|
|
175
|
+
* Checks if the provided value is a symbol.
|
|
176
|
+
*
|
|
177
|
+
* @param props The value to check.
|
|
178
|
+
* @returns {boolean} True if the value is a symbol, false otherwise.
|
|
179
|
+
*/
|
|
180
|
+
public isSymbol(props: unknown): props is symbol {
|
|
181
|
+
return typeof props === "symbol";
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* @description
|
|
186
|
+
* Checks if the provided value is a string.
|
|
187
|
+
*
|
|
188
|
+
* @param props The value to check.
|
|
189
|
+
* @returns {boolean} True if the value is a string, false otherwise.
|
|
190
|
+
*/
|
|
191
|
+
public isString(props: unknown): props is string {
|
|
192
|
+
return typeof props === "string";
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* @description
|
|
197
|
+
* Checks if the provided value is undefined.
|
|
198
|
+
*
|
|
199
|
+
* @param props The value to check.
|
|
200
|
+
* @returns {boolean} True if the value is undefined, false otherwise.
|
|
201
|
+
*/
|
|
202
|
+
public isUndefined(props: unknown): props is undefined {
|
|
203
|
+
return typeof props === "undefined";
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* @description
|
|
208
|
+
* Checks if the provided value is a value object.
|
|
209
|
+
*
|
|
210
|
+
* @param props The value to check.
|
|
211
|
+
* @returns {boolean} True if the value is a value object, false otherwise.
|
|
212
|
+
*/
|
|
213
|
+
public isValueObject(props: unknown): props is ValueObject<never> {
|
|
214
|
+
if (props === null || typeof props !== "object") return false;
|
|
215
|
+
return (props as AnyObject).__kind === "ValueObject";
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
public date(target: Date) {
|
|
219
|
+
return {
|
|
220
|
+
isBeforeThan: (value: Date): boolean =>
|
|
221
|
+
Validator.instance.isDate(target) &&
|
|
222
|
+
Validator.instance.isDate(value) &&
|
|
223
|
+
target.getTime() < value.getTime(),
|
|
224
|
+
isBeforeOrEqualTo: (value: Date): boolean =>
|
|
225
|
+
Validator.instance.isDate(target) &&
|
|
226
|
+
Validator.instance.isDate(value) &&
|
|
227
|
+
target.getTime() <= value.getTime(),
|
|
228
|
+
isAfterNow: (): boolean =>
|
|
229
|
+
Validator.instance.isDate(target) && target.getTime() > Date.now(),
|
|
230
|
+
isBeforeNow: (): boolean =>
|
|
231
|
+
Validator.instance.isDate(target) && target.getTime() < Date.now(),
|
|
232
|
+
isBetween: (start: Date, end: Date): boolean =>
|
|
233
|
+
Validator.instance.isDate(target) &&
|
|
234
|
+
target.getTime() > start.getTime() &&
|
|
235
|
+
target.getTime() < end.getTime(),
|
|
236
|
+
isWeekend: (): boolean =>
|
|
237
|
+
(Validator.instance.isDate(target) && target.getDay() === 0) ||
|
|
238
|
+
(Validator.instance.isDate(target) && target.getDay() === 6),
|
|
239
|
+
isAfterThan: (value: Date): boolean =>
|
|
240
|
+
Validator.instance.isDate(target) &&
|
|
241
|
+
Validator.instance.isDate(value) &&
|
|
242
|
+
target.getTime() > value.getTime(),
|
|
243
|
+
isAfterOrEqualTo: (value: Date): boolean =>
|
|
244
|
+
Validator.instance.isDate(target) &&
|
|
245
|
+
Validator.instance.isDate(value) &&
|
|
246
|
+
target.getTime() >= value.getTime(),
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
public number(target: number) {
|
|
251
|
+
return {
|
|
252
|
+
isEqualTo: (value: number): boolean =>
|
|
253
|
+
Validator.instance.isNumber(target) &&
|
|
254
|
+
Validator.instance.isNumber(value) &&
|
|
255
|
+
target === value,
|
|
256
|
+
isGreaterThan: (value: number): boolean =>
|
|
257
|
+
Validator.instance.isNumber(target) &&
|
|
258
|
+
Validator.instance.isNumber(value) &&
|
|
259
|
+
target > value,
|
|
260
|
+
isLessThan: (value: number): boolean =>
|
|
261
|
+
Validator.instance.isNumber(target) &&
|
|
262
|
+
Validator.instance.isNumber(value) &&
|
|
263
|
+
target < value,
|
|
264
|
+
isLessOrEqualTo: (value: number): boolean =>
|
|
265
|
+
Validator.instance.isNumber(target) &&
|
|
266
|
+
Validator.instance.isNumber(value) &&
|
|
267
|
+
target <= value,
|
|
268
|
+
isGreaterOrEqualTo: (value: number): boolean =>
|
|
269
|
+
Validator.instance.isNumber(target) &&
|
|
270
|
+
Validator.instance.isNumber(value) &&
|
|
271
|
+
target >= value,
|
|
272
|
+
isSafeInteger: (): boolean =>
|
|
273
|
+
Validator.instance.isNumber(target) &&
|
|
274
|
+
target <= Number.MAX_SAFE_INTEGER &&
|
|
275
|
+
target >= Number.MIN_SAFE_INTEGER,
|
|
276
|
+
isPositive: (): boolean =>
|
|
277
|
+
Validator.instance.isNumber(target) && target >= 0,
|
|
278
|
+
isNegative: (): boolean =>
|
|
279
|
+
Validator.instance.isNumber(target) && target < 0,
|
|
280
|
+
isEven: (): boolean =>
|
|
281
|
+
Validator.instance.isNumber(target) && target % 2 === 0,
|
|
282
|
+
isInteger: (): boolean =>
|
|
283
|
+
Validator.instance.isNumber(target) &&
|
|
284
|
+
target - Math.trunc(target) === 0,
|
|
285
|
+
isBetween: (min: number, max: number): boolean =>
|
|
286
|
+
Validator.instance.isNumber(target) && target < max && target > min,
|
|
287
|
+
isBetweenOrEqual: (min: number, max: number): boolean =>
|
|
288
|
+
Validator.instance.isNumber(target) && target <= max && target >= min,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
public string(target: string) {
|
|
293
|
+
return {
|
|
294
|
+
isSpecialChar: (index = 0): boolean =>
|
|
295
|
+
Validator.instance.isString(target[index]) &&
|
|
296
|
+
Validator.isSpecialChar(target, index),
|
|
297
|
+
hasSpecialChar: (): boolean =>
|
|
298
|
+
Validator.instance.isString(target) &&
|
|
299
|
+
target
|
|
300
|
+
.split("")
|
|
301
|
+
.map((char) => Validator.isSpecialChar(char, 0))
|
|
302
|
+
.includes(true),
|
|
303
|
+
hasLengthGreaterThan: (length: number): boolean =>
|
|
304
|
+
Validator.instance.isString(target) && target.length > length,
|
|
305
|
+
hasLengthGreaterOrEqualTo: (length: number): boolean =>
|
|
306
|
+
Validator.instance.isString(target) && target.length >= length,
|
|
307
|
+
hasLengthLessThan: (length: number): boolean =>
|
|
308
|
+
Validator.instance.isString(target) && target.length < length,
|
|
309
|
+
hasLengthLessOrEqualTo: (length: number): boolean =>
|
|
310
|
+
Validator.instance.isString(target) && target.length <= length,
|
|
311
|
+
hasLengthEqualTo: (length: number): boolean =>
|
|
312
|
+
Validator.instance.isString(target) && target.length === length,
|
|
313
|
+
hasLengthBetween: (min: number, max: number): boolean =>
|
|
314
|
+
Validator.instance.isString(target) &&
|
|
315
|
+
target.length > min &&
|
|
316
|
+
target.length < max,
|
|
317
|
+
hasLengthBetweenOrEqual: (min: number, max: number): boolean =>
|
|
318
|
+
Validator.instance.isString(target) &&
|
|
319
|
+
target.length >= min &&
|
|
320
|
+
target.length <= max,
|
|
321
|
+
includes: (value: string): boolean =>
|
|
322
|
+
(Validator.instance.isString(target) && target.includes(value)) ||
|
|
323
|
+
value
|
|
324
|
+
.split("")
|
|
325
|
+
.map((char) => target.includes(char))
|
|
326
|
+
.includes(true),
|
|
327
|
+
isEmpty: (): boolean =>
|
|
328
|
+
Validator.instance.isUndefined(target) ||
|
|
329
|
+
Validator.instance.isNull(target) ||
|
|
330
|
+
(Validator.instance.isString(target) && target.trim() === ""),
|
|
331
|
+
match: (regex: RegExp): boolean => regex.test(target),
|
|
332
|
+
hasOnlyNumbers: (): boolean =>
|
|
333
|
+
Validator.instance.isString(target) &&
|
|
334
|
+
target
|
|
335
|
+
.split("")
|
|
336
|
+
.map((n) => n.charCodeAt(0) >= 48 && n.charCodeAt(0) <= 57)
|
|
337
|
+
.every((v) => v === true),
|
|
338
|
+
hasOnlyLetters: (): boolean =>
|
|
339
|
+
Validator.instance.isString(target) &&
|
|
340
|
+
target
|
|
341
|
+
.toUpperCase()
|
|
342
|
+
.split("")
|
|
343
|
+
.map((n) => n.charCodeAt(0) >= 65 && n.charCodeAt(0) <= 90)
|
|
344
|
+
.every((v) => v === true),
|
|
345
|
+
isEqual: (value: string) =>
|
|
346
|
+
Validator.instance.isString(target) &&
|
|
347
|
+
Validator.instance.isString(value) &&
|
|
348
|
+
target === value,
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export default Validator.create();
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { IResult } from "./result.types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @description
|
|
5
|
+
* Represents a simpler adapter for transforming objects.
|
|
6
|
+
*
|
|
7
|
+
* @template A The input type.
|
|
8
|
+
* @template B The output type.
|
|
9
|
+
*/
|
|
10
|
+
export interface Adapter<A = unknown, B = unknown> {
|
|
11
|
+
adaptOne(item: A): B;
|
|
12
|
+
adaptMany?(items: Array<A>): Array<B>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @description
|
|
17
|
+
* Represents an adapter that transforms one type to another.
|
|
18
|
+
*
|
|
19
|
+
* @template F The input type.
|
|
20
|
+
* @template T The output type.
|
|
21
|
+
* @template E The error type (default: void).
|
|
22
|
+
* @template M The metadata type (default: void).
|
|
23
|
+
*/
|
|
24
|
+
export interface IAdapter<F, T, E = void, M = void> {
|
|
25
|
+
build(target: F): IResult<T, E, M>;
|
|
26
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { IResult } from "./result.types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @description
|
|
5
|
+
* Represents a command — an operation that changes state and returns a Result.
|
|
6
|
+
*
|
|
7
|
+
* @template Input The input type.
|
|
8
|
+
* @template Output The output type.
|
|
9
|
+
*/
|
|
10
|
+
export interface ICommand<Input = void, Output = void> {
|
|
11
|
+
execute(input: Input): Promise<IResult<Output, string>>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @description
|
|
16
|
+
* Represents a query — a read-only operation that returns data without changing state.
|
|
17
|
+
* Queries should never produce side effects.
|
|
18
|
+
*
|
|
19
|
+
* @template Input The query parameters type.
|
|
20
|
+
* @template Output The returned data type.
|
|
21
|
+
*/
|
|
22
|
+
export interface IQuery<Input = void, Output = void> {
|
|
23
|
+
execute(input: Input): Promise<IResult<Output, string>>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @description
|
|
28
|
+
* Convenience alias — a use case is either a Command or a Query.
|
|
29
|
+
* Use `ICommand` when the operation mutates state.
|
|
30
|
+
* Use `IQuery` when the operation is read-only.
|
|
31
|
+
*
|
|
32
|
+
* @template Input The input type.
|
|
33
|
+
* @template Output The output type.
|
|
34
|
+
*/
|
|
35
|
+
export type IUseCase<Input = void, Output = void> =
|
|
36
|
+
| ICommand<Input, Output>
|
|
37
|
+
| IQuery<Input, Output>;
|