@cuemath/web-utils 1.0.0 → 1.0.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.
Files changed (50) 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/cookie/index.d.ts +1 -1
  21. package/dist/cookie/index.js.map +1 -1
  22. package/dist/date-time-helper/index.d.ts +30 -0
  23. package/dist/date-time-helper/index.js +107 -0
  24. package/dist/date-time-helper/index.js.map +1 -0
  25. package/dist/growth-source/index.d.ts +2 -2
  26. package/dist/growth-source/index.js +11 -11
  27. package/dist/growth-source/index.js.map +1 -1
  28. package/dist/growth-source/types.d.ts +15 -1
  29. package/dist/index.d.ts +4 -0
  30. package/dist/index.js +4 -0
  31. package/dist/index.js.map +1 -1
  32. package/dist/local-storage/index.d.ts +13 -0
  33. package/dist/local-storage/index.js +173 -0
  34. package/dist/local-storage/index.js.map +1 -0
  35. package/dist/object/index.d.ts +7 -0
  36. package/dist/object/index.js +40 -0
  37. package/dist/object/index.js.map +1 -0
  38. package/package.json +9 -5
  39. package/src/constants/country/index.ts +1 -0
  40. package/src/constants/country/iso-country-code.ts +248 -0
  41. package/src/constants/date-time/index.ts +43 -0
  42. package/src/constants/date-time/international-timezones.ts +157 -0
  43. package/src/constants/index.ts +1 -0
  44. package/src/cookie/index.ts +1 -1
  45. package/src/date-time-helper/index.ts +139 -0
  46. package/src/growth-source/index.ts +18 -17
  47. package/src/growth-source/types.ts +16 -1
  48. package/src/index.ts +4 -0
  49. package/src/local-storage/index.ts +196 -0
  50. package/src/object/index.ts +43 -0
