@cuemath/web-utils 1.0.1 → 1.0.3-beta.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.
Files changed (56) hide show
  1. package/.eslintignore +2 -0
  2. package/.husky/pre-commit +1 -0
  3. package/.huskyrc +5 -0
  4. package/.lintstagedrc.yml +1 -0
  5. package/dist/constants/country/index.d.ts +1 -0
  6. package/dist/constants/country/index.js +6 -0
  7. package/dist/constants/country/index.js.map +1 -0
  8. package/dist/constants/country/iso-country-code.d.ts +248 -0
  9. package/dist/constants/country/iso-country-code.js +252 -0
  10. package/dist/constants/country/iso-country-code.js.map +1 -0
  11. package/dist/constants/date-time/index.d.ts +17 -0
  12. package/dist/constants/date-time/index.js +42 -0
  13. package/dist/constants/date-time/index.js.map +1 -0
  14. package/dist/constants/date-time/international-timezones.d.ts +7 -0
  15. package/dist/constants/date-time/international-timezones.js +155 -0
  16. package/dist/constants/date-time/international-timezones.js.map +1 -0
  17. package/dist/constants/index.d.ts +1 -0
  18. package/dist/constants/index.js +18 -0
  19. package/dist/constants/index.js.map +1 -0
  20. package/dist/date-time-helper/e-cna/index.d.ts +4 -0
  21. package/dist/date-time-helper/e-cna/index.js +40 -0
  22. package/dist/date-time-helper/e-cna/index.js.map +1 -0
  23. package/dist/date-time-helper/index.d.ts +30 -0
  24. package/dist/date-time-helper/index.js +107 -0
  25. package/dist/date-time-helper/index.js.map +1 -0
  26. package/dist/e-cna/index.d.ts +1 -0
  27. package/dist/e-cna/index.js +11 -0
  28. package/dist/e-cna/index.js.map +1 -0
  29. package/dist/growth-source/index.js +32 -13
  30. package/dist/growth-source/index.js.map +1 -1
  31. package/dist/index.d.ts +6 -0
  32. package/dist/index.js +6 -0
  33. package/dist/index.js.map +1 -1
  34. package/dist/local-storage/index.d.ts +13 -0
  35. package/dist/local-storage/index.js +173 -0
  36. package/dist/local-storage/index.js.map +1 -0
  37. package/dist/object/index.d.ts +2 -0
  38. package/dist/object/index.js +24 -0
  39. package/dist/object/index.js.map +1 -0
  40. package/dist/url/index.d.ts +3 -0
  41. package/dist/url/index.js +20 -0
  42. package/dist/url/index.js.map +1 -0
  43. package/package.json +9 -5
  44. package/src/constants/country/index.ts +1 -0
  45. package/src/constants/country/iso-country-code.ts +248 -0
  46. package/src/constants/date-time/index.ts +43 -0
  47. package/src/constants/date-time/international-timezones.ts +157 -0
  48. package/src/constants/index.ts +1 -0
  49. package/src/date-time-helper/e-cna/index.ts +44 -0
  50. package/src/date-time-helper/index.ts +139 -0
  51. package/src/e-cna/index.ts +8 -0
  52. package/src/growth-source/index.ts +35 -16
  53. package/src/index.ts +6 -0
  54. package/src/local-storage/index.ts +196 -0
  55. package/src/object/index.ts +21 -0
  56. package/src/url/index.ts +19 -0
