@yangsaiyam/helper 1.5.0 → 1.5.2
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/package.json +4 -1
- package/src/roles/index.test.ts +23 -1
- package/src/roles/index.ts +17 -1
- package/src/time/index.test.ts +37 -3
- package/src/time/index.ts +33 -20
package/package.json
CHANGED
package/src/roles/index.test.ts
CHANGED
|
@@ -46,14 +46,36 @@ assert.equal(
|
|
|
46
46
|
permissionLabelKey(Permission.CONTACT_FORM_DELETE),
|
|
47
47
|
"permissions.contactFormDelete",
|
|
48
48
|
);
|
|
49
|
+
assert.equal(
|
|
50
|
+
permissionLabelKey(Permission.OTP_ATTEMPTS_READ),
|
|
51
|
+
"permissions.otpAttemptsRead",
|
|
52
|
+
);
|
|
53
|
+
assert.equal(
|
|
54
|
+
permissionLabelKey(Permission.TWO_FACTOR_AUTH_WRITE),
|
|
55
|
+
"permissions.twoFactorAuthWrite",
|
|
56
|
+
);
|
|
57
|
+
assert.equal(permissionLabelKey(Permission.VIEWS_READ), "permissions.viewsRead");
|
|
49
58
|
|
|
50
59
|
assert.ok(ROLE_PERMISSIONS["super-admin"].includes(Permission.USERS_DELETE));
|
|
60
|
+
assert.ok(ROLE_PERMISSIONS["super-admin"].includes(Permission.OTP_ATTEMPTS_READ));
|
|
61
|
+
assert.ok(
|
|
62
|
+
ROLE_PERMISSIONS["super-admin"].includes(Permission.TWO_FACTOR_AUTH_WRITE),
|
|
63
|
+
);
|
|
64
|
+
assert.ok(ROLE_PERMISSIONS["super-admin"].includes(Permission.VIEWS_READ));
|
|
51
65
|
assert.ok(ROLE_PERMISSIONS.admin.includes(Permission.CONTACT_FORM_DELETE));
|
|
66
|
+
assert.ok(ROLE_PERMISSIONS.admin.includes(Permission.VIEWS_READ));
|
|
67
|
+
assert.equal(
|
|
68
|
+
ROLE_PERMISSIONS.admin.includes(Permission.USERS_WRITE),
|
|
69
|
+
false,
|
|
70
|
+
);
|
|
52
71
|
assert.ok(ROLE_PERMISSIONS.hr.includes(Permission.JOB_APPLICATIONS_READ));
|
|
53
|
-
assert.deepEqual(ROLE_PERMISSIONS.staff, [Permission.
|
|
72
|
+
assert.deepEqual(ROLE_PERMISSIONS.staff, [Permission.CONTACT_FORM_READ]);
|
|
54
73
|
|
|
55
74
|
assert.equal(hasPermission("super-admin", Permission.USERS_WRITE), true);
|
|
75
|
+
assert.equal(hasPermission("super-admin", Permission.OTP_ATTEMPTS_WRITE), true);
|
|
56
76
|
assert.equal(hasPermission(1, Permission.USERS_WRITE), false);
|
|
57
77
|
assert.equal(hasPermission("hr", Permission.CAREERS_WRITE), true);
|
|
58
78
|
assert.equal(hasPermission("staff", Permission.CAREERS_WRITE), false);
|
|
79
|
+
assert.equal(hasPermission("staff", Permission.CONTACT_FORM_READ), true);
|
|
80
|
+
assert.equal(hasPermission("admin", Permission.VIEWS_READ), true);
|
|
59
81
|
assert.equal(hasPermission(undefined, Permission.CAREERS_READ), false);
|
package/src/roles/index.ts
CHANGED
|
@@ -74,6 +74,11 @@ export const Permission = {
|
|
|
74
74
|
CONTACT_FORM_READ: "contact-form:read",
|
|
75
75
|
CONTACT_FORM_DELETE: "contact-form:delete",
|
|
76
76
|
JOB_APPLICATIONS_READ: "job-applications:read",
|
|
77
|
+
OTP_ATTEMPTS_READ: "otp-attempts:read",
|
|
78
|
+
OTP_ATTEMPTS_WRITE: "otp-attempts:write",
|
|
79
|
+
TWO_FACTOR_AUTH_READ: "two-factor-auth:read",
|
|
80
|
+
TWO_FACTOR_AUTH_WRITE: "two-factor-auth:write",
|
|
81
|
+
VIEWS_READ: "views:read",
|
|
77
82
|
} as const;
|
|
78
83
|
|
|
79
84
|
export type Permission = (typeof Permission)[keyof typeof Permission];
|
|
@@ -90,6 +95,11 @@ export const PERMISSION_I18N_KEY: Record<Permission, string> = {
|
|
|
90
95
|
[Permission.CONTACT_FORM_READ]: "permissions.contactFormRead",
|
|
91
96
|
[Permission.CONTACT_FORM_DELETE]: "permissions.contactFormDelete",
|
|
92
97
|
[Permission.JOB_APPLICATIONS_READ]: "permissions.jobApplicationsRead",
|
|
98
|
+
[Permission.OTP_ATTEMPTS_READ]: "permissions.otpAttemptsRead",
|
|
99
|
+
[Permission.OTP_ATTEMPTS_WRITE]: "permissions.otpAttemptsWrite",
|
|
100
|
+
[Permission.TWO_FACTOR_AUTH_READ]: "permissions.twoFactorAuthRead",
|
|
101
|
+
[Permission.TWO_FACTOR_AUTH_WRITE]: "permissions.twoFactorAuthWrite",
|
|
102
|
+
[Permission.VIEWS_READ]: "permissions.viewsRead",
|
|
93
103
|
};
|
|
94
104
|
|
|
95
105
|
export const ROLE_PERMISSIONS: Record<CanonicalRole, Permission[]> = {
|
|
@@ -105,6 +115,11 @@ export const ROLE_PERMISSIONS: Record<CanonicalRole, Permission[]> = {
|
|
|
105
115
|
Permission.CONTACT_FORM_READ,
|
|
106
116
|
Permission.CONTACT_FORM_DELETE,
|
|
107
117
|
Permission.JOB_APPLICATIONS_READ,
|
|
118
|
+
Permission.OTP_ATTEMPTS_READ,
|
|
119
|
+
Permission.OTP_ATTEMPTS_WRITE,
|
|
120
|
+
Permission.TWO_FACTOR_AUTH_READ,
|
|
121
|
+
Permission.TWO_FACTOR_AUTH_WRITE,
|
|
122
|
+
Permission.VIEWS_READ,
|
|
108
123
|
],
|
|
109
124
|
admin: [
|
|
110
125
|
Permission.ALLOWED_EMAILS_READ,
|
|
@@ -116,6 +131,7 @@ export const ROLE_PERMISSIONS: Record<CanonicalRole, Permission[]> = {
|
|
|
116
131
|
Permission.CONTACT_FORM_READ,
|
|
117
132
|
Permission.CONTACT_FORM_DELETE,
|
|
118
133
|
Permission.JOB_APPLICATIONS_READ,
|
|
134
|
+
Permission.VIEWS_READ,
|
|
119
135
|
],
|
|
120
136
|
hr: [
|
|
121
137
|
Permission.CAREERS_READ,
|
|
@@ -123,7 +139,7 @@ export const ROLE_PERMISSIONS: Record<CanonicalRole, Permission[]> = {
|
|
|
123
139
|
Permission.CAREERS_DELETE,
|
|
124
140
|
Permission.JOB_APPLICATIONS_READ,
|
|
125
141
|
],
|
|
126
|
-
staff: [Permission.
|
|
142
|
+
staff: [Permission.CONTACT_FORM_READ],
|
|
127
143
|
};
|
|
128
144
|
|
|
129
145
|
export function hasPermission(
|
package/src/time/index.test.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
+
convertTimestampToTimezone,
|
|
4
5
|
TIMEZONE,
|
|
6
|
+
TIMEZONE_CURRENT_UTC_OFFSET,
|
|
5
7
|
TIMEZONE_TO_UTC,
|
|
6
8
|
normalizeTimezone,
|
|
7
9
|
timezoneLabelKey,
|
|
@@ -21,7 +23,14 @@ assert.equal(timezoneToUtc("UTC"), "UTC+00:00");
|
|
|
21
23
|
assert.equal(timezoneToUtc("Etc/UTC"), "UTC+00:00");
|
|
22
24
|
assert.equal(timezoneToUtc("Asia/Kuala_Lumpur"), "UTC+08:00");
|
|
23
25
|
assert.equal(timezoneToUtc("kuala lumpur"), "UTC+08:00");
|
|
24
|
-
assert.equal(
|
|
26
|
+
assert.equal(
|
|
27
|
+
timezoneToUtc("America/New_York", Date.UTC(2024, 0, 1, 12, 0, 0)),
|
|
28
|
+
"UTC-05:00",
|
|
29
|
+
);
|
|
30
|
+
assert.equal(
|
|
31
|
+
timezoneToUtc("America/New_York", Date.UTC(2024, 6, 1, 12, 0, 0)),
|
|
32
|
+
"UTC-04:00",
|
|
33
|
+
);
|
|
25
34
|
assert.equal(timezoneToUtc("unknown"), undefined);
|
|
26
35
|
|
|
27
36
|
assert.equal(timezoneLabelKey("UTC"), "time.timezone.utc");
|
|
@@ -38,5 +47,30 @@ assert.deepEqual(TIMEZONE[0], {
|
|
|
38
47
|
});
|
|
39
48
|
assert.equal(TIMEZONE[2]?.value, "Asia/Singapore");
|
|
40
49
|
|
|
41
|
-
assert.equal(
|
|
42
|
-
assert.equal(
|
|
50
|
+
assert.equal(TIMEZONE_CURRENT_UTC_OFFSET.UTC, "UTC+00:00");
|
|
51
|
+
assert.equal(TIMEZONE_CURRENT_UTC_OFFSET["Asia/Kuala_Lumpur"], "UTC+08:00");
|
|
52
|
+
assert.equal(TIMEZONE_TO_UTC, TIMEZONE_CURRENT_UTC_OFFSET);
|
|
53
|
+
|
|
54
|
+
const utcNineAm = Date.UTC(2024, 0, 1, 9, 0, 0);
|
|
55
|
+
const klNineAm = Date.UTC(2024, 0, 1, 1, 0, 0);
|
|
56
|
+
|
|
57
|
+
assert.equal(
|
|
58
|
+
convertTimestampToTimezone(utcNineAm, "UTC", "Asia/Kuala_Lumpur"),
|
|
59
|
+
Date.UTC(2024, 0, 1, 1, 0, 0),
|
|
60
|
+
);
|
|
61
|
+
assert.equal(
|
|
62
|
+
convertTimestampToTimezone(
|
|
63
|
+
klNineAm,
|
|
64
|
+
"Asia/Kuala_Lumpur",
|
|
65
|
+
"America/New_York",
|
|
66
|
+
),
|
|
67
|
+
Date.UTC(2024, 0, 1, 14, 0, 0),
|
|
68
|
+
);
|
|
69
|
+
assert.equal(
|
|
70
|
+
convertTimestampToTimezone(utcNineAm, "unknown", "Asia/Kuala_Lumpur"),
|
|
71
|
+
undefined,
|
|
72
|
+
);
|
|
73
|
+
assert.equal(
|
|
74
|
+
convertTimestampToTimezone(utcNineAm, "UTC", "unknown"),
|
|
75
|
+
undefined,
|
|
76
|
+
);
|
package/src/time/index.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
+
import dayjs from "dayjs";
|
|
2
|
+
import timezone from "dayjs/plugin/timezone.js";
|
|
3
|
+
import utc from "dayjs/plugin/utc.js";
|
|
4
|
+
|
|
1
5
|
import type { MappingOption } from "../shared/types";
|
|
2
6
|
|
|
7
|
+
dayjs.extend(utc);
|
|
8
|
+
dayjs.extend(timezone);
|
|
9
|
+
|
|
3
10
|
export const TIMEZONE = [
|
|
4
11
|
{ key: 1, value: "UTC", i18nKey: "time.timezone.utc" },
|
|
5
12
|
{
|
|
@@ -82,25 +89,6 @@ export const TIMEZONE = [
|
|
|
82
89
|
export type CanonicalTimezone = (typeof TIMEZONE)[number]["value"];
|
|
83
90
|
export type TimezoneInput = CanonicalTimezone | string;
|
|
84
91
|
|
|
85
|
-
export const TIMEZONE_TO_UTC: Record<CanonicalTimezone, string> = {
|
|
86
|
-
UTC: "UTC+00:00",
|
|
87
|
-
"Asia/Kuala_Lumpur": "UTC+08:00",
|
|
88
|
-
"Asia/Singapore": "UTC+08:00",
|
|
89
|
-
"Asia/Bangkok": "UTC+07:00",
|
|
90
|
-
"Asia/Jakarta": "UTC+07:00",
|
|
91
|
-
"Asia/Manila": "UTC+08:00",
|
|
92
|
-
"Asia/Hong_Kong": "UTC+08:00",
|
|
93
|
-
"Asia/Shanghai": "UTC+08:00",
|
|
94
|
-
"Asia/Tokyo": "UTC+09:00",
|
|
95
|
-
"Australia/Sydney": "UTC+10:00",
|
|
96
|
-
"Europe/London": "UTC+00:00",
|
|
97
|
-
"Europe/Berlin": "UTC+01:00",
|
|
98
|
-
"America/New_York": "UTC-05:00",
|
|
99
|
-
"America/Chicago": "UTC-06:00",
|
|
100
|
-
"America/Denver": "UTC-07:00",
|
|
101
|
-
"America/Los_Angeles": "UTC-08:00",
|
|
102
|
-
};
|
|
103
|
-
|
|
104
92
|
const TIMEZONE_ALIAS_TO_CANONICAL: Record<string, CanonicalTimezone> = {
|
|
105
93
|
utc: "UTC",
|
|
106
94
|
"etc/utc": "UTC",
|
|
@@ -164,6 +152,7 @@ export function normalizeTimezone(
|
|
|
164
152
|
|
|
165
153
|
export function timezoneToUtc(
|
|
166
154
|
timezone: TimezoneInput | undefined,
|
|
155
|
+
timestamp = Date.now(),
|
|
167
156
|
): string | undefined {
|
|
168
157
|
const normalizedTimezone = normalizeTimezone(timezone);
|
|
169
158
|
|
|
@@ -171,9 +160,15 @@ export function timezoneToUtc(
|
|
|
171
160
|
return undefined;
|
|
172
161
|
}
|
|
173
162
|
|
|
174
|
-
return
|
|
163
|
+
return `UTC${dayjs(timestamp).tz(normalizedTimezone).format("Z")}`;
|
|
175
164
|
}
|
|
176
165
|
|
|
166
|
+
export const TIMEZONE_CURRENT_UTC_OFFSET: Record<CanonicalTimezone, string> = Object.fromEntries(
|
|
167
|
+
TIMEZONE.map(({ value }) => [value, timezoneToUtc(value)]),
|
|
168
|
+
) as Record<CanonicalTimezone, string>;
|
|
169
|
+
|
|
170
|
+
export const TIMEZONE_TO_UTC = TIMEZONE_CURRENT_UTC_OFFSET;
|
|
171
|
+
|
|
177
172
|
export function timezoneLabelKey(timezone: TimezoneInput | undefined): string {
|
|
178
173
|
const normalizedTimezone = normalizeTimezone(timezone);
|
|
179
174
|
|
|
@@ -183,3 +178,21 @@ export function timezoneLabelKey(timezone: TimezoneInput | undefined): string {
|
|
|
183
178
|
|
|
184
179
|
return TIMEZONE_I18N_KEY[normalizedTimezone];
|
|
185
180
|
}
|
|
181
|
+
|
|
182
|
+
export function convertTimestampToTimezone(
|
|
183
|
+
timestamp: number,
|
|
184
|
+
sourceTimezone: TimezoneInput | undefined,
|
|
185
|
+
targetTimezone: TimezoneInput | undefined,
|
|
186
|
+
): number | undefined {
|
|
187
|
+
const normalizedSourceTimezone = normalizeTimezone(sourceTimezone);
|
|
188
|
+
const normalizedTargetTimezone = normalizeTimezone(targetTimezone);
|
|
189
|
+
|
|
190
|
+
if (!normalizedSourceTimezone || !normalizedTargetTimezone) {
|
|
191
|
+
return undefined;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const sourceDateTime = dayjs(timestamp).tz(normalizedSourceTimezone);
|
|
195
|
+
const preservedWallClock = sourceDateTime.format("YYYY-MM-DDTHH:mm:ss.SSS");
|
|
196
|
+
|
|
197
|
+
return dayjs.tz(preservedWallClock, normalizedTargetTimezone).valueOf();
|
|
198
|
+
}
|