@haskou/value-objects 1.0.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.
- package/.editorconfig +15 -0
- package/.eslintignore +3 -0
- package/.eslintrc.json +218 -0
- package/.prettierrc.json +13 -0
- package/README.md +156 -0
- package/TECHNICAL_DOCUMENTATION.md +1091 -0
- package/jest.config.ts +30 -0
- package/package.json +49 -0
- package/src/errors/BaseError.ts +20 -0
- package/src/errors/DomainError.ts +12 -0
- package/src/errors/InvalidColorError.ts +7 -0
- package/src/errors/InvalidDayError.ts +7 -0
- package/src/errors/InvalidDayFormatError.ts +7 -0
- package/src/errors/InvalidEmailError.ts +7 -0
- package/src/errors/InvalidHourError.ts +7 -0
- package/src/errors/InvalidIntegerError.ts +7 -0
- package/src/errors/InvalidLatitudeError.ts +7 -0
- package/src/errors/InvalidLongitudeError.ts +7 -0
- package/src/errors/InvalidMinutesError.ts +7 -0
- package/src/errors/InvalidNumberError.ts +7 -0
- package/src/errors/InvalidPositiveNumberError.ts +7 -0
- package/src/errors/InvalidStringLengthError.ts +9 -0
- package/src/errors/InvalidTimestampIntervalError.ts +10 -0
- package/src/errors/NullObjectError.ts +8 -0
- package/src/errors/ValueNotInEnumError.ts +9 -0
- package/src/errors/index.ts +17 -0
- package/src/index.ts +5 -0
- package/src/interfaces/PrimitiveOf.ts +5 -0
- package/src/interfaces/index.ts +1 -0
- package/src/patterns/Assert.ts +15 -0
- package/src/patterns/NullObject.ts +60 -0
- package/src/patterns/ValueObject.ts +40 -0
- package/src/patterns/index.ts +3 -0
- package/src/types/Nullish.ts +1 -0
- package/src/types/Primitive.ts +1 -0
- package/src/types/index.ts +2 -0
- package/src/value-objects/Color.ts +39 -0
- package/src/value-objects/Email.ts +23 -0
- package/src/value-objects/Enum.ts +31 -0
- package/src/value-objects/Integer.ts +22 -0
- package/src/value-objects/NumberValueObject.ts +56 -0
- package/src/value-objects/PositiveNumber.ts +20 -0
- package/src/value-objects/StringValueObject.ts +27 -0
- package/src/value-objects/coordinates/Coordinates.ts +30 -0
- package/src/value-objects/coordinates/Latitude.ts +22 -0
- package/src/value-objects/coordinates/Longitude.ts +25 -0
- package/src/value-objects/coordinates/index.ts +3 -0
- package/src/value-objects/index.ts +9 -0
- package/src/value-objects/time/CalendarDay.ts +91 -0
- package/src/value-objects/time/Day.ts +17 -0
- package/src/value-objects/time/DayOfWeek.ts +60 -0
- package/src/value-objects/time/Duration.ts +142 -0
- package/src/value-objects/time/Hour.ts +105 -0
- package/src/value-objects/time/Month.ts +39 -0
- package/src/value-objects/time/MonthOfYear.ts +52 -0
- package/src/value-objects/time/Timestamp.ts +208 -0
- package/src/value-objects/time/TimestampInterval.ts +122 -0
- package/src/value-objects/time/Year.ts +27 -0
- package/src/value-objects/time/index.ts +10 -0
- package/tests/errors/BaseError.spec.ts +63 -0
- package/tests/errors/DomainError.spec.ts +52 -0
- package/tests/patterns/Assert.spec.ts +29 -0
- package/tests/patterns/NullObject.spec.ts +55 -0
- package/tests/setup.jest.ts +2 -0
- package/tests/value-objects/Color.spec.ts +214 -0
- package/tests/value-objects/Email.spec.ts +145 -0
- package/tests/value-objects/Enum.spec.ts +293 -0
- package/tests/value-objects/Integer.spec.ts +38 -0
- package/tests/value-objects/NumberValueObject.spec.ts +446 -0
- package/tests/value-objects/PositiveNumber.spec.ts +274 -0
- package/tests/value-objects/StringValueObject.spec.ts +135 -0
- package/tests/value-objects/coordinates/Coordinates.spec.ts +90 -0
- package/tests/value-objects/coordinates/Latitude.spec.ts +24 -0
- package/tests/value-objects/coordinates/Longitude.spec.ts +24 -0
- package/tests/value-objects/time/CalendarDay.spec.ts +182 -0
- package/tests/value-objects/time/Day.spec.ts +29 -0
- package/tests/value-objects/time/DayOfWeek.spec.ts +71 -0
- package/tests/value-objects/time/Duration.spec.ts +278 -0
- package/tests/value-objects/time/Hour.spec.ts +197 -0
- package/tests/value-objects/time/MonthOfYear.spec.ts +111 -0
- package/tests/value-objects/time/Timestamp.spec.ts +497 -0
- package/tests/value-objects/time/TimestampInterval.spec.ts +383 -0
- package/tests/value-objects/time/Year.spec.ts +48 -0
- package/tsconfig.jest.json +33 -0
- package/tsconfig.json +42 -0
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { Hour, InvalidHourError, InvalidMinutesError } from '../../../src';
|
|
2
|
+
|
|
3
|
+
describe('Hour', () => {
|
|
4
|
+
describe('constructor', () => {
|
|
5
|
+
describe('when value is a string', () => {
|
|
6
|
+
it('should return a Hour instance', () => {
|
|
7
|
+
const hour = new Hour('23:59');
|
|
8
|
+
expect(hour).toBeInstanceOf(Hour);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('should throw error if hour is invalid', () => {
|
|
12
|
+
expect(() => new Hour('24:00')).toThrow('Invalid hour');
|
|
13
|
+
expect(() => new Hour('24:00')).toThrow(InvalidHourError);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('should throw error if minutes is invalid', () => {
|
|
17
|
+
expect(() => new Hour('23:60')).toThrow('Invalid minutes');
|
|
18
|
+
expect(() => new Hour('23:60')).toThrow(InvalidMinutesError);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should work with 0 hour', () => {
|
|
22
|
+
expect(new Hour('00:00').toString()).toEqual('00:00');
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe('when receives 2 numbers', () => {
|
|
27
|
+
it('should return a Hour instance', () => {
|
|
28
|
+
const hour = new Hour(23, 59);
|
|
29
|
+
expect(hour).toBeInstanceOf(Hour);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should throw error if hour is invalid', () => {
|
|
33
|
+
expect(() => new Hour(24, 0)).toThrow('Invalid hour');
|
|
34
|
+
expect(() => new Hour(24, 0)).toThrow(InvalidHourError);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should throw error if minutes is invalid', () => {
|
|
38
|
+
expect(() => new Hour(23, 60)).toThrow('Invalid minutes');
|
|
39
|
+
expect(() => new Hour(23, 60)).toThrow(InvalidMinutesError);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should work with 0 hour', () => {
|
|
43
|
+
const hour = new Hour(0, 0).toString();
|
|
44
|
+
expect(hour).toEqual('00:00');
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe('addMinutes', () => {
|
|
50
|
+
it('should add minutes to hour', () => {
|
|
51
|
+
expect(new Hour('23:59').addMinutes(1).toString()).toEqual('00:00');
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('toString', () => {
|
|
56
|
+
it('should return string value', () => {
|
|
57
|
+
expect(new Hour('23:59').toString()).toEqual('23:59');
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('isEqual', () => {
|
|
62
|
+
it('should return true if value is equal', () => {
|
|
63
|
+
expect(new Hour('23:59').isEqual(new Hour('23:59'))).toBeTrue();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('should return false if value is not equal', () => {
|
|
67
|
+
expect(new Hour('23:59').isEqual(new Hour('00:00'))).toBeFalse();
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('with some values', () => {
|
|
72
|
+
it('should not throw error', () => {
|
|
73
|
+
expect(() => new Hour('23:59')).not.toThrow();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('should add trail zero to hours', () => {
|
|
77
|
+
expect(new Hour('1:59').toString()).toEqual('01:59');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should add trail zero to minutes', () => {
|
|
81
|
+
expect(new Hour('23:9').toString()).toEqual('23:09');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should add minutes to hours', () => {
|
|
85
|
+
expect(new Hour('23:59').addMinutes(1).toString()).toEqual('00:00');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('should add hours to hours', () => {
|
|
89
|
+
expect(new Hour('23:59').addMinutes(60).toString()).toEqual('00:59');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('should subtract hours to hours', () => {
|
|
93
|
+
expect(new Hour('23:59').addMinutes(-60).toString()).toEqual('22:59');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('should subtract minutes to hours', () => {
|
|
97
|
+
expect(new Hour('00:00').addMinutes(-1).toString()).toEqual('23:59');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should add more than 2 hours', () => {
|
|
101
|
+
expect(new Hour('23:59').addMinutes(120).toString()).toEqual('01:59');
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
describe('diffInMinutes', () => {
|
|
106
|
+
it('should return diff in minutes when jump the hour day', () => {
|
|
107
|
+
expect(new Hour('23:59').diffInMinutes(new Hour('00:00'))).toEqual(1);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('should return diff in minutes in total', () => {
|
|
111
|
+
expect(new Hour('00:00').diffInMinutes(new Hour('23:59'))).toEqual(1439);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('should return 60 minutes when diff is 1 hour', () => {
|
|
115
|
+
expect(new Hour('00:00').diffInMinutes(new Hour('01:00'))).toEqual(60);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('should return 1380 minutes when diff is 23 hours', () => {
|
|
119
|
+
expect(new Hour('01:00').diffInMinutes(new Hour('00:00'))).toEqual(1380);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('should return 1439 minutes when diff is -1 minute from start', () => {
|
|
123
|
+
expect(new Hour('01:05').diffInMinutes(new Hour('01:04'))).toEqual(1439);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe('getMinutes', () => {
|
|
128
|
+
it('should return minutes', () => {
|
|
129
|
+
expect(new Hour('23:59').getMinutes()).toEqual(59);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe('getHours', () => {
|
|
134
|
+
it('should return hours', () => {
|
|
135
|
+
expect(new Hour('23:59').getHours()).toEqual(23);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe('isGreaterThan', () => {
|
|
140
|
+
it('should return true if the hour is greater', () => {
|
|
141
|
+
const hour1 = new Hour('12:30');
|
|
142
|
+
const hour2 = new Hour('10:45');
|
|
143
|
+
expect(hour1.isGreaterThan(hour2)).toBeTrue();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('should return false if the hour is equal', () => {
|
|
147
|
+
const hour1 = new Hour('12:30');
|
|
148
|
+
const hour2 = new Hour('12:30');
|
|
149
|
+
expect(hour1.isGreaterThan(hour2)).toBeFalse();
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('should return false if the hour is less', () => {
|
|
153
|
+
const hour1 = new Hour('12:30');
|
|
154
|
+
const hour2 = new Hour('14:45');
|
|
155
|
+
expect(hour1.isGreaterThan(hour2)).toBeFalse();
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('should return true if the hour is greater with minutes', () => {
|
|
159
|
+
const hour1 = new Hour('12:30');
|
|
160
|
+
const hour2 = new Hour('12:15');
|
|
161
|
+
expect(hour1.isGreaterThan(hour2)).toBeTrue();
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('should return false if the hour is less with minutes', () => {
|
|
165
|
+
const hour1 = new Hour('12:30');
|
|
166
|
+
const hour2 = new Hour('12:45');
|
|
167
|
+
expect(hour1.isGreaterThan(hour2)).toBeFalse();
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
describe('isLessThan', () => {
|
|
171
|
+
it('should return true if the hour is less', () => {
|
|
172
|
+
const hour1 = new Hour('10:45');
|
|
173
|
+
const hour2 = new Hour('12:30');
|
|
174
|
+
expect(hour1.isLessThan(hour2)).toBeTrue();
|
|
175
|
+
});
|
|
176
|
+
it('should return false if the hour is equal', () => {
|
|
177
|
+
const hour1 = new Hour('12:30');
|
|
178
|
+
const hour2 = new Hour('12:30');
|
|
179
|
+
expect(hour1.isLessThan(hour2)).toBeFalse();
|
|
180
|
+
});
|
|
181
|
+
it('should return false if the hour is greater', () => {
|
|
182
|
+
const hour1 = new Hour('14:45');
|
|
183
|
+
const hour2 = new Hour('12:30');
|
|
184
|
+
expect(hour1.isLessThan(hour2)).toBeFalse();
|
|
185
|
+
});
|
|
186
|
+
it('should return true if the hour is less with minutes', () => {
|
|
187
|
+
const hour1 = new Hour('12:15');
|
|
188
|
+
const hour2 = new Hour('12:30');
|
|
189
|
+
expect(hour1.isLessThan(hour2)).toBeTrue();
|
|
190
|
+
});
|
|
191
|
+
it('should return false if the hour is greater with minutes', () => {
|
|
192
|
+
const hour1 = new Hour('12:45');
|
|
193
|
+
const hour2 = new Hour('12:30');
|
|
194
|
+
expect(hour1.isLessThan(hour2)).toBeFalse();
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
});
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MonthOfYear,
|
|
3
|
+
Month,
|
|
4
|
+
Year,
|
|
5
|
+
ValueNotInEnumError,
|
|
6
|
+
InvalidIntegerError,
|
|
7
|
+
} from '../../../src';
|
|
8
|
+
|
|
9
|
+
describe('MonthOfYear', () => {
|
|
10
|
+
describe('constructor', () => {
|
|
11
|
+
it('should create a Month instance', () => {
|
|
12
|
+
const month = new MonthOfYear(1, 2022);
|
|
13
|
+
expect(month).toMatchObject({
|
|
14
|
+
month: Month.JANUARY,
|
|
15
|
+
year: new Year(2022),
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
it('should throw error if the Month is negative', () => {
|
|
19
|
+
expect(() => new MonthOfYear(-1, 2022)).toThrow(ValueNotInEnumError);
|
|
20
|
+
});
|
|
21
|
+
it('should throw error if the Month is not integer', () => {
|
|
22
|
+
expect(() => new MonthOfYear(1.1, 2022)).toThrow(ValueNotInEnumError);
|
|
23
|
+
});
|
|
24
|
+
it('should throw error if the Month is greater than 12', () => {
|
|
25
|
+
expect(() => new MonthOfYear(13, 2022)).toThrow(ValueNotInEnumError);
|
|
26
|
+
});
|
|
27
|
+
it('should throw error if the Year is not integer', () => {
|
|
28
|
+
expect(() => new MonthOfYear(1, 2022.1)).toThrow(InvalidIntegerError);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('getYear', () => {
|
|
33
|
+
it('should return the year', () => {
|
|
34
|
+
const month = new MonthOfYear(1, 2022);
|
|
35
|
+
expect(month.getYear()).toStrictEqual(new Year(2022));
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe('getDays', () => {
|
|
40
|
+
it('should return the number of days in the month', () => {
|
|
41
|
+
const month = new MonthOfYear(1, 2022);
|
|
42
|
+
expect(month.getNumberOfDays()).toBe(31);
|
|
43
|
+
});
|
|
44
|
+
it('should return the correct number of days for February', () => {
|
|
45
|
+
const month = new MonthOfYear(2, 2022);
|
|
46
|
+
expect(month.getNumberOfDays()).toBe(28);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('getTimestampInterval', () => {
|
|
51
|
+
it('should return the timestamp interval for the month', () => {
|
|
52
|
+
const month = new MonthOfYear(1, 2022);
|
|
53
|
+
const interval = month.getTimestampInterval();
|
|
54
|
+
expect(interval.getStart().valueOf()).toBe(1640995200000);
|
|
55
|
+
expect(interval.getEnd().valueOf()).toBe(1643587200000);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
describe('getMonth', () => {
|
|
59
|
+
it('should return a Month instance with the correct month value', () => {
|
|
60
|
+
const monthOfYear = new MonthOfYear(5, 2022);
|
|
61
|
+
const month = monthOfYear.getMonth();
|
|
62
|
+
expect(month).toStrictEqual(Month.MAY);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
describe('fromTimestamp', () => {
|
|
66
|
+
it('should create a MonthOfYear instance from a valid Timestamp', () => {
|
|
67
|
+
const timestamp = { getMonth: () => 5, getYear: () => 2022 } as any;
|
|
68
|
+
const monthOfYear = MonthOfYear.fromTimestamp(timestamp);
|
|
69
|
+
expect(monthOfYear.getMonth().valueOf()).toBe(5);
|
|
70
|
+
expect(monthOfYear.getYear()).toStrictEqual(new Year(2022));
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('should throw an error if the Timestamp has an invalid month', () => {
|
|
74
|
+
const timestamp = { getMonth: () => 13, getYear: () => 2022 } as any;
|
|
75
|
+
expect(() => MonthOfYear.fromTimestamp(timestamp)).toThrow(
|
|
76
|
+
ValueNotInEnumError,
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
describe('fromString', () => {
|
|
81
|
+
it('should create a MonthOfYear instance from a valid string', () => {
|
|
82
|
+
const monthOfYear = MonthOfYear.fromString('2022/05');
|
|
83
|
+
expect(monthOfYear.getMonth().valueOf()).toBe(5);
|
|
84
|
+
expect(monthOfYear.getYear()).toStrictEqual(new Year(2022));
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('should pad single digit months correctly', () => {
|
|
88
|
+
const monthOfYear = MonthOfYear.fromString('2022/1');
|
|
89
|
+
expect(monthOfYear.getMonth().valueOf()).toBe(1);
|
|
90
|
+
expect(monthOfYear.getYear()).toStrictEqual(new Year(2022));
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('should throw error for invalid month', () => {
|
|
94
|
+
expect(() => MonthOfYear.fromString('2022/13')).toThrow(
|
|
95
|
+
ValueNotInEnumError,
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('should throw error for invalid year', () => {
|
|
100
|
+
expect(() => MonthOfYear.fromString('2022.1/05')).toThrow(
|
|
101
|
+
InvalidIntegerError,
|
|
102
|
+
);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('should throw error for malformed string', () => {
|
|
106
|
+
expect(() => MonthOfYear.fromString('2022')).toThrow();
|
|
107
|
+
expect(() => MonthOfYear.fromString('')).toThrow();
|
|
108
|
+
expect(() => MonthOfYear.fromString('2022/')).toThrow();
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
});
|