@@ -0,0 +1,157 @@
1
+ interface Timezone {
2
+ id: string;
3
+ value: string;
4
+ meta: string;
5
+ }
6
+
7
+ export const timezones: Timezone[] = [
8
+ {
9
+ id: 'Pacific/Honolulu',
10
+ value: 'HST(GMT-10)',
11
+ meta: 'HST(GMT-10)',
12
+ },
13
+ {
14
+ id: 'America/Anchorage',
15
+ value: 'AST(GMT-9)',
16
+ meta: 'AST(GMT-9)',
17
+ },
18
+ {
19
+ id: 'America/Los_Angeles',
20
+ value: 'PST(GMT-8)',
21
+ meta: 'PST(GMT-8)',
22
+ },
23
+ {
24
+ id: 'America/Denver',
25
+ value: 'MST(GMT-7)',
26
+ meta: 'MST(GMT-7)',
27
+ },
28
+ {
29
+ id: 'America/Phoenix',
30
+ value: 'MST(GMT-7)',
31
+ meta: 'MST(GMT-7)',
32
+ },
33
+ {
34
+ id: 'America/Matamoros',
35
+ value: 'CST(GMT-6)',
36
+ meta: 'CST(GMT-6)',
37
+ },
38
+ {
39
+ id: 'America/New_York',
40
+ value: 'EST(GMT-5)',
41
+ meta: 'EST(GMT-5)',
42
+ },
43
+ {
44
+ id: 'America/Glace_Bay',
45
+ value: 'AST(GMT-4)',
46
+ meta: 'AST(GMT-4)',
47
+ },
48
+ {
49
+ id: 'America/St_Johns',
50
+ value: 'NST(GMT-3:30)',
51
+ meta: 'NST(GMT-3:30)',
52
+ },
53
+ {
54
+ id: 'America/Noronha',
55
+ value: 'FNT(GMT-2)',
56
+ meta: 'FNT(GMT-2)',
57
+ },
58
+ {
59
+ id: 'Asia/Calcutta',
60
+ value: 'IST(GMT+5:30)',
61
+ meta: 'IST(GMT+5:30)',
62
+ },
63
+ {
64
+ id: 'Asia/Kolkata',
65
+ value: 'IST(GMT+5:30)',
66
+ meta: 'IST(GMT+5:30)',
67
+ },
68
+ {
69
+ id: 'Asia/Dubai',
70
+ value: 'GST(GMT+4)',
71
+ meta: 'GST(GMT+4)',
72
+ },
73
+ {
74
+ id: 'Asia/Singapore',
75
+ value: 'SGT(GMT+8)',
76
+ meta: 'SGT(GMT+8)',
77
+ },
78
+ {
79
+ id: 'Pacific/Auckland',
80
+ value: 'NZDT(GMT+13)',
81
+ meta: 'NZDT(GMT+13)',
82
+ },
83
+ {
84
+ id: 'Australia/Adelaide',
85
+ value: 'ACDT(GMT+10:30)',
86
+ meta: 'ACDT(GMT+10:30)',
87
+ },
88
+ {
89
+ id: 'Australia/Perth',
90
+ value: 'AWST(GMT+8)',
91
+ meta: 'AWST(GMT+8)',
92
+ },
93
+ {
94
+ id: 'Australia/Sydney',
95
+ value: 'AEDT(GMT+11)',
96
+ meta: 'AEDT(GMT+11)',
97
+ },
98
+ {
99
+ id: 'Australia/Darwin',
100
+ value: 'ACST(GMT+9:30)',
101
+ meta: 'ACST(GMT+9:30)',
102
+ },
103
+ {
104
+ id: 'Australia/Broken_Hill',
105
+ value: 'ACDT(GMT+10:30)',
106
+ meta: 'ACDT(GMT+10:30)',
107
+ },
108
+ {
109
+ id: 'Australia/Melbourne',
110
+ value: 'AEDT(GMT+11)',
111
+ meta: 'AEDT(GMT+11)',
112
+ },
113
+ {
114
+ id: 'Australia/Brisbane',
115
+ value: 'AEST(GMT+10)',
116
+ meta: 'AEST(GMT+10)',
117
+ },
118
+ {
119
+ id: 'Australia/Hobart',
120
+ value: 'AEDT(GMT+11)',
121
+ meta: 'AEDT(GMT+11)',
122
+ },
123
+ {
124
+ id: 'Europe/London',
125
+ value: 'GMT(GMT+0)',
126
+ meta: 'GMT(GMT+0)',
127
+ },
128
+ {
129
+ id: 'Asia/Muscat',
130
+ value: 'GST(GMT+4)',
131
+ meta: 'GST(GMT+4)',
132
+ },
133
+ {
134
+ id: 'Asia/Kuwait',
135
+ value: 'AST(GMT+3)',
136
+ meta: 'AST(GMT+3)',
137
+ },
138
+ {
139
+ id: 'Asia/Bahrain',
140
+ value: 'AST(GMT+3)',
141
+ meta: 'AST(GMT+3)',
142
+ },
143
+ {
144
+ id: 'Asia/Qatar',
145
+ value: 'AST(GMT+3)',
146
+ meta: 'AST(GMT+3)',
147
+ },
148
+ {
149
+ id: 'Asia/Riyadh',
150
+ value: 'AST(GMT+3)',
151
+ meta: 'AST(GMT+3)',
152
+ },
153
+ ].map(timezone => ({
154
+ ...timezone,
155
+ key: timezone.id,
156
+ label: timezone.meta,
157
+ }));
@@ -0,0 +1 @@
1
+ export * from './country';
@@ -0,0 +1,44 @@
1
+ import { ISO_COUNTRY_CODE } from '../../constants/country/index';
2
+ import { TWENTY_FOUR_HOURS_IN_SECONDS } from '../../constants/date-time';
3
+ import { getCurrentDatebyTimezone } from '../index';
4
+
5
+ export const getDates = (
6
+ timezone: string,
7
+ platform: string,
8
+ country: string,
9
+ ): Array<{ futureDate: number; disabled: boolean }> => {
10
+ const currentDate = Date.parse(getCurrentDatebyTimezone(timezone)) / 1000;
11
+ const dates = [];
12
+
13
+ let days = 7;
14
+ let buffer = 1;
15
+
16
+ switch (platform) {
17
+ case 'astro':
18
+ if (country === ISO_COUNTRY_CODE.INDIA) {
19
+ days = 10;
20
+ buffer = 2;
21
+ }
22
+ break;
23
+ case 'website':
24
+ if (country === ISO_COUNTRY_CODE.INDIA) {
25
+ days = 10;
26
+ buffer = 2;
27
+ }
28
+ break;
29
+ default:
30
+ days = 7;
31
+ buffer = 1;
32
+ break;
33
+ }
34
+
35
+ // getting dates by adding 86400 seconds(24 hours)
36
+ for (let i = 0; i < days; i += 1) {
37
+ dates.push({
38
+ futureDate: currentDate + TWENTY_FOUR_HOURS_IN_SECONDS * i,
39
+ disabled: !(i > buffer),
40
+ });
41
+ }
42
+
43
+ return dates;
44
+ };
@@ -0,0 +1,139 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import dayjs from 'dayjs';
3
+ import relativeTime from 'dayjs/plugin/relativeTime';
4
+
5
+ import { timezones } from '../constants/date-time/international-timezones';
6
+
7
+ type Timestamp = number;
8
+
9
+ export interface DateSlots {
10
+ key: number;
11
+ label: string;
12
+ subLabel: string;
13
+ disabled: boolean;
14
+ }
15
+
16
+ export const formatDateTime = (
17
+ dateTime: string | number | Date | dayjs.Dayjs | null | undefined,
18
+ format: string | undefined,
19
+ ) => {
20
+ const momentDate = dayjs(dateTime);
21
+
22
+ return momentDate.format(format);
23
+ };
24
+
25
+ export function getCurrentTimestamp(): number {
26
+ if (!Date.now) {
27
+ Date.now = () => new Date().getTime();
28
+ }
29
+
30
+ return Math.floor(Date.now() / 1000);
31
+ }
32
+
33
+ export function dateTimeByTimezone(timestamp: Timestamp, timezone?: string): string {
34
+ let options = {};
35
+
36
+ if (timezone) {
37
+ options = {
38
+ timeZone: timezone,
39
+ };
40
+ }
41
+
42
+ return new Date(timestamp).toLocaleString('en-US', options);
43
+ }
44
+
45
+ export function dateByTimezone(
46
+ timestamp: number,
47
+ timezone: string,
48
+ format: string,
49
+ ): string {
50
+ const datetime = dateTimeByTimezone(timestamp, timezone);
51
+
52
+ return dayjs(datetime).format(format || 'mm/dd/yyyy');
53
+ }
54
+
55
+ export const dateByTimestamp = (timestamp: Timestamp, format: string): string => {
56
+ const datetime = new Date(timestamp).toLocaleString('en-US');
57
+
58
+ return dayjs(datetime).format(format || 'mm/dd/yyyy');
59
+ };
60
+
61
+ export function convertTimeStampToSeconds(timeStamp: Timestamp): number {
62
+ const demoTimeStamp = timeStamp;
63
+ const currentTimeStamp = getCurrentTimestamp();
64
+
65
+ if (Number.isNaN(demoTimeStamp) || Number.isNaN(currentTimeStamp)) {
66
+ return timeStamp;
67
+ }
68
+
69
+ const demoTimeStampLength = String(demoTimeStamp).length;
70
+ const currentTimeStampLength = String(currentTimeStamp).length;
71
+
72
+ return demoTimeStampLength > currentTimeStampLength ? timeStamp / 1000 : timeStamp;
73
+ }
74
+
75
+ export function formatTimestamp(datetime: Timestamp, format: string | undefined): string {
76
+ const timeStamp = convertTimeStampToSeconds(datetime);
77
+
78
+ return dayjs.unix(timeStamp).format(format);
79
+ }
80
+
81
+ export function getTimezone(): string {
82
+ return Intl?.DateTimeFormat().resolvedOptions().timeZone;
83
+ }
84
+
85
+ export function timeToX(
86
+ datetime: string | number | Date | dayjs.Dayjs | null | undefined,
87
+ ): string {
88
+ dayjs.extend(relativeTime);
89
+
90
+ return dayjs().to(datetime, true);
91
+ }
92
+
93
+ export function getCurrentDatebyTimezone(timezone: string): string {
94
+ let options = {};
95
+
96
+ if (timezone) {
97
+ options = {
98
+ timeZone: timezone,
99
+ };
100
+ }
101
+
102
+ return new Date().toLocaleString('en-US', options);
103
+ }
104
+
105
+ export function getTimeDiff(
106
+ date1: string | number | Date | dayjs.Dayjs | null | undefined,
107
+ date2: string | number | Date | dayjs.Dayjs | null | undefined,
108
+ ): { days: any; hour: any; minute: any; seconds: any; distance: any } {
109
+ const dateTime1 = dayjs(date1);
110
+ const dateTime2 = dayjs(date2);
111
+
112
+ return {
113
+ days: dateTime1.diff(dateTime2, 'days'),
114
+ hour: dateTime1.diff(dateTime2, 'hour'),
115
+ minute: dateTime1.diff(dateTime2, 'minute'),
116
+ seconds: dateTime1.diff(dateTime2, 'seconds'),
117
+ distance: dateTime1.diff(dateTime2),
118
+ };
119
+ }
120
+
121
+ export function getISOTimeString(demoTs: number): string {
122
+ return new Date(demoTs * 1000).toISOString().replace(/-|:|\.\d\d\d/g, '');
123
+ }
124
+
125
+ export function getDateAfterDays(days: number): string {
126
+ return formatDateTime(dayjs().add(days, 'day'), 'D MMM, YYYY').toUpperCase();
127
+ }
128
+
129
+ export const convertedTimezone = (selectedTimezone: string): string => {
130
+ const formattedTimezone = timezones?.find(
131
+ currentTimezone => currentTimezone?.id === selectedTimezone,
132
+ );
133
+
134
+ return formattedTimezone?.value ?? selectedTimezone?.replaceAll('_', ' ');
135
+ };
136
+
137
+ export const convertSecondsToMiliseconds = (seconds: number) => {
138
+ return seconds * 1000;
139
+ };
@@ -0,0 +1,8 @@
1
+ import { COUNTRY_NAME_COUNTRY_CODE_MAP as ISO_COUNTRY_CODE } from '../constants/country/iso-country-code';
2
+ import { getCookie } from '../cookie';
3
+
4
+ export const showLaptopQuestion = () => {
5
+ const cueCountryCode = getCookie('cue_country_code');
6
+
7
+ return cueCountryCode === ISO_COUNTRY_CODE.INDIA;
8
+ };
@@ -26,7 +26,7 @@ export const readSourceCookie = (): SourceDetailsCookie => {
26
26
  };
