@cuemath/web-utils 0.0.1-beta.0 → 0.0.1-beta.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/.eslintignore +2 -0
- package/.husky/pre-commit +1 -0
- package/.huskyrc +5 -0
- package/.lintstagedrc.yml +5 -0
- package/dist/index.js +3 -0
- package/lerna.json +8 -0
- package/package-lock.json +7528 -0
- package/package.json +22 -5
- package/src/constants/country/index.ts +4 -0
- package/src/constants/country/iso-country-code.ts +248 -0
- package/src/constants/date-time/e-cna-international-timezones.ts +157 -0
- package/src/constants/date-time/index.ts +78 -0
- package/src/constants/e-cna/index.ts +502 -0
- package/src/constants/index.ts +2 -0
- package/src/cookie/index.ts +59 -1
- package/src/date-time-helper/index.ts +363 -0
- package/src/e-cna/flow/aggregator.ts +6 -0
- package/src/e-cna/flow/index.ts +1 -0
- package/src/e-cna/flow/variants/default.ts +40 -0
- package/src/e-cna/flow/variants/index.ts +1 -0
- package/src/e-cna/index.ts +264 -0
- package/src/e-cna/slides/child_hobbies/index.ts +1 -0
- package/src/e-cna/slides/child_hobbies/variants.ts +13 -0
- package/src/e-cna/slides/child_personality/index.ts +1 -0
- package/src/e-cna/slides/child_personality/variants.ts +12 -0
- package/src/e-cna/slides/grade/index.ts +1 -0
- package/src/e-cna/slides/grade/variants.ts +27 -0
- package/src/e-cna/slides/index.ts +16 -0
- package/src/e-cna/slides/intro/index.ts +1 -0
- package/src/e-cna/slides/intro/variants.ts +7 -0
- package/src/e-cna/slides/key_need_l1/index.ts +1 -0
- package/src/e-cna/slides/key_need_l1/variants.ts +44 -0
- package/src/e-cna/slides/key_need_l2/index.ts +1 -0
- package/src/e-cna/slides/key_need_l2/variants.ts +143 -0
- package/src/e-cna/slides/key_need_l3/index.ts +1 -0
- package/src/e-cna/slides/key_need_l3/variants.ts +13 -0
- package/src/e-cna/slides/math-perception/index.ts +1 -0
- package/src/e-cna/slides/math-perception/variants.ts +30 -0
- package/src/e-cna/slides/other_curriculum/index.ts +1 -0
- package/src/e-cna/slides/other_curriculum/variants.ts +14 -0
- package/src/e-cna/slides/school_performance_5_12/index.ts +1 -0
- package/src/e-cna/slides/school_performance_5_12/variants.ts +23 -0
- package/src/e-cna/slides/school_performance_kg_4/index.ts +1 -0
- package/src/e-cna/slides/school_performance_kg_4/variants.ts +25 -0
- package/src/e-cna/slides/secondary_needs/index.ts +1 -0
- package/src/e-cna/slides/secondary_needs/variants.ts +26 -0
- package/src/e-cna/slides/signup/index.ts +1 -0
- package/src/e-cna/slides/signup/variants.ts +7 -0
- package/src/e-cna/slides/signup_refer/index.ts +1 -0
- package/src/e-cna/slides/signup_refer/variants.ts +16 -0
- package/src/e-cna/slides/slot_pick/index.ts +1 -0
- package/src/e-cna/slides/slot_pick/variants.ts +7 -0
- package/src/e-cna/utils/index.ts +29 -0
- package/src/index.ts +3 -0
- package/src/local-storage/index.ts +191 -0
- package/.vscode/settings.json +0 -1
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
export const LOCAL_STORAGE_PRIMARY_KEY = 'CUEMATH_WEB';
|
|
3
|
+
|
|
4
|
+
export const checkLocalStorageSupport = () => {
|
|
5
|
+
try {
|
|
6
|
+
return 'localStorage' in window && window.localStorage !== null;
|
|
7
|
+
} catch (e) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const removeKeyFromObject = (obj: { [x: string]: any }, keyToRemove: string) => {
|
|
13
|
+
if (obj) {
|
|
14
|
+
return Object.keys(obj)
|
|
15
|
+
.filter(key => key !== String(keyToRemove))
|
|
16
|
+
.reduce((accumulator: any, key: string) => {
|
|
17
|
+
accumulator[key] = obj[key];
|
|
18
|
+
|
|
19
|
+
return accumulator;
|
|
20
|
+
}, {});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return null;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// Default expiry set to 24 hours
|
|
27
|
+
export const localStorageSet = (key: string | number, value: any, ttl = 86400000) => {
|
|
28
|
+
if (checkLocalStorageSupport()) {
|
|
29
|
+
try {
|
|
30
|
+
let dbData: any = window.localStorage.getItem(LOCAL_STORAGE_PRIMARY_KEY);
|
|
31
|
+
const now = new Date();
|
|
32
|
+
|
|
33
|
+
const item = {
|
|
34
|
+
value,
|
|
35
|
+
expiry: now.getTime() + ttl,
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
if (dbData) {
|
|
39
|
+
dbData = JSON.parse(dbData);
|
|
40
|
+
dbData[key] = item;
|
|
41
|
+
} else {
|
|
42
|
+
dbData = { [key]: item };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
window.localStorage.setItem(LOCAL_STORAGE_PRIMARY_KEY, JSON.stringify(dbData));
|
|
46
|
+
|
|
47
|
+
return true;
|
|
48
|
+
} catch (e) {
|
|
49
|
+
return false;
|
|
50
|
+
} // case when localStorage space quota is exceeded
|
|
51
|
+
} else {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const localStorageGet = (key: string) => {
|
|
57
|
+
if (checkLocalStorageSupport()) {
|
|
58
|
+
try {
|
|
59
|
+
const primaryLocalStorageValue = window.localStorage.getItem(
|
|
60
|
+
LOCAL_STORAGE_PRIMARY_KEY,
|
|
61
|
+
);
|
|
62
|
+
let dbData =
|
|
63
|
+
primaryLocalStorageValue != null
|
|
64
|
+
? JSON.parse(primaryLocalStorageValue)
|
|
65
|
+
: primaryLocalStorageValue;
|
|
66
|
+
const requestedData = dbData[key];
|
|
67
|
+
const now = new Date();
|
|
68
|
+
|
|
69
|
+
if (now.getTime() > requestedData.expiry) {
|
|
70
|
+
dbData = removeKeyFromObject(dbData, key);
|
|
71
|
+
window.localStorage.setItem(LOCAL_STORAGE_PRIMARY_KEY, JSON.stringify(dbData));
|
|
72
|
+
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return requestedData.value;
|
|
77
|
+
} catch (e) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
} else {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export const localStorageGetAll = () => {
|
|
86
|
+
if (checkLocalStorageSupport()) {
|
|
87
|
+
try {
|
|
88
|
+
const primaryLocalStorageValue = window.localStorage.getItem(
|
|
89
|
+
LOCAL_STORAGE_PRIMARY_KEY,
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
return primaryLocalStorageValue != null
|
|
93
|
+
? JSON.parse(primaryLocalStorageValue)
|
|
94
|
+
: primaryLocalStorageValue;
|
|
95
|
+
} catch (e) {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export const localStorageRemove = (key: string) => {
|
|
104
|
+
if (checkLocalStorageSupport()) {
|
|
105
|
+
try {
|
|
106
|
+
const primaryLocalStorageValue = window.localStorage.getItem(
|
|
107
|
+
LOCAL_STORAGE_PRIMARY_KEY,
|
|
108
|
+
);
|
|
109
|
+
let dbData =
|
|
110
|
+
primaryLocalStorageValue != null
|
|
111
|
+
? JSON.parse(primaryLocalStorageValue)
|
|
112
|
+
: primaryLocalStorageValue;
|
|
113
|
+
|
|
114
|
+
if (dbData[key]) {
|
|
115
|
+
dbData = removeKeyFromObject(dbData, key);
|
|
116
|
+
window.localStorage.setItem(LOCAL_STORAGE_PRIMARY_KEY, JSON.stringify(dbData));
|
|
117
|
+
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return false;
|
|
122
|
+
} catch (e) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
} else {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
export const localStorageRemoveAll = () => {
|
|
131
|
+
if (checkLocalStorageSupport()) {
|
|
132
|
+
try {
|
|
133
|
+
const primaryLocalStorageValue = window.localStorage.getItem(
|
|
134
|
+
LOCAL_STORAGE_PRIMARY_KEY,
|
|
135
|
+
);
|
|
136
|
+
const dbData =
|
|
137
|
+
primaryLocalStorageValue != null
|
|
138
|
+
? JSON.parse(primaryLocalStorageValue)
|
|
139
|
+
: primaryLocalStorageValue;
|
|
140
|
+
|
|
141
|
+
if (dbData) {
|
|
142
|
+
window.localStorage.removeItem(LOCAL_STORAGE_PRIMARY_KEY);
|
|
143
|
+
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return false;
|
|
148
|
+
} catch (e) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
} else {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
export const removeExpiredKeysOnMount = () => {
|
|
157
|
+
if (checkLocalStorageSupport()) {
|
|
158
|
+
try {
|
|
159
|
+
const primaryLocalStorageValue = window.localStorage.getItem(
|
|
160
|
+
LOCAL_STORAGE_PRIMARY_KEY,
|
|
161
|
+
);
|
|
162
|
+
let dbData =
|
|
163
|
+
primaryLocalStorageValue != null
|
|
164
|
+
? JSON.parse(primaryLocalStorageValue)
|
|
165
|
+
: primaryLocalStorageValue;
|
|
166
|
+
const now = new Date();
|
|
167
|
+
|
|
168
|
+
if (dbData) {
|
|
169
|
+
dbData = Object.keys(dbData).reduce((accumulator: any, key) => {
|
|
170
|
+
if (now.getTime() < dbData[key].expiry) {
|
|
171
|
+
accumulator[key] = dbData[key];
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return accumulator;
|
|
175
|
+
}, {});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (Object.keys(dbData).length) {
|
|
179
|
+
window.localStorage.setItem(LOCAL_STORAGE_PRIMARY_KEY, JSON.stringify(dbData));
|
|
180
|
+
} else {
|
|
181
|
+
localStorageRemoveAll();
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return true;
|
|
185
|
+
} catch (e) {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
} else {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
};
|
package/.vscode/settings.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{}
|