@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,182 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Timestamp,
|
|
3
|
+
CalendarDay,
|
|
4
|
+
InvalidDayError,
|
|
5
|
+
Month,
|
|
6
|
+
Day,
|
|
7
|
+
DayOfWeek,
|
|
8
|
+
Year,
|
|
9
|
+
InvalidDayFormatError,
|
|
10
|
+
} from '../../../src';
|
|
11
|
+
|
|
12
|
+
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
13
|
+
describe('CalendarDay', () => {
|
|
14
|
+
describe('constructor', () => {
|
|
15
|
+
it('should create a CalendarDay ', () => {
|
|
16
|
+
const now = new Timestamp();
|
|
17
|
+
const day = new CalendarDay();
|
|
18
|
+
expect(day).toMatchObject({
|
|
19
|
+
year: now.getYear().valueOf(),
|
|
20
|
+
month: now.getMonth().valueOf(),
|
|
21
|
+
day: now.getDay().valueOf(),
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should create a day instance with a valid date string', () => {
|
|
26
|
+
const day = new CalendarDay('2022-01-01');
|
|
27
|
+
expect(day).toMatchObject({
|
|
28
|
+
year: 2022,
|
|
29
|
+
month: 1,
|
|
30
|
+
day: 1,
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should create a day instance with a valid Timestamp', () => {
|
|
35
|
+
const day = new CalendarDay(new Timestamp('2022-01-01'));
|
|
36
|
+
expect(day).toMatchObject({
|
|
37
|
+
year: 2022,
|
|
38
|
+
month: 1,
|
|
39
|
+
day: 1,
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should create a day instance with a valid Date', () => {
|
|
44
|
+
const day = new CalendarDay(new Date('2022-01-01'));
|
|
45
|
+
expect(day).toMatchObject({
|
|
46
|
+
year: 2022,
|
|
47
|
+
month: 1,
|
|
48
|
+
day: 1,
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('should create a day instance with a valid number', () => {
|
|
53
|
+
const day = new CalendarDay(1640995200000);
|
|
54
|
+
expect(day).toMatchObject({
|
|
55
|
+
year: 2022,
|
|
56
|
+
month: 1,
|
|
57
|
+
day: 1,
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should throw InvalidDateFormatError when provided with an invalid date string', () => {
|
|
62
|
+
expect(() => new CalendarDay('2022-01-001')).toThrow(
|
|
63
|
+
InvalidDayFormatError,
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should throw InvalidDateFormatError when provided with an invalid date string', () => {
|
|
68
|
+
expect(() => new CalendarDay('2022-01')).toThrow(InvalidDayFormatError);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('should throw InvalidDateFormatError when provided with an invalid date', () => {
|
|
72
|
+
expect(() => new CalendarDay(new Date(NaN))).toThrow(InvalidDayError);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('valueOf', () => {
|
|
77
|
+
it('should return the date value as a string', () => {
|
|
78
|
+
const day = new CalendarDay('2022-01-01');
|
|
79
|
+
expect(day.valueOf()).toBe('2022-01-01');
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe('isEqual', () => {
|
|
84
|
+
it('should return true when two days have the same value', () => {
|
|
85
|
+
const day1 = new CalendarDay('2022-01-01');
|
|
86
|
+
const day2 = new CalendarDay('2022-01-01');
|
|
87
|
+
expect(day1.isEqual(day2)).toBe(true);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('should return false when two days have different values', () => {
|
|
91
|
+
const day1 = new CalendarDay('2022-01-01');
|
|
92
|
+
const day2 = new CalendarDay('2022-01-02');
|
|
93
|
+
expect(day1.isEqual(day2)).toBe(false);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe('isAfter', () => {
|
|
98
|
+
it('should return true when the current day is after the provided day', () => {
|
|
99
|
+
const day1 = new CalendarDay('2022-01-02');
|
|
100
|
+
const day2 = new CalendarDay('2022-01-01');
|
|
101
|
+
expect(day1.isAfter(day2)).toBe(true);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('should return false when the current day is not after the provided day', () => {
|
|
105
|
+
const day1 = new CalendarDay('2022-01-01');
|
|
106
|
+
const day2 = new CalendarDay('2022-01-02');
|
|
107
|
+
expect(day1.isAfter(day2)).toBe(false);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe('isBefore', () => {
|
|
112
|
+
it('should return true when the current day is before the provided day', () => {
|
|
113
|
+
const day1 = new CalendarDay('2022-01-01');
|
|
114
|
+
const day2 = new CalendarDay('2022-01-02');
|
|
115
|
+
expect(day1.isBefore(day2)).toBe(true);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('should return false when the current day is not before the provided day', () => {
|
|
119
|
+
const day1 = new CalendarDay('2022-01-02');
|
|
120
|
+
const day2 = new CalendarDay('2022-01-01');
|
|
121
|
+
expect(day1.isBefore(day2)).toBe(false);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
describe('getYear', () => {
|
|
125
|
+
it('should return the year of the day', () => {
|
|
126
|
+
const day = new CalendarDay('2022-01-02');
|
|
127
|
+
expect(day.getYear()).toBe(2022);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
describe('getMonth', () => {
|
|
131
|
+
it('should return the Month of the day', () => {
|
|
132
|
+
const day = new CalendarDay('2022-01-02');
|
|
133
|
+
expect(day.getMonth()).toStrictEqual(Month.JANUARY);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
describe('getDay', () => {
|
|
137
|
+
it('should return the year of the day', () => {
|
|
138
|
+
const day = new CalendarDay('2022-01-02');
|
|
139
|
+
expect(day.getDay()).toStrictEqual(new Day(2));
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
describe('getDayOfWeek', () => {
|
|
143
|
+
it('should return the correct DayOfWeek for a given date', () => {
|
|
144
|
+
expect(new CalendarDay('2025-01-05').getDayOfWeek()).toEqual(
|
|
145
|
+
DayOfWeek.SUNDAY,
|
|
146
|
+
);
|
|
147
|
+
expect(new CalendarDay('2025-01-31').getDayOfWeek()).toEqual(
|
|
148
|
+
DayOfWeek.FRIDAY,
|
|
149
|
+
);
|
|
150
|
+
expect(new CalendarDay('2025-02-28').getDayOfWeek()).toEqual(
|
|
151
|
+
DayOfWeek.FRIDAY,
|
|
152
|
+
);
|
|
153
|
+
expect(new CalendarDay('2024-12-31').getDayOfWeek()).toEqual(
|
|
154
|
+
DayOfWeek.TUESDAY,
|
|
155
|
+
);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
describe('toTimestamp', () => {
|
|
159
|
+
it('should return the correct Timestamp instance', () => {
|
|
160
|
+
const timestamp = new Timestamp('2022-01-01');
|
|
161
|
+
const day = new CalendarDay(timestamp);
|
|
162
|
+
expect(day.toTimestamp()).toStrictEqual(timestamp);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('should return a Timestamp instance matching the date value', () => {
|
|
166
|
+
const day = new CalendarDay('2022-01-01');
|
|
167
|
+
const timestamp = day.toTimestamp();
|
|
168
|
+
expect(timestamp.valueOf()).toBe(new Timestamp('2022-01-01').valueOf());
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
describe('getMonthOfYear', () => {
|
|
172
|
+
it('should return a MonthOfYear instance with the correct month and year', () => {
|
|
173
|
+
const day = new CalendarDay('2022-01-02');
|
|
174
|
+
const month = day.getMonthOfYear();
|
|
175
|
+
expect(month).toMatchObject({
|
|
176
|
+
month: Month.JANUARY,
|
|
177
|
+
year: new Year(2022),
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Day, InvalidDayError, NumberValueObject } from '../../../src';
|
|
2
|
+
|
|
3
|
+
describe('Day', () => {
|
|
4
|
+
it('should create a valid Day instance for a value within the valid range', () => {
|
|
5
|
+
expect(() => new Day(1)).not.toThrow();
|
|
6
|
+
expect(() => new Day(15)).not.toThrow();
|
|
7
|
+
expect(() => new Day(31)).not.toThrow();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('should throw InvalidDayError for a value less than 1', () => {
|
|
11
|
+
expect(() => new Day(0)).toThrow(InvalidDayError);
|
|
12
|
+
expect(() => new Day(-1)).toThrow(InvalidDayError);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('should throw InvalidDayError for a value greater than 31', () => {
|
|
16
|
+
expect(() => new Day(32)).toThrow(InvalidDayError);
|
|
17
|
+
expect(() => new Day(100)).toThrow(InvalidDayError);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should accept a NumberValueObject as input', () => {
|
|
21
|
+
expect(() => new Day(new NumberValueObject(3))).not.toThrow();
|
|
22
|
+
});
|
|
23
|
+
it('should be not equal to another Day with a different value', () => {
|
|
24
|
+
const day1 = new Day(1);
|
|
25
|
+
const day2 = new Day(2);
|
|
26
|
+
expect(day1.isEqual(day2)).toBe(false);
|
|
27
|
+
expect(day1.isNotEqual(day2)).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { DayOfWeek, Timestamp, EDaysOfWeek } from '../../../src';
|
|
2
|
+
|
|
3
|
+
describe('DayOfWeek', () => {
|
|
4
|
+
describe('fromNumber', () => {
|
|
5
|
+
it('should create the correct Day instance from a number', () => {
|
|
6
|
+
expect(
|
|
7
|
+
DayOfWeek.fromNumber(new Timestamp('2024-05-27').getDayOfWeek()),
|
|
8
|
+
).toEqual(DayOfWeek.MONDAY);
|
|
9
|
+
expect(
|
|
10
|
+
DayOfWeek.fromNumber(new Timestamp('2024-05-28').getDayOfWeek()),
|
|
11
|
+
).toEqual(DayOfWeek.TUESDAY);
|
|
12
|
+
expect(
|
|
13
|
+
DayOfWeek.fromNumber(new Timestamp('2024-05-29').getDayOfWeek()),
|
|
14
|
+
).toEqual(DayOfWeek.WEDNESDAY);
|
|
15
|
+
expect(
|
|
16
|
+
DayOfWeek.fromNumber(new Timestamp('2024-05-30').getDayOfWeek()),
|
|
17
|
+
).toEqual(DayOfWeek.THURSDAY);
|
|
18
|
+
expect(
|
|
19
|
+
DayOfWeek.fromNumber(new Timestamp('2024-05-31').getDayOfWeek()),
|
|
20
|
+
).toEqual(DayOfWeek.FRIDAY);
|
|
21
|
+
expect(
|
|
22
|
+
DayOfWeek.fromNumber(new Timestamp('2024-06-01').getDayOfWeek()),
|
|
23
|
+
).toEqual(DayOfWeek.SATURDAY);
|
|
24
|
+
expect(
|
|
25
|
+
DayOfWeek.fromNumber(new Timestamp('2024-06-02').getDayOfWeek()),
|
|
26
|
+
).toEqual(DayOfWeek.SUNDAY);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should throw an error when an invalid number is provided', () => {
|
|
30
|
+
expect(() => {
|
|
31
|
+
DayOfWeek.fromNumber(23);
|
|
32
|
+
}).toThrow();
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
describe('toNumber', () => {
|
|
36
|
+
it('should return the correct number for a day', () => {
|
|
37
|
+
expect(DayOfWeek.MONDAY.toNumber()).toEqual(1);
|
|
38
|
+
expect(DayOfWeek.TUESDAY.toNumber()).toEqual(2);
|
|
39
|
+
expect(DayOfWeek.WEDNESDAY.toNumber()).toEqual(3);
|
|
40
|
+
expect(DayOfWeek.THURSDAY.toNumber()).toEqual(4);
|
|
41
|
+
expect(DayOfWeek.FRIDAY.toNumber()).toEqual(5);
|
|
42
|
+
expect(DayOfWeek.SATURDAY.toNumber()).toEqual(6);
|
|
43
|
+
expect(DayOfWeek.SUNDAY.toNumber()).toEqual(0);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
describe('fromTimestamp', () => {
|
|
47
|
+
it('should create the correct Day instance from a timestamp', () => {
|
|
48
|
+
expect(DayOfWeek.fromTimestamp(new Timestamp('2024-05-27'))).toEqual(
|
|
49
|
+
DayOfWeek.MONDAY,
|
|
50
|
+
);
|
|
51
|
+
expect(DayOfWeek.fromTimestamp(new Timestamp('2024-05-28'))).toEqual(
|
|
52
|
+
DayOfWeek.TUESDAY,
|
|
53
|
+
);
|
|
54
|
+
expect(DayOfWeek.fromTimestamp(new Timestamp('2024-05-29'))).toEqual(
|
|
55
|
+
DayOfWeek.WEDNESDAY,
|
|
56
|
+
);
|
|
57
|
+
expect(DayOfWeek.fromTimestamp(new Timestamp('2024-05-30'))).toEqual(
|
|
58
|
+
DayOfWeek.THURSDAY,
|
|
59
|
+
);
|
|
60
|
+
expect(DayOfWeek.fromTimestamp(new Timestamp('2024-05-31'))).toEqual(
|
|
61
|
+
DayOfWeek.FRIDAY,
|
|
62
|
+
);
|
|
63
|
+
expect(DayOfWeek.fromTimestamp(new Timestamp('2024-06-01'))).toEqual(
|
|
64
|
+
DayOfWeek.SATURDAY,
|
|
65
|
+
);
|
|
66
|
+
expect(DayOfWeek.fromTimestamp(new Timestamp('2024-06-02'))).toEqual(
|
|
67
|
+
DayOfWeek.SUNDAY,
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
});
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { Duration } from '../../../src';
|
|
2
|
+
|
|
3
|
+
describe('Duration', () => {
|
|
4
|
+
describe('fromDays', () => {
|
|
5
|
+
it('should create a Time instance from days', () => {
|
|
6
|
+
const days = 2;
|
|
7
|
+
const expectedMilliseconds = 2 * 24 * 60 * 60 * 1000;
|
|
8
|
+
const time = Duration.fromDays(days);
|
|
9
|
+
expect(time.valueOf()).toBe(expectedMilliseconds);
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
describe('fromHours', () => {
|
|
14
|
+
it('should create a Time instance from hours', () => {
|
|
15
|
+
const hours = 2;
|
|
16
|
+
const expectedMilliseconds = 2 * 60 * 60 * 1000;
|
|
17
|
+
const time = Duration.fromHours(hours);
|
|
18
|
+
expect(time.valueOf()).toBe(expectedMilliseconds);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('fromMinutes', () => {
|
|
23
|
+
it('should create a Time instance from minutes', () => {
|
|
24
|
+
const minutes = 30;
|
|
25
|
+
const expectedMilliseconds = 30 * 60 * 1000;
|
|
26
|
+
const time = Duration.fromMinutes(minutes);
|
|
27
|
+
expect(time.valueOf()).toBe(expectedMilliseconds);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('fromSeconds', () => {
|
|
32
|
+
it('should create a Time instance from seconds', () => {
|
|
33
|
+
const seconds = 45;
|
|
34
|
+
const expectedMilliseconds = 45 * 1000;
|
|
35
|
+
const time = Duration.fromSeconds(seconds);
|
|
36
|
+
expect(time.valueOf()).toBe(expectedMilliseconds);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe('fromMilliseconds', () => {
|
|
41
|
+
it('should create a Time instance from milliseconds', () => {
|
|
42
|
+
const milliseconds = 500;
|
|
43
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
44
|
+
expect(time.valueOf()).toBe(milliseconds);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('getTotalDays', () => {
|
|
49
|
+
it('should return the total days', () => {
|
|
50
|
+
const milliseconds = 2 * 24 * 60 * 60 * 1000;
|
|
51
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
52
|
+
const totalDays = time.getTotalDays();
|
|
53
|
+
expect(totalDays.valueOf()).toBe(2);
|
|
54
|
+
});
|
|
55
|
+
it('should keep the value after getting it', () => {
|
|
56
|
+
const milliseconds = 2 * 24 * 60 * 60 * 1000;
|
|
57
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
58
|
+
const firstTime = time.getTotalDays();
|
|
59
|
+
const secondTime = time.getTotalDays();
|
|
60
|
+
|
|
61
|
+
expect(firstTime.valueOf()).toBe(2);
|
|
62
|
+
expect(secondTime.valueOf()).toBe(2);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('getTotalHours', () => {
|
|
67
|
+
it('should return the total hours', () => {
|
|
68
|
+
const milliseconds = 2 * 60 * 60 * 1000;
|
|
69
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
70
|
+
const totalHours = time.getTotalHours();
|
|
71
|
+
expect(totalHours.valueOf()).toBe(2);
|
|
72
|
+
});
|
|
73
|
+
it('should keep the value after getting it', () => {
|
|
74
|
+
const milliseconds = 2 * 60 * 60 * 1000;
|
|
75
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
76
|
+
const firstTime = time.getTotalHours();
|
|
77
|
+
const secondTime = time.getTotalHours();
|
|
78
|
+
|
|
79
|
+
expect(firstTime.valueOf()).toBe(2);
|
|
80
|
+
expect(secondTime.valueOf()).toBe(2);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe('getTotalMinutes', () => {
|
|
85
|
+
it('should return the total minutes', () => {
|
|
86
|
+
const milliseconds = 3000 * 60 * 1000;
|
|
87
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
88
|
+
const totalMinutes = time.getTotalMinutes();
|
|
89
|
+
expect(totalMinutes.valueOf()).toBe(3000);
|
|
90
|
+
});
|
|
91
|
+
it('should keep the value after getting it', () => {
|
|
92
|
+
const milliseconds = 3000 * 60 * 1000;
|
|
93
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
94
|
+
const firstTime = time.getTotalMinutes();
|
|
95
|
+
const secondTime = time.getTotalMinutes();
|
|
96
|
+
|
|
97
|
+
expect(firstTime.valueOf()).toBe(3000);
|
|
98
|
+
expect(secondTime.valueOf()).toBe(3000);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe('getTotalSeconds', () => {
|
|
103
|
+
it('should return the total seconds', () => {
|
|
104
|
+
const milliseconds = 450 * 1000;
|
|
105
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
106
|
+
const totalSeconds = time.getTotalSeconds();
|
|
107
|
+
expect(totalSeconds.valueOf()).toBe(450);
|
|
108
|
+
});
|
|
109
|
+
it('should keep the value after getting it', () => {
|
|
110
|
+
const milliseconds = 450 * 1000;
|
|
111
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
112
|
+
const firstTime = time.getTotalSeconds();
|
|
113
|
+
const secondTime = time.getTotalSeconds();
|
|
114
|
+
|
|
115
|
+
expect(firstTime.valueOf()).toBe(450);
|
|
116
|
+
expect(secondTime.valueOf()).toBe(450);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe('getTotalMilliseconds', () => {
|
|
121
|
+
it('should return the total milliseconds', () => {
|
|
122
|
+
const milliseconds = 5000000;
|
|
123
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
124
|
+
const totalMilliseconds = time.getTotalMilliseconds();
|
|
125
|
+
expect(totalMilliseconds.valueOf()).toBe(milliseconds);
|
|
126
|
+
});
|
|
127
|
+
it('should return the total milliseconds', () => {
|
|
128
|
+
const milliseconds = 500;
|
|
129
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
130
|
+
const totalMilliseconds = time.getTotalMilliseconds();
|
|
131
|
+
expect(totalMilliseconds.valueOf()).toBe(milliseconds);
|
|
132
|
+
});
|
|
133
|
+
it('should keep the value after getting it', () => {
|
|
134
|
+
const milliseconds = 3000 * 60 * 1000;
|
|
135
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
136
|
+
const firstTime = time.getTotalMilliseconds();
|
|
137
|
+
const secondTime = time.getTotalMilliseconds();
|
|
138
|
+
|
|
139
|
+
expect(firstTime.valueOf()).toBe(milliseconds);
|
|
140
|
+
expect(secondTime.valueOf()).toBe(milliseconds);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
describe('getDays', () => {
|
|
145
|
+
it('should return the days', () => {
|
|
146
|
+
const milliseconds = 2 * 24 * 60 * 60 * 1000;
|
|
147
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
148
|
+
const days = time.getDays();
|
|
149
|
+
expect(days.valueOf()).toBe(2);
|
|
150
|
+
});
|
|
151
|
+
it('should return the days when there are hours and milliseconds', () => {
|
|
152
|
+
let milliseconds = 2 * 24 * 60 * 60 * 1000;
|
|
153
|
+
milliseconds += 2 * 60 * 60 * 1000;
|
|
154
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
155
|
+
const days = time.getDays();
|
|
156
|
+
expect(days.valueOf()).toBe(2);
|
|
157
|
+
});
|
|
158
|
+
it('should return the days', () => {
|
|
159
|
+
const milliseconds = 976940000;
|
|
160
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
161
|
+
const days = time.getDays();
|
|
162
|
+
expect(days.valueOf()).toBe(11);
|
|
163
|
+
});
|
|
164
|
+
it('should keep the value after getting it', () => {
|
|
165
|
+
const milliseconds = 976940000;
|
|
166
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
167
|
+
const firstTime = time.getDays();
|
|
168
|
+
const secondTime = time.getDays();
|
|
169
|
+
|
|
170
|
+
expect(firstTime.valueOf()).toBe(11);
|
|
171
|
+
expect(secondTime.valueOf()).toBe(11);
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
describe('getHours', () => {
|
|
176
|
+
it('should return the hours', () => {
|
|
177
|
+
const milliseconds = 2 * 60 * 60 * 1000;
|
|
178
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
179
|
+
const hours = time.getHours();
|
|
180
|
+
expect(hours.valueOf()).toBe(2);
|
|
181
|
+
});
|
|
182
|
+
it('should return the hours when there are minutes and milliseconds', () => {
|
|
183
|
+
let milliseconds = 2 * 60 * 60 * 1000;
|
|
184
|
+
milliseconds += 30 * 60 * 1000;
|
|
185
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
186
|
+
const hours = time.getHours();
|
|
187
|
+
expect(hours.valueOf()).toBe(2);
|
|
188
|
+
});
|
|
189
|
+
it('should return the hours', () => {
|
|
190
|
+
const milliseconds = 35940000;
|
|
191
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
192
|
+
const minutes = time.getHours();
|
|
193
|
+
expect(minutes.valueOf()).toBe(9);
|
|
194
|
+
});
|
|
195
|
+
it('should keep the value after getting it', () => {
|
|
196
|
+
const milliseconds = 35940000;
|
|
197
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
198
|
+
const firstTime = time.getHours();
|
|
199
|
+
const secondTime = time.getHours();
|
|
200
|
+
|
|
201
|
+
expect(firstTime.valueOf()).toBe(9);
|
|
202
|
+
expect(secondTime.valueOf()).toBe(9);
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
describe('getMinutes', () => {
|
|
207
|
+
it('should return the minutes', () => {
|
|
208
|
+
const milliseconds = 30 * 60 * 1000;
|
|
209
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
210
|
+
const minutes = time.getMinutes();
|
|
211
|
+
expect(minutes.valueOf()).toBe(30);
|
|
212
|
+
});
|
|
213
|
+
it('should return the minutes', () => {
|
|
214
|
+
const milliseconds = 35940000;
|
|
215
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
216
|
+
const minutes = time.getMinutes();
|
|
217
|
+
expect(minutes.valueOf()).toBe(59);
|
|
218
|
+
});
|
|
219
|
+
it('should keep the value after getting it', () => {
|
|
220
|
+
const milliseconds = 35940000;
|
|
221
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
222
|
+
const firstTime = time.getMinutes();
|
|
223
|
+
const secondTime = time.getMinutes();
|
|
224
|
+
|
|
225
|
+
expect(firstTime.valueOf()).toBe(59);
|
|
226
|
+
expect(secondTime.valueOf()).toBe(59);
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
describe('getSeconds', () => {
|
|
231
|
+
it('should return the seconds', () => {
|
|
232
|
+
const milliseconds = 45 * 1000;
|
|
233
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
234
|
+
const seconds = time.getSeconds();
|
|
235
|
+
expect(seconds.valueOf()).toBe(45);
|
|
236
|
+
});
|
|
237
|
+
it('should return the seconds', () => {
|
|
238
|
+
const milliseconds = 70 * 1000;
|
|
239
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
240
|
+
const minutes = time.getSeconds();
|
|
241
|
+
expect(minutes.valueOf()).toBe(10);
|
|
242
|
+
});
|
|
243
|
+
it('should keep the value after getting it', () => {
|
|
244
|
+
const milliseconds = 45 * 1000;
|
|
245
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
246
|
+
const firstTime = time.getSeconds();
|
|
247
|
+
const secondTime = time.getSeconds();
|
|
248
|
+
|
|
249
|
+
expect(firstTime.valueOf()).toBe(45);
|
|
250
|
+
expect(secondTime.valueOf()).toBe(45);
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
describe('getMilliseconds', () => {
|
|
255
|
+
it('should return the milliseconds', () => {
|
|
256
|
+
const milliseconds = 1500;
|
|
257
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
258
|
+
expect(time.getMilliseconds().valueOf()).toBe(500);
|
|
259
|
+
});
|
|
260
|
+
it('should keep the value after getting it', () => {
|
|
261
|
+
const milliseconds = 1500;
|
|
262
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
263
|
+
const firstTime = time.getMilliseconds();
|
|
264
|
+
const secondTime = time.getMilliseconds();
|
|
265
|
+
|
|
266
|
+
expect(firstTime.valueOf()).toBe(500);
|
|
267
|
+
expect(secondTime.valueOf()).toBe(500);
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
describe('valueOf', () => {
|
|
272
|
+
it('should return the value in milliseconds', () => {
|
|
273
|
+
const milliseconds = 500;
|
|
274
|
+
const time = Duration.fromMilliseconds(milliseconds);
|
|
275
|
+
expect(time.valueOf()).toBe(milliseconds);
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
});
|