27
27
 
28
28
  const writeSourceCookie = (sourceDetails: SourceDetailsCookie): void => {
29
- createCookie(SOURCE_DETAILS_COOKIE, JSON.stringify(sourceDetails), 365, true);
29
+ createCookie(SOURCE_DETAILS_COOKIE, JSON.stringify(sourceDetails), 365);
30
30
  };
31
31
 
32
32
  export const isSourcePresent = (): boolean => {
@@ -45,7 +45,7 @@ const updateSourceCookie = (sourceDetail: Partial<SourceDetailsCookie>): void =>
45
45
  writeSourceCookie(updatedDetails);
46
46
  };
47
47
 
48
- const getRevenueChannel = (platform: Platform, utmParams?: UTMParams): RevenueChannel => {
48
+ const getRevenueChannel = (platform: Platform, utmSource?: string): RevenueChannel => {
49
49
  let revenueChannel: RevenueChannel = 'unknown';
50
50
 
51
51
  // set defaults for WEBSITE and ASTRO
@@ -55,9 +55,7 @@ const getRevenueChannel = (platform: Platform, utmParams?: UTMParams): RevenueCh
55
55
  revenueChannel = 'performance';
56
56
  }
57
57
 
58
- if (utmParams) {
59
- const { utm_source: utmSource = '' } = utmParams;
60
-
58
+ if (utmSource) {
61
59
  switch (true) {
62
60
  case /^affiliate-/.test(utmSource):
63
61
  revenueChannel = 'affiliate';
@@ -99,8 +97,8 @@ const getFlow = (platform: Platform, experiments?: Experiments): SignupFlow => {
99
97
  return flow;
100
98
  };
101
99
 
102
- const getUTMParams = (): UTMParams | undefined => {
103
- if (typeof window === undefined) return;
100
+ const getUTMParams = (): Partial<UTMParams> => {
101
+ if (typeof window === undefined) return {};
104
102
 
105
103
  const search = new URLSearchParams(window.location.search);
106
104
  const utmParams = UTM_PARAMS.reduce((acc: UTMParams, param) => {
@@ -128,11 +126,9 @@ const getFirstPage = (): string => {
128
126
  organic-<channel>
129
127
  offline-<channel>
130
128
  */
131
- const getChannel = (utmParams: UTMParams | undefined): string => {
129
+ const getChannel = (utmSource?: string): string => {
132
130
  // TODO: UTM source patter for FB, GOOGLE...
133
- if (!utmParams) return '';
134
-
135
- const { utm_source: utmSource = '' } = utmParams;
131
+ if (!utmSource) return '';
136
132
 
137
133
  return utmSource.split('-')[1];
138
134
  };
@@ -150,11 +146,13 @@ export const initSourceDetails = async ({
150
146
  performanceChannel,
151
147
  revenueChannel,
152
148
  }: SourceDetails): Promise<void> => {
153
- if (!isSourcePresent()) {
154
- const utmParams = getUTMParams();
155
- const leadChannel = getChannel(utmParams);
149
+ const utmParams: Partial<UTMParams> = getUTMParams();
150
+ const { utm_source: utmSource } = utmParams;
151
+ const leadChannel = getChannel(utmSource);
156
152
 
157
- revenueChannel = revenueChannel || getRevenueChannel(platform, utmParams);
153
+ revenueChannel = revenueChannel || getRevenueChannel(platform, utmSource);
154
+
155
+ if (utmSource) {
158
156
  affiliateChannel =
159
157
  revenueChannel === 'affiliate' ? affiliateChannel || leadChannel : undefined;
160
158
  offlineChannel =
@@ -163,11 +161,13 @@ export const initSourceDetails = async ({
163
161
  revenueChannel === 'organic' ? organicChannel || leadChannel : undefined;
164
162
  performanceChannel =
165
163
  revenueChannel === 'performance' ? performanceChannel || leadChannel : undefined;
164
+ }
166
165
 
166
+ if (!isSourcePresent()) {
167
167
  const sourceDetails: SourceDetailsCookie = {
168
168
  platform_channel: platform,
169
169
  experiment_dict: experiments,
170
- utm_params: getUTMParams(),
170
+ utm_params: utmParams,
171
171
  flow: flow || getFlow(platform, experiments),
172
172
  affiliate_channel: affiliateChannel,
173
173
  offline_channel: offlineChannel,
@@ -178,6 +178,25 @@ export const initSourceDetails = async ({
178
178
  };
179
179
 
180
180
  writeSourceCookie(sourceDetails);
181
+ } else {
182
+ let updatedSource: Partial<SourceDetailsCookie> = {
183
+ experiment_dict: experiments,
184
+ flow: flow || getFlow(platform, experiments),
185
+ };
186
+
187
+ if (utmSource) {
188
+ updatedSource = {
189
+ ...updatedSource,
190
+ utm_params: utmParams,
191
+ affiliate_channel: affiliateChannel,
192
+ offline_channel: offlineChannel,
193
+ organic_channel: organicChannel,
194
+ performance_channel: performanceChannel,
195
+ revenue_channel: revenueChannel,
196
+ };
197
+ }
198
+
199
+ updateSourceCookie(updatedSource);
181
200
  }
182
201
  };
183
202
 
package/src/index.ts CHANGED
@@ -1,2 +1,8 @@
1
1
  export * from './cookie';
2
+ export * from './constants';
3
+ export * from './local-storage';
4
+ export * from './date-time-helper';
5
+ export * from './object';
2
6
  export * from './growth-source';
7
+ export * from './url';
8
+ export * from './e-cna';
@@ -0,0 +1,196 @@
1
+ import { TWENTY_FOUR_HOURS_IN_SECONDS } from '../constants/date-time';
2
+
3
+ /* eslint-disable @typescript-eslint/no-explicit-any */
4
+ export const LOCAL_STORAGE_PRIMARY_KEY = 'CUEMATH_WEB';
5
+
6
+ export const checkLocalStorageSupport = (): boolean => {
7
+ try {
8
+ return 'localStorage' in window && window.localStorage !== null;
9
+ } catch (e) {
10
+ return false;
11
+ }
12
+ };
13
+
14
+ export const removeKeyFromObject = (
15
+ obj: { [x: string]: any },
16
+ keyToRemove: string,
17
+ ): null | { [x: string]: any } => {
18
+ if (obj) {
19
+ delete obj[keyToRemove];
20
+
21
+ return obj;
22
+ }
23
+
24
+ return null;
25
+ };
26
+
27
+ // Default expiry set to 24 hours
28
+ export const localStorageSet = (
29
+ key: string | number,
30
+ value: any,
31
+ ttl = TWENTY_FOUR_HOURS_IN_SECONDS,
32
+ ): boolean => {
33
+ if (checkLocalStorageSupport()) {
34
+ try {
35
+ let dbData: any = window.localStorage.getItem(LOCAL_STORAGE_PRIMARY_KEY);
36
+ const now = new Date();
37
+
38
+ const item = {
39
+ value,
40
+ expiry: now.getTime() + ttl,
41
+ };
42
+
43
+ if (dbData) {
44
+ dbData = JSON.parse(dbData);
45
+ dbData[key] = item;
46
+ } else {
47
+ dbData = { [key]: item };
48
+ }
49
+
50
+ window.localStorage.setItem(LOCAL_STORAGE_PRIMARY_KEY, JSON.stringify(dbData));
51
+
52
+ return true;
53
+ } catch (e) {
54
+ return false;
55
+ } // case when localStorage space quota is exceeded
56
+ } else {
57
+ return false;
58
+ }
59
+ };
60
+
61
+ export const localStorageGet = (key: string): null | string => {
62
+ if (checkLocalStorageSupport()) {
63
+ try {
64
+ const primaryLocalStorageValue = window.localStorage.getItem(
65
+ LOCAL_STORAGE_PRIMARY_KEY,
66
+ );
67
+ let dbData =
68
+ primaryLocalStorageValue != null
69
+ ? JSON.parse(primaryLocalStorageValue)
70
+ : primaryLocalStorageValue;
71
+ const requestedData = dbData[key];
72
+ const now = new Date();
73
+
74
+ if (now.getTime() > requestedData.expiry) {
75
+ dbData = removeKeyFromObject(dbData, key);
76
+ window.localStorage.setItem(LOCAL_STORAGE_PRIMARY_KEY, JSON.stringify(dbData));
77
+
78
+ return null;
79
+ }
80
+
81
+ return requestedData.value;
82
+ } catch (e) {
83
+ return null;
84
+ }
85
+ } else {
86
+ return null;
87
+ }
88
+ };
89
+
90
+ export const localStorageGetAll = (): null | string => {
91
+ if (checkLocalStorageSupport()) {
92
+ try {
93
+ const primaryLocalStorageValue = window.localStorage.getItem(
94
+ LOCAL_STORAGE_PRIMARY_KEY,
95
+ );
96
+
97
+ return primaryLocalStorageValue != null
98
+ ? JSON.parse(primaryLocalStorageValue)
99
+ : primaryLocalStorageValue;
100
+ } catch (e) {
101
+ return null;
102
+ }
103
+ } else {
104
+ return null;
105
+ }
106
+ };
107
+
108
+ export const localStorageRemove = (key: string): boolean => {
109
+ if (checkLocalStorageSupport()) {
110
+ try {
111
+ const primaryLocalStorageValue = window.localStorage.getItem(
112
+ LOCAL_STORAGE_PRIMARY_KEY,
113
+ );
114
+ let dbData =
115
+ primaryLocalStorageValue != null
116
+ ? JSON.parse(primaryLocalStorageValue)
117
+ : primaryLocalStorageValue;
118
+
119
+ if (dbData[key]) {
120
+ dbData = removeKeyFromObject(dbData, key);
121
+ window.localStorage.setItem(LOCAL_STORAGE_PRIMARY_KEY, JSON.stringify(dbData));
122
+
123
+ return true;
124
+ }
125
+
126
+ return false;
127
+ } catch (e) {
128
+ return false;
129
+ }
130
+ } else {
131
+ return false;
132
+ }
133
+ };
134
+
135
+ export const localStorageRemoveAll = (): boolean => {
136
+ if (checkLocalStorageSupport()) {
137
+ try {
138
+ const primaryLocalStorageValue = window.localStorage.getItem(
139
+ LOCAL_STORAGE_PRIMARY_KEY,
140
+ );
141
+ const dbData =
142
+ primaryLocalStorageValue != null
143
+ ? JSON.parse(primaryLocalStorageValue)
144
+ : primaryLocalStorageValue;
145
+
146
+ if (dbData) {
147
+ window.localStorage.removeItem(LOCAL_STORAGE_PRIMARY_KEY);
148
+
149
+ return true;
150
+ }
151
+
152
+ return false;
153
+ } catch (e) {
154
+ return false;
155
+ }
156
+ } else {
157
+ return false;
158
+ }
159
+ };
160
+
161
+ export const removeExpiredKeysOnMount = (): boolean => {
162
+ if (checkLocalStorageSupport()) {
163
+ try {
164
+ const primaryLocalStorageValue = window.localStorage.getItem(
165
+ LOCAL_STORAGE_PRIMARY_KEY,
166
+ );
167
+ let dbData =
168
+ primaryLocalStorageValue != null
169
+ ? JSON.parse(primaryLocalStorageValue)
170
+ : primaryLocalStorageValue;
171
+ const now = new Date();
172
+
173
+ if (dbData) {
174
+ dbData = Object.keys(dbData).reduce((accumulator: any, key) => {
175
+ if (now.getTime() < dbData[key].expiry) {
176
+ accumulator[key] = dbData[key];
177
+ }
178
+
179
+ return accumulator;
180
+ }, {});
181
+ }
182
+
183
+ if (Object.keys(dbData).length) {
184
+ window.localStorage.setItem(LOCAL_STORAGE_PRIMARY_KEY, JSON.stringify(dbData));
185
+ } else {
186
+ localStorageRemoveAll();
187
+ }
188
+
189
+ return true;
190
+ } catch (e) {
191
+ return false;
192
+ }
193
+ } else {
194
+ return false;
195
+ }
196
+ };
@@ -0,0 +1,21 @@
1
+ export function isEmpty(obj: object): boolean {
2
+ return Object.keys(obj).length === 0;
3
+ }
4
+
5
+ export function nestedMapsAreEqual(map1: Map<any, any>, map2: Map<any, any>): boolean {
6
+ // First, check if the maps have the same size
7
+ if (map1.size !== map2.size) {
8
+ return false;
9
+ }
10
+
11
+ // Then, iterate over each key-value pair in map1 and check if the corresponding key-value pair exists in map2
12
+ for (const [key, value] of map1) {
13
+ if (!map2.has(key) || map2.get(key) !== value) {
14
+ // If the key doesn't exist in map2, or the value for the key is different, the maps are not equal
15
+ return false;
16
+ }
17
+ }
18
+
19
+ // If we haven't returned false by now, the maps are equal
20
+ return true;
21
+ }