czapp-shared 1.0.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/README.md +2 -0
- package/dist/index.d.mts +321 -0
- package/dist/index.d.ts +321 -0
- package/dist/index.js +250 -0
- package/dist/index.mjs +204 -0
- package/package.json +32 -0
- package/src/index.ts +4 -0
- package/src/lexemes.ts +114 -0
- package/src/types.ts +194 -0
- package/src/user.ts +21 -0
- package/src/util.ts +118 -0
- package/test/util/util.test.ts +149 -0
- package/tsconfig.json +17 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { RowDataPacket } from 'mysql2';
|
|
2
|
+
import { ILexemePairRecord } from './lexemes';
|
|
3
|
+
|
|
4
|
+
export enum Language {
|
|
5
|
+
CZECH = 'cz',
|
|
6
|
+
ENGLISH = 'en',
|
|
7
|
+
UNKNOWN = 'UNKNOWN'
|
|
8
|
+
}
|
|
9
|
+
export type Result<T> = [T, undefined] | [undefined, Error];
|
|
10
|
+
|
|
11
|
+
export enum Familiarity {
|
|
12
|
+
KNOWN = "KNOWN",
|
|
13
|
+
ALMOST = "ALMOST",
|
|
14
|
+
UNKNOWN = "UNKNOWN",
|
|
15
|
+
}
|
|
16
|
+
export enum VocabCheckType {
|
|
17
|
+
FLASHCARD = 'flashcard',
|
|
18
|
+
TEST = 'test',
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export enum LearningStage {
|
|
22
|
+
NEW = 'NEW',
|
|
23
|
+
LEARNING = 'LEARNING',
|
|
24
|
+
RETIRED = 'RETIRED',
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type MySQLDate = `${number}-${number}-${number} ${number}:${number}:${number}`;
|
|
28
|
+
|
|
29
|
+
export enum ReminderFrequency {
|
|
30
|
+
ALWAYS = "always",
|
|
31
|
+
DAILY = "daily",
|
|
32
|
+
NEVER = "never",
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface ControllerResponse<T> {
|
|
36
|
+
message: string;
|
|
37
|
+
status: HttpStatusCode;
|
|
38
|
+
data: T;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface TestInitResponse {
|
|
42
|
+
lexemePairs: ILexemePairRecord[];
|
|
43
|
+
vocabCheckType: VocabCheckType;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface InsertOrUpdateResponse {
|
|
47
|
+
scenario: Scenario,
|
|
48
|
+
lexemePair: ILexemePairRecord,
|
|
49
|
+
newMappingInserted?: boolean
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export enum ApiPath {
|
|
53
|
+
AUTOLOGIN = '/autologin',
|
|
54
|
+
HEALTH = '/health',
|
|
55
|
+
LEXEMES = '/lexemes',
|
|
56
|
+
LEXEMES_ALL = '/lexemes/all',
|
|
57
|
+
LEXEMES_COUNT = '/lexemes/count/:language',
|
|
58
|
+
LEXEMES_DELETE_ALL = '/lexemes/deleteAll',
|
|
59
|
+
LEXEMES_PAGE = '/lexemes/:length/:page',
|
|
60
|
+
LEXEME_PAIR = '/lexemePair/:czId/:enId',
|
|
61
|
+
LEXEME_PAIRS_BY_TEXT = '/lexemePairsByText/:inputText/:inputLang',
|
|
62
|
+
LEXEME_PAIR_FAMILIARITY = '/lexemePair/:czId/:enId/:familiarity',
|
|
63
|
+
SIGN_IN = '/signIn',
|
|
64
|
+
SIGN_OUT = '/signOut',
|
|
65
|
+
TEST_COMPLETE = '/test/complete',
|
|
66
|
+
TEST_INIT = '/test/init',
|
|
67
|
+
TRANSLATE = '/translate',
|
|
68
|
+
TRANSLATIONS_ALL = '/lexemes/allTranslations/:sourceText/:sourceLang',
|
|
69
|
+
USER = '/user',
|
|
70
|
+
USER_RESET_TEST = '/user/test/reset/:flashcardReviewCompletedCount/:testsUnlocked',
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export enum ApiMethod {
|
|
74
|
+
GET = 'GET',
|
|
75
|
+
POST = 'POST',
|
|
76
|
+
PUT = 'PUT',
|
|
77
|
+
DELETE = 'DELETE',
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export enum AppError {
|
|
81
|
+
NONE = 'No error',
|
|
82
|
+
REQUEST_AUTOLOGIN_FAILED = 'Failed to auto-login',
|
|
83
|
+
REQUEST_DELETE_ALL_USER_DATA_FAILED = 'Failed to delete all user data',
|
|
84
|
+
REQUEST_DELETE_LEXEME_PAIR_FAILED = 'Failed to delete lexeme pair',
|
|
85
|
+
REQUEST_SIGN_IN_FAILED = 'Failed to sign in',
|
|
86
|
+
REQUEST_SIGN_OUT_FAILED = 'Failed to sign out',
|
|
87
|
+
REQUEST_GET_LEXEMES_FAILED = 'Failed to get lexemes',
|
|
88
|
+
REQUEST_GET_LEXEMES_FOR_TEST_FAILED = 'Failed to get lexemes for test',
|
|
89
|
+
REQUEST_UPDATE_FAMILIARITY_FAILED = 'Failed to update familiarity',
|
|
90
|
+
REQUEST_UPDATE_USER_SETTINGS_FAILED = 'Failed to update user settings',
|
|
91
|
+
REQUEST_INSERT_OR_UPDATE_LEXEME_PAIR_FAILED = 'Failed to insert or update lexeme pair',
|
|
92
|
+
REQUEST_TRANSLATION_FAILED = 'Failed to get translation',
|
|
93
|
+
REQUEST_TEST_COMPLETE_FAILED = 'Failed to complete test',
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export enum HttpStatusCode {
|
|
97
|
+
OK = 200,
|
|
98
|
+
BAD_REQUEST = 400,
|
|
99
|
+
UNAUTHORIZED = 401,
|
|
100
|
+
NOT_FOUND = 404,
|
|
101
|
+
TIMEOUT = 408,
|
|
102
|
+
ABORTED = 409,
|
|
103
|
+
INTERNAL_SERVER_ERROR = 500
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface ControllerResponse<T> {
|
|
107
|
+
message: string;
|
|
108
|
+
status: HttpStatusCode;
|
|
109
|
+
data: T;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
export type DeeplTranslation = {
|
|
114
|
+
detectedSourceLanguage: string,
|
|
115
|
+
translations: Array<{
|
|
116
|
+
translatedText: string
|
|
117
|
+
}>
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* RowDataPacket types - Database query results
|
|
123
|
+
*/
|
|
124
|
+
|
|
125
|
+
export interface MappedCzIds extends RowDataPacket {
|
|
126
|
+
czId: number,
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export interface MappedEnIds extends RowDataPacket {
|
|
130
|
+
enId: number,
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export interface DuplicateId extends RowDataPacket {
|
|
134
|
+
id: number,
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface InsertIdDataRecord extends RowDataPacket {
|
|
138
|
+
insertId: number;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export interface UpdateDataRecord extends RowDataPacket {}
|
|
142
|
+
|
|
143
|
+
export interface UserRecord extends RowDataPacket {
|
|
144
|
+
id: number | undefined,
|
|
145
|
+
googleSub: string,
|
|
146
|
+
name: string,
|
|
147
|
+
email: string,
|
|
148
|
+
phone: string,
|
|
149
|
+
signUpDate: Date,
|
|
150
|
+
lastModified: Date,
|
|
151
|
+
learningLanguage: string,
|
|
152
|
+
flashcardReviewCompletedCount: number,
|
|
153
|
+
currentStreakLength: number,
|
|
154
|
+
currentStreakEnd: MySQLDate | undefined,
|
|
155
|
+
highStreakLength: number,
|
|
156
|
+
highStreakEnd: MySQLDate | undefined,
|
|
157
|
+
testsUnlocked: boolean,
|
|
158
|
+
rewardPoints: number,
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export interface CountRecord extends RowDataPacket {
|
|
162
|
+
count: number;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export interface LastWordsAddedRecord extends RowDataPacket {
|
|
166
|
+
czText: string;
|
|
167
|
+
enText: string;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
export enum Scenario {
|
|
172
|
+
S1_INSERT_HAPPY_PATH = "Insert, happy path",
|
|
173
|
+
S2_INSERT_ENGLISH_LEXEME_AND_MAPPING_DUPLICATE_CZECH_LEXEME_FOUND_AND_USED = "Insert English lexeme and mapping, duplicate Czech lexeme found and used",
|
|
174
|
+
S3_INSERT_CZECH_LEXEME_AND_MAPPING_DUPLICATE_ENGLISH_LEXEME_FOUND_AND_USED = "Insert Czech lexeme and mapping, duplicate English lexeme found and used",
|
|
175
|
+
S4_INSERT_MAPPING_ONLY_DUPLICATE_CZECH_AND_ENGLISH_LEXEMES_FOUND_AND_USED = "Insert mapping only, duplicate Czech and English lexemes found and used",
|
|
176
|
+
S5_NO_INSERT_REQUIRED_DUPLICATE_LEXEMES_AND_PAIRING_FOUND = "No insert required, duplicate lexemes and pairing found",
|
|
177
|
+
S6_NO_UPDATE_REQUIRED_NO_CHANGES_DETECTED = "No update required, no changes detected",
|
|
178
|
+
S7_CZECH_LEXEME_UPDATED = "Scenario 7: Czech lexeme updated",
|
|
179
|
+
S8_CZECH_LEXEME_DELETED_DUPLICATE_CZECH_LEXEME_CAN_BE_USED = "Scenario 8: Czech lexeme deleted, duplicate Czech lexeme can be used can be used",
|
|
180
|
+
S9_CZECH_LEXEME_AND_MAPPING_DELETED_DISCOVERED_LEXEMES_AND_MAPPING_CAN_BE_USED = "Scenario 9: Czech lexeme and mapping deleted, discovered lexemes and mapping can be used",
|
|
181
|
+
S10_MAPPING_UPDATED_TO_USE_NEW_CZECH_LEXEME = "Scenario 10: Mapping updated to use new Czech lexeme",
|
|
182
|
+
S11_MAPPING_UPDATED_TO_USE_DUPLICATE_CZECH_LEXEME = "Scenario 11: Mapping updated to use duplicate Czech lexeme",
|
|
183
|
+
S12_MAPPING_DELETED_DUPLICATE_MAPPING_CAN_BE_USED_WHEN_CZECH_LEXEME_MODIFIED = "Scenario 12: Mapping deleted, duplicate mapping can be used when Czech lexeme modified",
|
|
184
|
+
S13_ENGLISH_LEXEME_UPDATED = "Scenario 13: English lexeme updated",
|
|
185
|
+
S14_ENGLISH_LEXEME_DELETED_DUPLICATE_ENGLISH_LEXEME_CAN_BE_USED = "Scenario 14: English lexeme deleted, duplicate English lexeme can be used",
|
|
186
|
+
S15_ENGLISH_LEXEME_AND_MAPPING_DELETED_DISCOVERED_LEXEMES_AND_MAPPING_CAN_BE_USED = "Scenario 15: English lexeme and mapping deleted, discovered lexemes and mapping can be used",
|
|
187
|
+
S16_MAPPING_UPDATED_TO_USE_NEW_ENGLISH_LEXEME = "Scenario 16: Mapping updated to use new English lexeme",
|
|
188
|
+
S17_MAPPING_UPDATED_TO_USE_DUPLICATE_ENGLISH_LEXEME = "Scenario 17: Mapping updated to use duplicate English lexeme",
|
|
189
|
+
S18_MAPPING_DELETED_DUPLICATE_MAPPING_CAN_BE_USED_WHEN_ENGLISH_LEXEME_MODIFIED = "Scenario 18: Mapping deleted, duplicate mapping can be used when English lexeme modified",
|
|
190
|
+
ERROR = "Error"
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
package/src/user.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Language, MySQLDate } from './types';
|
|
2
|
+
|
|
3
|
+
export type UserData = {
|
|
4
|
+
googleSub: string,
|
|
5
|
+
name: string,
|
|
6
|
+
email: string,
|
|
7
|
+
phone: string,
|
|
8
|
+
signUpDate: MySQLDate,
|
|
9
|
+
lastModified: MySQLDate,
|
|
10
|
+
learningLanguage: Language,
|
|
11
|
+
uiLanguage: Language,
|
|
12
|
+
testsCompletedCount: number,
|
|
13
|
+
currentStreakLength: number,
|
|
14
|
+
currentStreakEnd: MySQLDate,
|
|
15
|
+
highStreakLength: number,
|
|
16
|
+
highStreakEnd: MySQLDate,
|
|
17
|
+
testsUnlocked: boolean,
|
|
18
|
+
rewardPoints: number,
|
|
19
|
+
lastWordAdded: string,
|
|
20
|
+
lexemePairCount: number,
|
|
21
|
+
}
|
package/src/util.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { MySQLDate } from './index';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @name convertJSDateObjectToMySQLDateString
|
|
5
|
+
* @description Converts a JavaScript Date object to a MySQL date string
|
|
6
|
+
* @param date
|
|
7
|
+
* @type Date
|
|
8
|
+
* @returns MySQLDate in the format "2021-01-01 07:41:23"
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export const convertJSDateObjectToMySQLDateString = (date: Date): MySQLDate | undefined => {
|
|
12
|
+
let dateStr: string;
|
|
13
|
+
try {
|
|
14
|
+
dateStr = date.toISOString().slice(0, 19).replace('T', ' ');
|
|
15
|
+
} catch (error) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
if (!isValidMysqlDateString(dateStr)) {
|
|
19
|
+
return undefined;
|
|
20
|
+
} else {
|
|
21
|
+
return dateStr as MySQLDate;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @name convertJSDateStringToMySQLDate
|
|
27
|
+
* @description Converts a JavaScript Date object to a MySQL date string
|
|
28
|
+
* @param date in the format "2021-01-01T07:41:23.000Z"
|
|
29
|
+
* @type string
|
|
30
|
+
* @returns date in the format "2021-01-01 07:41:23"
|
|
31
|
+
* @returns undefined if the input date is invalid
|
|
32
|
+
*/
|
|
33
|
+
export const convertJSDateStringToMySQLDate = (date: string): MySQLDate | undefined => {
|
|
34
|
+
let dateStr: string;
|
|
35
|
+
try {
|
|
36
|
+
dateStr = date.slice(0, 19).replace('T', ' ');
|
|
37
|
+
} catch (error) {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
if (!isValidMysqlDateString(dateStr)) return undefined;
|
|
41
|
+
return dateStr as MySQLDate;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @name isValidMysqlDateString
|
|
46
|
+
* @description Checks if a string representation of date/time is in the format "2021-01-01 07:41:23"
|
|
47
|
+
* @param dateStr
|
|
48
|
+
* @returns boolean
|
|
49
|
+
*/
|
|
50
|
+
export const isValidMysqlDateString = (dateStr: string): boolean => {
|
|
51
|
+
return /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(dateStr)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @name isValidJsDateString
|
|
56
|
+
* @description Checks if a string representation of date/time is in the format "2021-01-01T07:41:23.000Z"
|
|
57
|
+
* @param dateStr
|
|
58
|
+
* @returns boolean
|
|
59
|
+
*/
|
|
60
|
+
export const isValidJsDateString = (dateStr: string): boolean => {
|
|
61
|
+
return /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/.test(dateStr)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @name isClientDateTooFarFromServerDate
|
|
66
|
+
* @description Checks if the client date is more than one day away from the server date
|
|
67
|
+
* @param clientDateMySQLFormat date in MySQL format
|
|
68
|
+
* @param serverDate date object representation of the current time on the server
|
|
69
|
+
* @returns boolean
|
|
70
|
+
*/
|
|
71
|
+
export const isClientDateTooFarFromServerDate = (clientDateMySQLFormat: MySQLDate, serverDate: Date): boolean => {
|
|
72
|
+
const serverDateMysqlFormat = convertJSDateObjectToMySQLDateString(serverDate);
|
|
73
|
+
if (!serverDateMysqlFormat) return true;
|
|
74
|
+
return Math.abs(dateDifferenceInDays(clientDateMySQLFormat, serverDateMysqlFormat)) > 1;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @name dateDifferenceInDays
|
|
79
|
+
* @description Calculates the difference in days between two dates and returns a positive integer
|
|
80
|
+
* if the first date is before the second date, and a negative integer if the first date is after the second date
|
|
81
|
+
* @param date1 date in MySQL format
|
|
82
|
+
* @param date2 date in MySQL format
|
|
83
|
+
* @returns number
|
|
84
|
+
*/
|
|
85
|
+
export const dateDifferenceInDays = (date1: MySQLDate, date2: MySQLDate): number => {
|
|
86
|
+
const dateObj_1 = new Date(date1);
|
|
87
|
+
const dateObj_2 = new Date(date2);
|
|
88
|
+
dateObj_1.setHours(0, 0, 0, 0);
|
|
89
|
+
dateObj_2.setHours(0, 0, 0, 0);
|
|
90
|
+
const difference = dateObj_2.getTime() - dateObj_1.getTime();
|
|
91
|
+
return difference / (1000 * 3600 * 24);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export const getClientDate = (date: Date): string => {
|
|
95
|
+
return `${date.getFullYear()}-${zeroPad(date.getMonth() + 1)}-${zeroPad(date.getDate())} ${zeroPad(date.getHours())}:${zeroPad(date.getMinutes())}:${zeroPad(date.getSeconds())}`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const zeroPad = (num: number): string => {
|
|
99
|
+
return num < 10 ? `0${num}` : num.toString();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export const isThePreviousDay = (date: MySQLDate, referenceDate: MySQLDate): boolean => {
|
|
103
|
+
const previousDay = new Date(referenceDate);
|
|
104
|
+
previousDay.setDate(new Date(referenceDate).getDate() - 1);
|
|
105
|
+
return new Date(date).toDateString() === previousDay.toDateString();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export const isTheSameDay = (date1: MySQLDate, date2: MySQLDate): boolean => {
|
|
109
|
+
return new Date(date1).toDateString() === new Date(date2).toDateString();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export const getFormattedDate = (date: Date) => {
|
|
113
|
+
const year = date.getFullYear();
|
|
114
|
+
const month = (1 + date.getMonth()).toString().padStart(2, '0');
|
|
115
|
+
const day = date.getDate().toString().padStart(2, '0');
|
|
116
|
+
|
|
117
|
+
return `${year}-${month}-${day}`;
|
|
118
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
import { MySQLDate } from '../../lib';
|
|
3
|
+
import {
|
|
4
|
+
convertJSDateObjectToMySQLDateString,
|
|
5
|
+
convertJSDateStringToMySQLDate, dateDifferenceInDays, isClientDateTooFarFromServerDate,
|
|
6
|
+
isThePreviousDay,
|
|
7
|
+
isTheSameDay, isValidJsDateString, isValidMysqlDateString
|
|
8
|
+
} from '../../lib/util';
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
describe('Date', () => {
|
|
12
|
+
describe('isTheSameDay', () => {
|
|
13
|
+
it('should return true if both dates are the same calendar day', () => {
|
|
14
|
+
const date1: MySQLDate = "2021-01-01 12:00:00";
|
|
15
|
+
const date2: MySQLDate = "2021-01-01 18:00:00";
|
|
16
|
+
expect(isTheSameDay(date1, date2)).to.be.true;
|
|
17
|
+
});
|
|
18
|
+
it('should return false if the provided dates are not the same calendar day', () => {
|
|
19
|
+
const date1: MySQLDate = "2021-01-01 12:00:00";
|
|
20
|
+
const date2: MySQLDate = "2021-01-02 12:00:00";
|
|
21
|
+
expect(isTheSameDay(date1, date2)).to.be.false
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe('isThePreviousDay', () => {
|
|
26
|
+
it('should return true if the date is the day before the reference day in different years', () => {
|
|
27
|
+
const today: MySQLDate = "2021-01-01 12:00:00";
|
|
28
|
+
const yesterday: MySQLDate = "2020-12-31 12:00:00";
|
|
29
|
+
expect(isThePreviousDay(yesterday, today)).to.be.true;
|
|
30
|
+
});
|
|
31
|
+
it('should return true if the date is the day before the reference day in different months', () => {
|
|
32
|
+
const today: MySQLDate = "2021-02-01 12:00:00";
|
|
33
|
+
const yesterday: MySQLDate = "2021-1-31 12:00:00";
|
|
34
|
+
expect(isThePreviousDay(yesterday, today)).to.be.true;
|
|
35
|
+
});
|
|
36
|
+
it('should return true if the date is the day before the reference day in the same month', () => {
|
|
37
|
+
const today: MySQLDate = "2021-02-15 12:00:00";
|
|
38
|
+
const yesterday: MySQLDate = "2021-02-14 12:00:00";
|
|
39
|
+
expect(isThePreviousDay(yesterday, today)).to.be.true;
|
|
40
|
+
});
|
|
41
|
+
it('should return false if the date is not the day before the reference day in the same month', () => {
|
|
42
|
+
const today: MySQLDate = "2021-02-15 12:00:00";
|
|
43
|
+
const yesterday: MySQLDate = "2021-02-16 12:00:00";
|
|
44
|
+
expect(isThePreviousDay(yesterday, today)).to.be.false;
|
|
45
|
+
});
|
|
46
|
+
it('should return false if the date is not yesterday', () => {
|
|
47
|
+
const today: MySQLDate = "2021-02-01 12:00:00";
|
|
48
|
+
const yesterday: MySQLDate = "2021-1-30 12:00:00";
|
|
49
|
+
expect(isThePreviousDay(yesterday, today)).to.be.false;
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('convertJSDateObjectToMySQLDateString', () =>{
|
|
54
|
+
it('should convert a JavaScript date object to a MySQL date string', () => {
|
|
55
|
+
const date = new Date("2021-01-01T07:41:23.000Z");
|
|
56
|
+
expect(convertJSDateObjectToMySQLDateString(date)).to.equal("2021-01-01 07:41:23");
|
|
57
|
+
});
|
|
58
|
+
it('should return undefined if the input date is invalid', () => {
|
|
59
|
+
const date = new Date("not a date");
|
|
60
|
+
expect(convertJSDateObjectToMySQLDateString(date)).to.be.undefined;
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('convertJSDateStringToMySQLDateString', ()=> {
|
|
65
|
+
it('should convert a JavaScript date string to a MySQL date string', () => {
|
|
66
|
+
const date = "2021-01-01T07:41:23.000Z";
|
|
67
|
+
expect(convertJSDateStringToMySQLDate(date)).to.equal("2021-01-01 07:41:23");
|
|
68
|
+
});
|
|
69
|
+
it('should return undefined if the input date is invalid', () => {
|
|
70
|
+
const date = "not a date";
|
|
71
|
+
expect(convertJSDateStringToMySQLDate(date)).to.be.undefined;
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe('isValidMysqlDateString', () => {
|
|
76
|
+
it('should return true if the input string is a valid MySQL date string', () => {
|
|
77
|
+
const date = "2021-01-01 07:41:23";
|
|
78
|
+
expect(isValidMysqlDateString(date)).to.be.true;
|
|
79
|
+
});
|
|
80
|
+
it('should return false if the input string is not a valid MySQL date string', () => {
|
|
81
|
+
const date = "2021-01-01 07:41:232";
|
|
82
|
+
expect(isValidMysqlDateString(date)).to.be.false;
|
|
83
|
+
});
|
|
84
|
+
it('should return false if the input string is in the format 2021-01-01T07:41:23.000Z', () => {
|
|
85
|
+
const date = "2021-01-01T07:41:23.000Z";
|
|
86
|
+
expect(isValidMysqlDateString(date)).to.be.false;
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe('isValidJsDateString', () => {
|
|
91
|
+
it('should return true if the input string is a valid JavaScript date string', () => {
|
|
92
|
+
const date = "2021-01-01T07:41:23.000Z";
|
|
93
|
+
expect(isValidJsDateString(date)).to.be.true;
|
|
94
|
+
});
|
|
95
|
+
it('should return false if the input string is not a valid JavaScript date string', () => {
|
|
96
|
+
const date = "2021-01-01T07:41:232";
|
|
97
|
+
expect(isValidJsDateString(date)).to.be.false;
|
|
98
|
+
});
|
|
99
|
+
it('should return false if the input string is in the format 2021-01-01 07:41:23', () => {
|
|
100
|
+
const date = "2021-01-01 07:41:23";
|
|
101
|
+
expect(isValidJsDateString(date)).to.be.false;
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
describe('isClientDateTooFarFromServerDate', () => {
|
|
105
|
+
it('should return true if the client date is more than one full day ahead of the server date', () => {
|
|
106
|
+
const clientDate: MySQLDate = "2024-08-24 12:00:00";
|
|
107
|
+
const serverDate = new Date("2024-08-22 12:00:00");
|
|
108
|
+
expect(isClientDateTooFarFromServerDate(clientDate, serverDate)).to.be.true;
|
|
109
|
+
});
|
|
110
|
+
it('should return true if the client date is more than one full day behind the server date', () => {
|
|
111
|
+
const clientDate: MySQLDate = "2024-08-20 12:00:00";
|
|
112
|
+
const serverDate = new Date("2024-08-22 12:00:00");
|
|
113
|
+
expect(isClientDateTooFarFromServerDate(clientDate, serverDate)).to.be.true;
|
|
114
|
+
});
|
|
115
|
+
it('should return false if the client date is less than one full day ahead of the server date', () => {
|
|
116
|
+
const clientDate: MySQLDate = "2024-08-23 12:00:01";
|
|
117
|
+
const serverDate = new Date("2024-08-22 12:00:00");
|
|
118
|
+
expect(isClientDateTooFarFromServerDate(clientDate, serverDate)).to.be.false;
|
|
119
|
+
});
|
|
120
|
+
it('should return false if the client date is less than one full day behind the server date', () => {
|
|
121
|
+
const clientDate: MySQLDate = "2024-08-21 11:59:59";
|
|
122
|
+
const serverDate = new Date("2024-08-22 12:00:00");
|
|
123
|
+
expect(isClientDateTooFarFromServerDate(clientDate, serverDate)).to.be.false;
|
|
124
|
+
});
|
|
125
|
+
it('should return false if the client date is the same as the server date', () => {
|
|
126
|
+
const clientDate: MySQLDate = "2024-08-22 00:00:00";
|
|
127
|
+
const serverDate = new Date("2024-08-22 23:59:59");
|
|
128
|
+
expect(isClientDateTooFarFromServerDate(clientDate, serverDate)).to.be.false;
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
describe('dateDifferenceInDays', () => {
|
|
133
|
+
it('should return a positive date difference when date1 is before date2', () => {
|
|
134
|
+
const date1: MySQLDate = "2024-08-20 12:00:00";
|
|
135
|
+
const date2: MySQLDate = "2024-08-22 12:00:00";
|
|
136
|
+
expect(dateDifferenceInDays(date1, date2)).to.equal(2);
|
|
137
|
+
});
|
|
138
|
+
it('should return a negative date difference when date1 is after date2', () => {
|
|
139
|
+
const date1: MySQLDate = "2024-08-22 12:00:00";
|
|
140
|
+
const date2: MySQLDate = "2024-08-20 12:00:00";
|
|
141
|
+
expect(dateDifferenceInDays(date1, date2)).to.equal(-2);
|
|
142
|
+
});
|
|
143
|
+
it('should return the difference in days between two dates', () => {
|
|
144
|
+
const date1: MySQLDate = "2024-08-20 12:00:00";
|
|
145
|
+
const date2: MySQLDate = "2024-08-20 12:00:00";
|
|
146
|
+
expect(dateDifferenceInDays(date1, date2)).to.equal(0);
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES6",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"outDir": "./lib",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"sourceMap": true,
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"lib": ["es2017"],
|
|
10
|
+
"noImplicitAny": true,
|
|
11
|
+
"removeComments": false,
|
|
12
|
+
},
|
|
13
|
+
"include": [
|
|
14
|
+
"src/**/*"
|
|
15
|
+
],
|
|
16
|
+
"exclude": ["node_modules", "test"]
|
|
17
|
+
}
|