@@ -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
+ };
@@ -1,5 +1,6 @@
1
1
  import type {
2
2
  SourceDetails,
3
+ SourceDetailsCookie,
3
4
  Platform,
4
5
  SignupFlow,
5
6
  UTMParams,
@@ -18,13 +19,13 @@ const UTM_PARAMS: UTMParamsKeys[] = [
18
19
  'utm_term',
19
20
  ];
20
21
 
21
- export const readSourceCookie = (): SourceDetails => {
22
+ export const readSourceCookie = (): SourceDetailsCookie => {
22
23
  const sourceDetails = getCookie(SOURCE_DETAILS_COOKIE);
23
24
 
24
25
  return sourceDetails ? JSON.parse(decodeURIComponent(sourceDetails)) : undefined;
25
26
  };
26
27
 
27
- const writeSourceCookie = (sourceDetails: SourceDetails): void => {
28
+ const writeSourceCookie = (sourceDetails: SourceDetailsCookie): void => {
28
29
  createCookie(SOURCE_DETAILS_COOKIE, JSON.stringify(sourceDetails), 365, true);
29
30
  };
30
31
 
@@ -32,11 +33,11 @@ export const isSourcePresent = (): boolean => {
32
33
  return !!getCookie(SOURCE_DETAILS_COOKIE);
33
34
  };
34
35
 
35
- const updateSourceCookie = (sourceDetail: Partial<SourceDetails>): void => {
36
+ const updateSourceCookie = (sourceDetail: Partial<SourceDetailsCookie>): void => {
36
37
  if (!isSourcePresent) return;
37
38
 
38
- const sourceDetails: SourceDetails = readSourceCookie();
39
- const updatedDetails: SourceDetails = {
39
+ const sourceDetails: SourceDetailsCookie = readSourceCookie();
40
+ const updatedDetails: SourceDetailsCookie = {
40
41
  ...sourceDetails,
41
42
  ...sourceDetail,
42
43
  };
@@ -163,17 +164,17 @@ export const initSourceDetails = async ({
163
164
  performanceChannel =
164
165
  revenueChannel === 'performance' ? performanceChannel || leadChannel : undefined;
165
166
 
166
- const sourceDetails: SourceDetails = {
167
- platform,
168
- experiments,
169
- utmParams: getUTMParams(),
167
+ const sourceDetails: SourceDetailsCookie = {
168
+ platform_channel: platform,
169
+ experiment_dict: experiments,
170
+ utm_params: getUTMParams(),
170
171
  flow: flow || getFlow(platform, experiments),
171
- affiliateChannel,
172
- offlineChannel,
173
- organicChannel,
174
- performanceChannel,
175
- revenueChannel: revenueChannel,
176
- firstPage: getFirstPage(),
172
+ affiliate_channel: affiliateChannel,
173
+ offline_channel: offlineChannel,
174
+ organic_channel: organicChannel,
175
+ performance_channel: performanceChannel,
176
+ revenue_channel: revenueChannel,
177
+ first_page: getFirstPage(),
177
178
  };
178
179
 
179
180
  writeSourceCookie(sourceDetails);
@@ -182,10 +183,10 @@ export const initSourceDetails = async ({
182
183
 
183
184
  // Last Page which user visited signup before
184
185
  export const setLastPage = async (pagePath: string): Promise<void> => {
185
- updateSourceCookie({ lastPage: pagePath });
186
+ updateSourceCookie({ last_page: pagePath });
186
187
  };
187
188
 
188
189
  // set Entry point where user clicked to Signup
189
190
  export const setFlowEntryPoint = async (flowEntry: string): Promise<void> => {
190
- updateSourceCookie({ flowEntry });
191
+ updateSourceCookie({ flow_entry: flowEntry });
191
192
  };
@@ -17,9 +17,24 @@ export type UTMParams = {
17
17
 
18
18
  export type UTMParamsKeys = keyof UTMParams;
19
19
 
20
+ export type SourceDetailsCookie = {
21
+ platform_channel: Platform;
22
+ experiment_dict?: Experiments;
23
+ flow?: SignupFlow;
24
+ utm_params?: UTMParams;
25
+ affiliate_channel?: string;
26
+ offline_channel?: string;
27
+ performance_channel?: string;
28
+ revenue_channel: RevenueChannel;
29
+ organic_channel?: string;
30
+ flow_entry?: string;
31
+ first_page?: string;
32
+ last_page?: string;
33
+ };
34
+
20
35
  export type SourceDetails = {
21
36
  platform: Platform;
22
- experiments?: Experiments;
37
+ experiments: Experiments;
23
38
  flow?: SignupFlow;
24
39
  utmParams?: UTMParams;
25
40
  affiliateChannel?: string;
package/src/index.ts CHANGED
@@ -1,2 +1,6 @@
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';
@@ -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,43 @@
1
+ export function isEmpty(obj: object): boolean {
2
+ return Object.keys(obj).length === 0;
3
+ }
4
+
5
+ // In this case both object have same key names Don't use to compare objects
6
+ export function isMapEqual(
7
+ map1: { [x: string]: any },
8
+ map2: { [x: string]: any },
9
+ ): boolean {
10
+ const map1Keys = Object.keys(map1);
11
+ const map2Keys = Object.keys(map2);
12
+
13
+ if (map1Keys.length !== map2Keys.length) {
14
+ return false;
15
+ }
16
+ for (let i = 0; i < map1Keys.length; i += 1) {
17
+ const key = map1Keys[i];
18
+
19
+ if (map1[key] !== map2[key]) {
20
+ return false;
21
+ }
22
+ }
23
+
24
+ return true;
25
+ }
26
+
27
+ export function nestedMapsAreEqual(map1: Map<any, any>, map2: Map<any, any>): boolean {
28
+ // First, check if the maps have the same size
29
+ if (map1.size !== map2.size) {
30
+ return false;
31
+ }
32
+
33
+ // Then, iterate over each key-value pair in map1 and check if the corresponding key-value pair exists in map2
34
+ for (const [key, value] of map1) {
35
+ if (!map2.has(key) || map2.get(key) !== value) {
36
+ // If the key doesn't exist in map2, or the value for the key is different, the maps are not equal
37
+ return false;
38
+ }
39
+ }
40
+
41
+ // If we haven't returned false by now, the maps are equal
42
+ return true;
43
+ }