@eliasrrosa/tutorhub-public-assets 0.0.10 → 0.0.11
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/dist/domain/entities/ISO8601DateTime.js +1 -1
- package/dist/domain/entities/ISO8601Time.d.ts +20 -9
- package/dist/domain/entities/ISO8601Time.js +62 -25
- package/dist/domain/entities/ISO8601Time.test.js +105 -14
- package/dist/domain/entities/ISO8601TimeValidator.d.ts +7 -0
- package/dist/domain/entities/ISO8601TimeValidator.js +9 -3
- package/dist/domain/entities/ISO8601Timezone.d.ts +12 -0
- package/dist/domain/entities/ISO8601Timezone.js +13 -0
- package/dist/domain/entities/ISO8601Timezone.test.d.ts +1 -0
- package/dist/domain/entities/ISO8601Timezone.test.js +11 -0
- package/dist/domain/entities/ISO8601TimezoneConverter.d.ts +19 -0
- package/dist/domain/entities/ISO8601TimezoneConverter.js +28 -0
- package/dist/domain/entities/ISO8601TimezoneConverter.test.d.ts +1 -0
- package/dist/domain/entities/ISO8601TimezoneConverter.test.js +50 -0
- package/dist/domain/entities/ISO8601TimezoneParser.d.ts +17 -0
- package/dist/domain/entities/ISO8601TimezoneParser.js +47 -0
- package/dist/domain/entities/ISO8601TimezoneParser.test.d.ts +1 -0
- package/dist/domain/entities/ISO8601TimezoneParser.test.js +87 -0
- package/dist/domain/entities/ISO8601TimezoneSelector.d.ts +32 -0
- package/dist/domain/entities/ISO8601TimezoneSelector.js +61 -0
- package/dist/domain/entities/ISO8601TimezoneSelector.test.d.ts +1 -0
- package/dist/domain/entities/ISO8601TimezoneSelector.test.js +151 -0
- package/dist/domain/entities/ISO8601TimezoneValidator.d.ts +22 -0
- package/dist/domain/entities/ISO8601TimezoneValidator.js +37 -0
- package/dist/domain/entities/ISO8601TimezoneValidator.test.d.ts +1 -0
- package/dist/domain/entities/ISO8601TimezoneValidator.test.js +65 -0
- package/dist/domain/entities/Time.d.ts +26 -6
- package/dist/index.cjs +239 -29
- package/package.json +1 -1
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const ISO8601TimezoneParser_1 = require("./ISO8601TimezoneParser");
|
|
4
|
+
describe("ISO8601TimezoneParser", () => {
|
|
5
|
+
describe("ISO8601TimezoneParser constructor", () => {
|
|
6
|
+
it("Should be instantiable without arguments", () => {
|
|
7
|
+
const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
|
|
8
|
+
expect(parser).toBeDefined();
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
describe("ISO8601TimezoneParser.toString()", () => {
|
|
12
|
+
it("Should return Z for UTC.", () => {
|
|
13
|
+
const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
|
|
14
|
+
expect(parser.toString(0)).toEqual("Z");
|
|
15
|
+
});
|
|
16
|
+
it("Should return Z for invalid timezone.", () => {
|
|
17
|
+
const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
|
|
18
|
+
expect(parser.toString(1000)).toEqual("Z");
|
|
19
|
+
});
|
|
20
|
+
it("Should return +03:00 for 180.", () => {
|
|
21
|
+
const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
|
|
22
|
+
expect(parser.toString(180)).toEqual("+03:00");
|
|
23
|
+
});
|
|
24
|
+
it("Should return -03:00 for -180.", () => {
|
|
25
|
+
const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
|
|
26
|
+
expect(parser.toString(-180)).toEqual("-03:00");
|
|
27
|
+
});
|
|
28
|
+
it("Should return -02:00 for -120.", () => {
|
|
29
|
+
const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
|
|
30
|
+
expect(parser.toString(-120)).toEqual("-02:00");
|
|
31
|
+
});
|
|
32
|
+
it("Should return -02:30 for -150.", () => {
|
|
33
|
+
const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
|
|
34
|
+
expect(parser.toString(-150)).toEqual("-02:30");
|
|
35
|
+
});
|
|
36
|
+
it("Should return +02:30 for 150.", () => {
|
|
37
|
+
const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
|
|
38
|
+
expect(parser.toString(150)).toEqual("+02:30");
|
|
39
|
+
});
|
|
40
|
+
it("Should return +04:00 for 240.", () => {
|
|
41
|
+
const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
|
|
42
|
+
expect(parser.toString(240)).toEqual("+04:00");
|
|
43
|
+
});
|
|
44
|
+
it("Should return +07:00 for 420.", () => {
|
|
45
|
+
const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
|
|
46
|
+
expect(parser.toString(420)).toEqual("+07:00");
|
|
47
|
+
});
|
|
48
|
+
it("Should return -07:00 for -420.", () => {
|
|
49
|
+
const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
|
|
50
|
+
expect(parser.toString(-420)).toEqual("-07:00");
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
describe("ISO8601TimezoneParser.toMinutes()", () => {
|
|
54
|
+
it("Should return 0 given Z", () => {
|
|
55
|
+
const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
|
|
56
|
+
expect(parser.toMinutes("Z")).toEqual(0);
|
|
57
|
+
});
|
|
58
|
+
it("Should return 0 given Z plus milliseconds", () => {
|
|
59
|
+
const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
|
|
60
|
+
expect(parser.toMinutes(".192Z")).toEqual(0);
|
|
61
|
+
});
|
|
62
|
+
it("Should return 60 given +01:00", () => {
|
|
63
|
+
const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
|
|
64
|
+
expect(parser.toMinutes("+01:00")).toEqual(60);
|
|
65
|
+
});
|
|
66
|
+
it("Should return 90 given +01:30", () => {
|
|
67
|
+
const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
|
|
68
|
+
expect(parser.toMinutes("+01:30")).toEqual(90);
|
|
69
|
+
});
|
|
70
|
+
it("Should return 130 given +02:10", () => {
|
|
71
|
+
const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
|
|
72
|
+
expect(parser.toMinutes("+02:10")).toEqual(130);
|
|
73
|
+
});
|
|
74
|
+
it("Should return -60 given -01:00", () => {
|
|
75
|
+
const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
|
|
76
|
+
expect(parser.toMinutes("-01:00")).toEqual(-60);
|
|
77
|
+
});
|
|
78
|
+
it("Should return 90 given +01:30", () => {
|
|
79
|
+
const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
|
|
80
|
+
expect(parser.toMinutes("+01:30")).toEqual(90);
|
|
81
|
+
});
|
|
82
|
+
it("Should return 130 given +02:10", () => {
|
|
83
|
+
const parser = new ISO8601TimezoneParser_1.ISO8601TimezoneParser();
|
|
84
|
+
expect(parser.toMinutes("+02:10")).toEqual(130);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export interface IISO8601TimezoneSelector {
|
|
2
|
+
/**
|
|
3
|
+
* Accepts any string with a timezone on it,
|
|
4
|
+
* eg:. `Z, .sssZ, (+|-)HH:MM`, or any valid timezone number
|
|
5
|
+
* in minutes (-840 up to 720) and returns only the timezone string.
|
|
6
|
+
* If the timezone is not valid, returns Z.
|
|
7
|
+
*/
|
|
8
|
+
getTimezoneString: (timezone: string) => string;
|
|
9
|
+
/**
|
|
10
|
+
* Returns a string representing the difference of hours of the timezone
|
|
11
|
+
* from UTC, rounded down. If timezone is UTC or invalid, returns undefined.
|
|
12
|
+
*/
|
|
13
|
+
getHoursString: (timezone: string) => string | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Returns a string representing the difference of minutes of the timezone
|
|
16
|
+
* from UTC minus the hours. Eg:. `+02:30` returns `"30"`. If timezone is UTC
|
|
17
|
+
* or invalid, returns undefined.
|
|
18
|
+
*/
|
|
19
|
+
getMinutesString: (timezone: string) => string | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Returns "+" for positive timezones likes +03:00, "-" for negative timezones
|
|
22
|
+
* like -02:00, or undefined if timezone is UTC or invalid.
|
|
23
|
+
*/
|
|
24
|
+
getTimezoneSign: (timezone: string) => string | undefined;
|
|
25
|
+
}
|
|
26
|
+
export declare class ISO8601TimezoneSelector implements IISO8601TimezoneSelector {
|
|
27
|
+
private timezoneValidator;
|
|
28
|
+
getTimezoneString: (timezone: string) => string;
|
|
29
|
+
getHoursString: (timezone: string) => string | undefined;
|
|
30
|
+
getMinutesString: (timezone: string) => string | undefined;
|
|
31
|
+
getTimezoneSign: (timezone: string) => string | undefined;
|
|
32
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ISO8601TimezoneSelector = void 0;
|
|
4
|
+
const ISO8601TimezoneValidator_1 = require("./ISO8601TimezoneValidator");
|
|
5
|
+
class ISO8601TimezoneSelector {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.timezoneValidator = new ISO8601TimezoneValidator_1.ISO8601TimezoneValidator();
|
|
8
|
+
this.getTimezoneString = (timezone) => {
|
|
9
|
+
const timezoneFormatIsValid = this.timezoneValidator.formatIsValid(timezone);
|
|
10
|
+
if (!timezoneFormatIsValid) {
|
|
11
|
+
return "Z";
|
|
12
|
+
}
|
|
13
|
+
if (typeof timezone == "string" &&
|
|
14
|
+
(this.timezoneValidator.timezoneIsZ(timezone) ||
|
|
15
|
+
this.timezoneValidator.timezoneIsZandMilliseconds(timezone))) {
|
|
16
|
+
return "Z";
|
|
17
|
+
}
|
|
18
|
+
if (typeof timezone == "string" &&
|
|
19
|
+
this.timezoneValidator.timezoneIsNumbered(timezone)) {
|
|
20
|
+
const match = timezone.match(this.timezoneValidator.numberedTimezonePattern);
|
|
21
|
+
if (!match)
|
|
22
|
+
return "Z";
|
|
23
|
+
return match[0];
|
|
24
|
+
}
|
|
25
|
+
return "Z";
|
|
26
|
+
};
|
|
27
|
+
this.getHoursString = (timezone) => {
|
|
28
|
+
var _a;
|
|
29
|
+
if (this.timezoneValidator.timezoneIsNumbered(timezone)) {
|
|
30
|
+
const matches = timezone.match(this.timezoneValidator.numberedTimezonePatternString);
|
|
31
|
+
if (matches == null || matches.length < 1)
|
|
32
|
+
return undefined;
|
|
33
|
+
return (_a = matches[0].split(":")) === null || _a === void 0 ? void 0 : _a[0].split("").slice(1).join("");
|
|
34
|
+
}
|
|
35
|
+
return undefined;
|
|
36
|
+
};
|
|
37
|
+
this.getMinutesString = (timezone) => {
|
|
38
|
+
var _a;
|
|
39
|
+
if (this.timezoneValidator.timezoneIsNumbered(timezone)) {
|
|
40
|
+
const matches = timezone.match(this.timezoneValidator.numberedTimezonePatternString);
|
|
41
|
+
if (matches == null || matches.length < 1)
|
|
42
|
+
return undefined;
|
|
43
|
+
return (_a = matches[0].split(":")) === null || _a === void 0 ? void 0 : _a[1];
|
|
44
|
+
}
|
|
45
|
+
return undefined;
|
|
46
|
+
};
|
|
47
|
+
this.getTimezoneSign = (timezone) => {
|
|
48
|
+
if (this.timezoneValidator.timezoneIsNumbered(timezone)) {
|
|
49
|
+
const matches = timezone.match(this.timezoneValidator.numberedTimezonePatternString);
|
|
50
|
+
if (matches == null || matches.length < 1)
|
|
51
|
+
return undefined;
|
|
52
|
+
if (matches[0].includes("+"))
|
|
53
|
+
return "+";
|
|
54
|
+
if (matches[0].includes("-"))
|
|
55
|
+
return "-";
|
|
56
|
+
}
|
|
57
|
+
return undefined;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.ISO8601TimezoneSelector = ISO8601TimezoneSelector;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const ISO8601TimezoneSelector_1 = require("./ISO8601TimezoneSelector");
|
|
4
|
+
describe("ISO8601TimezoneSelector", () => {
|
|
5
|
+
describe("ISO8601TimezoneSelector constructor", () => {
|
|
6
|
+
it("Should be instantiable without arguments.", () => {
|
|
7
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
8
|
+
expect(selector).toBeDefined();
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
describe("ISO8601TimezoneSelector.getTimezoneString()", () => {
|
|
12
|
+
it("Should return 'Z' for no timezone.", () => {
|
|
13
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
14
|
+
expect(selector.getTimezoneString("")).toEqual("Z");
|
|
15
|
+
});
|
|
16
|
+
it("Should return 'Z' for UTC timezone without milliseconds.", () => {
|
|
17
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
18
|
+
expect(selector.getTimezoneString("12:00:00Z")).toEqual("Z");
|
|
19
|
+
});
|
|
20
|
+
it("Should return 'Z' for UTC timezone with milliseconds.", () => {
|
|
21
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
22
|
+
expect(selector.getTimezoneString("12:00:00.000Z")).toEqual("Z");
|
|
23
|
+
});
|
|
24
|
+
it("Should return timezone given timezone presence in argument", () => {
|
|
25
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
26
|
+
expect(selector.getTimezoneString("12:00:00+03:00")).toEqual("+03:00");
|
|
27
|
+
});
|
|
28
|
+
it("Should return negative timezone given timezone presence in argument", () => {
|
|
29
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
30
|
+
expect(selector.getTimezoneString("12:00:00-03:00")).toEqual("-03:00");
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
describe("ISO8601TimezoneSelector.getMinutesString()", () => {
|
|
34
|
+
it("Should return undefined for no timezone.", () => {
|
|
35
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
36
|
+
expect(selector.getMinutesString("")).toEqual(undefined);
|
|
37
|
+
});
|
|
38
|
+
it("Should return undefined for no UTC timezone.", () => {
|
|
39
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
40
|
+
expect(selector.getMinutesString("Z")).toEqual(undefined);
|
|
41
|
+
});
|
|
42
|
+
it("Should return undefined for no UTC timezone plus hours.", () => {
|
|
43
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
44
|
+
expect(selector.getMinutesString("12:00:00Z")).toEqual(undefined);
|
|
45
|
+
});
|
|
46
|
+
it("Should return undefined for no UTC plus milliseconds timezone.", () => {
|
|
47
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
48
|
+
expect(selector.getMinutesString(".123Z")).toEqual(undefined);
|
|
49
|
+
});
|
|
50
|
+
it("Should return undefined for no UTC plus milliseconds timezone plus hours.", () => {
|
|
51
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
52
|
+
expect(selector.getMinutesString("12:00:00.123Z")).toEqual(undefined);
|
|
53
|
+
});
|
|
54
|
+
it("Should return undefined for no UTC plus milliseconds timezone plus hours.", () => {
|
|
55
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
56
|
+
expect(selector.getMinutesString("12:00:00.123Z")).toEqual(undefined);
|
|
57
|
+
});
|
|
58
|
+
it("Should return 00 given +01:00 timezone.", () => {
|
|
59
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
60
|
+
expect(selector.getMinutesString("+01:00")).toEqual("00");
|
|
61
|
+
});
|
|
62
|
+
it("Should return 00 given -01:00 timezone.", () => {
|
|
63
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
64
|
+
expect(selector.getMinutesString("-01:00")).toEqual("00");
|
|
65
|
+
});
|
|
66
|
+
it("Should return 30 given +01:30 timezone.", () => {
|
|
67
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
68
|
+
expect(selector.getMinutesString("+01:30")).toEqual("30");
|
|
69
|
+
});
|
|
70
|
+
it("Should return 30 given -01:30 timezone.", () => {
|
|
71
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
72
|
+
expect(selector.getMinutesString("-01:30")).toEqual("30");
|
|
73
|
+
});
|
|
74
|
+
it("Should return 00 given +03:00 timezone.", () => {
|
|
75
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
76
|
+
expect(selector.getMinutesString("+03:00")).toEqual("00");
|
|
77
|
+
});
|
|
78
|
+
it("Should return 00 given timezone with time.", () => {
|
|
79
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
80
|
+
expect(selector.getMinutesString("12:00:00+03:00")).toEqual("00");
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
describe("ISO8601TimezoneSelector.getHoursString()", () => {
|
|
84
|
+
it("Should return undefined for no timezone.", () => {
|
|
85
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
86
|
+
expect(selector.getHoursString("")).toEqual(undefined);
|
|
87
|
+
});
|
|
88
|
+
it("Should return undefined for no UTC timezone.", () => {
|
|
89
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
90
|
+
expect(selector.getHoursString("Z")).toEqual(undefined);
|
|
91
|
+
});
|
|
92
|
+
it("Should return undefined for no UTC timezone plus hours.", () => {
|
|
93
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
94
|
+
expect(selector.getHoursString("12:00:00Z")).toEqual(undefined);
|
|
95
|
+
});
|
|
96
|
+
it("Should return undefined for no UTC plus milliseconds timezone.", () => {
|
|
97
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
98
|
+
expect(selector.getHoursString(".123Z")).toEqual(undefined);
|
|
99
|
+
});
|
|
100
|
+
it("Should return undefined for no UTC plus milliseconds timezone plus hours.", () => {
|
|
101
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
102
|
+
expect(selector.getHoursString("12:00:00.123Z")).toEqual(undefined);
|
|
103
|
+
});
|
|
104
|
+
it("Should return undefined for no UTC plus milliseconds timezone plus hours.", () => {
|
|
105
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
106
|
+
expect(selector.getHoursString("12:00:00.123Z")).toEqual(undefined);
|
|
107
|
+
});
|
|
108
|
+
it("Should return 01 given +01:00 timezone.", () => {
|
|
109
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
110
|
+
expect(selector.getHoursString("+01:00")).toEqual("01");
|
|
111
|
+
});
|
|
112
|
+
it("Should return 01 given -01:00 timezone.", () => {
|
|
113
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
114
|
+
expect(selector.getHoursString("-01:00")).toEqual("01");
|
|
115
|
+
});
|
|
116
|
+
it("Should return 01 given +01:30 timezone.", () => {
|
|
117
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
118
|
+
expect(selector.getHoursString("+01:30")).toEqual("01");
|
|
119
|
+
});
|
|
120
|
+
it("Should return 01 given -01:30 timezone.", () => {
|
|
121
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
122
|
+
expect(selector.getHoursString("-01:30")).toEqual("01");
|
|
123
|
+
});
|
|
124
|
+
it("Should return 03 given +03:00 timezone.", () => {
|
|
125
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
126
|
+
expect(selector.getHoursString("+03:00")).toEqual("03");
|
|
127
|
+
});
|
|
128
|
+
it("Should return 03 given timezone with time.", () => {
|
|
129
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
130
|
+
expect(selector.getHoursString("12:00:00+03:00")).toEqual("03");
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
describe("ISO8601TimezoneSelector.getTimezoneSign()", () => {
|
|
134
|
+
it("Should return + for positive timezones", () => {
|
|
135
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
136
|
+
expect(selector.getTimezoneSign("12:00:00+03:00")).toEqual("+");
|
|
137
|
+
});
|
|
138
|
+
it("Should return - for negative timezones", () => {
|
|
139
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
140
|
+
expect(selector.getTimezoneSign("12:00:00-03:00")).toEqual("-");
|
|
141
|
+
});
|
|
142
|
+
it("Should return undefined for UTC timezones", () => {
|
|
143
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
144
|
+
expect(selector.getTimezoneSign("12:00:00Z")).toEqual(undefined);
|
|
145
|
+
});
|
|
146
|
+
it("Should return undefined for invalid timezones", () => {
|
|
147
|
+
const selector = new ISO8601TimezoneSelector_1.ISO8601TimezoneSelector();
|
|
148
|
+
expect(selector.getTimezoneSign("asdasdasd")).toEqual(undefined);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface IISO8601TimezoneValidator {
|
|
2
|
+
/**Returns `true` if timezone is a string and contains a (+|-)HH:MM format,
|
|
3
|
+
* with a range of +12:00 and -14:00, or a number between -840 (-14:00) and 720 (+12:00).
|
|
4
|
+
* Otherwise returns `false` */
|
|
5
|
+
formatIsValid: (timezone: number | string) => boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare class ISO8601TimezoneValidator implements IISO8601TimezoneValidator {
|
|
8
|
+
private upTo59;
|
|
9
|
+
private upTo11;
|
|
10
|
+
private upTo13;
|
|
11
|
+
zTimezonePatternString: string;
|
|
12
|
+
zTimezonePattern: RegExp;
|
|
13
|
+
zAndMillisecondsPatternString: string;
|
|
14
|
+
zAndMillisecondsPattern: RegExp;
|
|
15
|
+
numberedTimezonePatternString: string;
|
|
16
|
+
numberedTimezonePattern: RegExp;
|
|
17
|
+
formatIsValid: (timezone: number | string) => boolean;
|
|
18
|
+
timezoneIsNumbered(timezone: string): boolean;
|
|
19
|
+
timezoneIsZ(timezone: string): boolean;
|
|
20
|
+
timezoneIsZandMilliseconds(timezone: string): boolean;
|
|
21
|
+
timezoneNumberIsValid(timezone: number): boolean;
|
|
22
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ISO8601TimezoneValidator = void 0;
|
|
4
|
+
class ISO8601TimezoneValidator {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.upTo59 = "[0-5][0-9]";
|
|
7
|
+
this.upTo11 = "(0[0-9]|1[0-1])";
|
|
8
|
+
this.upTo13 = "(0[0-9]|1[0-4])";
|
|
9
|
+
this.zTimezonePatternString = "Z";
|
|
10
|
+
this.zTimezonePattern = new RegExp(`${this.zTimezonePatternString}$`);
|
|
11
|
+
this.zAndMillisecondsPatternString = `.[0-9]{3}Z`;
|
|
12
|
+
this.zAndMillisecondsPattern = new RegExp(`${this.zAndMillisecondsPatternString}$`);
|
|
13
|
+
this.numberedTimezonePatternString = `(\\+(${this.upTo11}:${this.upTo59}|12:00)|\\-(${this.upTo13}:${this.upTo59}|14:00))`;
|
|
14
|
+
this.numberedTimezonePattern = new RegExp(`${this.numberedTimezonePatternString}$`);
|
|
15
|
+
this.formatIsValid = (timezone) => {
|
|
16
|
+
if (typeof timezone == "number") {
|
|
17
|
+
return this.timezoneNumberIsValid(timezone);
|
|
18
|
+
}
|
|
19
|
+
return (this.timezoneIsNumbered(timezone) ||
|
|
20
|
+
this.timezoneIsZ(timezone) ||
|
|
21
|
+
this.timezoneIsZandMilliseconds(timezone));
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
timezoneIsNumbered(timezone) {
|
|
25
|
+
return this.numberedTimezonePattern.test(timezone);
|
|
26
|
+
}
|
|
27
|
+
timezoneIsZ(timezone) {
|
|
28
|
+
return this.zTimezonePattern.test(timezone);
|
|
29
|
+
}
|
|
30
|
+
timezoneIsZandMilliseconds(timezone) {
|
|
31
|
+
return this.zAndMillisecondsPattern.test(timezone);
|
|
32
|
+
}
|
|
33
|
+
timezoneNumberIsValid(timezone) {
|
|
34
|
+
return timezone >= -840 && timezone <= 720;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.ISO8601TimezoneValidator = ISO8601TimezoneValidator;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const ISO8601TimezoneValidator_1 = require("./ISO8601TimezoneValidator");
|
|
4
|
+
describe("ISO8601TimezoneValidator", () => {
|
|
5
|
+
describe("ISO8601TimezoneValidator constructor", () => {
|
|
6
|
+
it("Should be instantiable with no arguments.", () => {
|
|
7
|
+
const validator = new ISO8601TimezoneValidator_1.ISO8601TimezoneValidator();
|
|
8
|
+
expect(validator).toBeDefined();
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
describe("ISO8601TimezoneValidator.formatIsValid()", () => {
|
|
12
|
+
it("Should return true for time string with numbered timezone", () => {
|
|
13
|
+
const validator = new ISO8601TimezoneValidator_1.ISO8601TimezoneValidator();
|
|
14
|
+
expect(validator.formatIsValid("12:00:00-03:00")).toBe(true);
|
|
15
|
+
});
|
|
16
|
+
it("Should return true for timezone string with numbered timezone", () => {
|
|
17
|
+
const validator = new ISO8601TimezoneValidator_1.ISO8601TimezoneValidator();
|
|
18
|
+
expect(validator.formatIsValid("-03:00")).toBe(true);
|
|
19
|
+
});
|
|
20
|
+
it("Should return true for timestring string with Z", () => {
|
|
21
|
+
const validator = new ISO8601TimezoneValidator_1.ISO8601TimezoneValidator();
|
|
22
|
+
expect(validator.formatIsValid("12:00:00Z")).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
it("Should return true for timezone string with Z", () => {
|
|
25
|
+
const validator = new ISO8601TimezoneValidator_1.ISO8601TimezoneValidator();
|
|
26
|
+
expect(validator.formatIsValid("Z")).toBe(true);
|
|
27
|
+
});
|
|
28
|
+
it("Should return true for timezone string with Z and miliseconds", () => {
|
|
29
|
+
const validator = new ISO8601TimezoneValidator_1.ISO8601TimezoneValidator();
|
|
30
|
+
expect(validator.formatIsValid(".999Z")).toBe(true);
|
|
31
|
+
});
|
|
32
|
+
it("Should return true for time string with Z and miliseconds", () => {
|
|
33
|
+
const validator = new ISO8601TimezoneValidator_1.ISO8601TimezoneValidator();
|
|
34
|
+
expect(validator.formatIsValid("12:00:00.999Z")).toBe(true);
|
|
35
|
+
});
|
|
36
|
+
it("Should return false for numbered timezones greater than +12:00", () => {
|
|
37
|
+
const validator = new ISO8601TimezoneValidator_1.ISO8601TimezoneValidator();
|
|
38
|
+
expect(validator.formatIsValid("12:00:00+12:01")).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
it("Should return false for numbered timezones smaller than -14:00", () => {
|
|
41
|
+
const validator = new ISO8601TimezoneValidator_1.ISO8601TimezoneValidator();
|
|
42
|
+
expect(validator.formatIsValid("12:00:00+14:01")).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
it("Should return false for no timezone", () => {
|
|
45
|
+
const validator = new ISO8601TimezoneValidator_1.ISO8601TimezoneValidator();
|
|
46
|
+
expect(validator.formatIsValid("12:00:00")).toBe(false);
|
|
47
|
+
});
|
|
48
|
+
it("Should return false for numbers below -840", () => {
|
|
49
|
+
const validator = new ISO8601TimezoneValidator_1.ISO8601TimezoneValidator();
|
|
50
|
+
expect(validator.formatIsValid(-841)).toBe(false);
|
|
51
|
+
});
|
|
52
|
+
it("Should return false for numbers above 720", () => {
|
|
53
|
+
const validator = new ISO8601TimezoneValidator_1.ISO8601TimezoneValidator();
|
|
54
|
+
expect(validator.formatIsValid(721)).toBe(false);
|
|
55
|
+
});
|
|
56
|
+
it("Should return true for numbers between -840 and 720", () => {
|
|
57
|
+
const validator = new ISO8601TimezoneValidator_1.ISO8601TimezoneValidator();
|
|
58
|
+
expect(validator.formatIsValid(720)).toBe(true);
|
|
59
|
+
expect(validator.formatIsValid(-840)).toBe(true);
|
|
60
|
+
expect(validator.formatIsValid(0)).toBe(true);
|
|
61
|
+
expect(validator.formatIsValid(200)).toBe(true);
|
|
62
|
+
expect(validator.formatIsValid(180)).toBe(true);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -1,9 +1,29 @@
|
|
|
1
|
+
import { IISO8601Timezone } from "./ISO8601Timezone";
|
|
1
2
|
import { ITimeValidator } from "./TimeValidator";
|
|
2
3
|
export interface ITime {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
originalTimeString: string;
|
|
5
|
+
timeValidator: ITimeValidator;
|
|
6
|
+
/**
|
|
7
|
+
* returns time string in chosen format. Default is `HH:MM`.
|
|
8
|
+
*
|
|
9
|
+
*`convertToTimezone` accepts a timezone string like `(+|-)HH:MM` or `Z` or
|
|
10
|
+
* a number representing the difference of the timezone from UTC in minutes (eg:. +03:00 is 180, -02:00 is -120).
|
|
11
|
+
* If present, this function will return a time string converted to this timezone, based on the original
|
|
12
|
+
* timezone present on the time string passed to the constructor.
|
|
13
|
+
*/
|
|
14
|
+
getTimeString: (opts?: {
|
|
15
|
+
format?: "HH:MM" | "HH:MM:SS" | "HH:MM:SS(+|-)HH:MM";
|
|
16
|
+
convertToTimezone?: string | number;
|
|
17
|
+
}) => string;
|
|
18
|
+
getHoursString: () => string;
|
|
19
|
+
getMinutesString: () => string;
|
|
20
|
+
getSecondsString: () => string;
|
|
21
|
+
getNowTimeString: () => string;
|
|
22
|
+
getTimezone: () => IISO8601Timezone;
|
|
23
|
+
addHours: (hours: number) => ITime;
|
|
24
|
+
addMinutes: (minutes: number) => ITime;
|
|
25
|
+
/**Adds a timezone without converting the time, following ISO8601 format, like `Z` for UTC,
|
|
26
|
+
* and `(+|-)HH:MM` for others. */
|
|
27
|
+
addTimezone: (timezone: string | number) => ITime;
|
|
28
|
+
getTimezoneString: () => string;
|
|
9
29
|
}
|