@modernman00/shared-js-lib 1.2.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/Cookie.js ADDED
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Sets a cookie with the given name, value, and number of days until it expires.
3
+ * By default, the cookie will expire in 365 days.
4
+ * @param {string} cname - The name of the cookie.
5
+ * @param {string} cvalue - The value of the cookie.
6
+ * @param {number} [exdays=365] - The number of days until the cookie expires.
7
+ */
8
+ export const setCookie = (cname, cvalue, exdays = 365) => {
9
+ const d = new Date(Date.now() + exdays * 864e5);
10
+ document.cookie = `${cname}=${cvalue}; expires=${d.toUTCString()}; path=/`;
11
+ }
12
+
13
+ /**
14
+ * Retrieves the value of a cookie by name.
15
+ * @param {string} cname - The name of the cookie to retrieve.
16
+ * @returns {string} The value of the cookie, or an empty string if the cookie does not exist.
17
+ */
18
+ export const getCookie = (cname) => {
19
+ const name = cname + "=";
20
+ const cookies = document.cookie.split(';').map(cookie => cookie.trim());
21
+ for (const cookie of cookies) {
22
+ if (cookie.startsWith(name)) {
23
+ return cookie.substring(name.length);
24
+ }
25
+ }
26
+ return "";
27
+ }
28
+
29
+
30
+
31
+ /**
32
+ * Checks if a cookie with the given name exists. If not, prompts the user
33
+ * for a name and sets the cookie for 365 days. If the cookie does exist,
34
+ * alerts the user with a welcome message.
35
+ * @param {string} name - The name of the cookie to check.
36
+ */
37
+ export const checkCookie = (name) => {
38
+ let user = getCookie(name);
39
+ if (!user) {
40
+ const askName = prompt("Please enter your name:", "");
41
+ if (askName) {
42
+ setCookie(name, askName, 365);
43
+ user = askName;
44
+ }
45
+ }
46
+ if (user) {
47
+ alert(`Welcome again ${user}`);
48
+ }
49
+ }
package/CountryCode.js ADDED
@@ -0,0 +1,214 @@
1
+ import {id} from "./UtilityHtml.js";
2
+
3
+ // inject the country code once one of the country is picked
4
+
5
+ // Object to map countries to country codes
6
+
7
+ const countryCodes = {
8
+ Afghanistan: "93",
9
+ Albania: "355",
10
+ Algeria: "213",
11
+ Andorra: "376",
12
+ Angola: "244",
13
+ "Antigua and Barbuda": "1-268",
14
+ Argentina: "54",
15
+ Armenia: "374",
16
+ Australia: "61",
17
+ Austria: "43",
18
+ Azerbaijan: "994",
19
+ Bahamas: "1-242",
20
+ Bahrain: "973",
21
+ Bangladesh: "880",
22
+ Barbados: "1-246",
23
+ Belarus: "375",
24
+ Belgium: "32",
25
+ Belize: "501",
26
+ Benin: "229",
27
+ Bhutan: "975",
28
+ Bolivia: "591",
29
+ "Bosnia and Herzegovina": "387",
30
+ Botswana: "267",
31
+ Brazil: "55",
32
+ Brunei: "673",
33
+ Bulgaria: "359",
34
+ "Burkina Faso": "226",
35
+ Burundi: "257",
36
+ Cambodia: "855",
37
+ Cameroon: "237",
38
+ Canada: "1",
39
+ "Cape Verde": "238",
40
+ "Central African Republic": "236",
41
+ Chad: "235",
42
+ Chile: "56",
43
+ China: "86",
44
+ Colombia: "57",
45
+ Comoros: "269",
46
+ "Congo (Brazzaville)": "242",
47
+ "Congo (Kinshasa)": "243",
48
+ "Costa Rica": "506",
49
+ "Côte d'Ivoire": "225",
50
+ Croatia: "385",
51
+ Cuba: "53",
52
+ Cyprus: "357",
53
+ "Czech Republic": "420",
54
+ Denmark: "45",
55
+ Djibouti: "253",
56
+ Dominica: "1-767",
57
+ "Dominican Republic": "1-809, 1-829, 1-849",
58
+ "East Timor": "670",
59
+ Ecuador: "593",
60
+ Egypt: "20",
61
+ "El Salvador": "503",
62
+ "Equatorial Guinea": "240",
63
+ Eritrea: "291",
64
+ Estonia: "372",
65
+ Ethiopia: "251",
66
+ Fiji: "679",
67
+ Finland: "358",
68
+ France: "33",
69
+ Gabon: "241",
70
+ Gambia: "220",
71
+ Georgia: "995",
72
+ Germany: "49",
73
+ Ghana: "233",
74
+ Greece: "30",
75
+ Grenada: "1-473",
76
+ Guatemala: "502",
77
+ Guinea: "224",
78
+ "Guinea-Bissau": "245",
79
+ Guyana: "592",
80
+ Haiti: "509",
81
+ Honduras: "504",
82
+ Hungary: "36",
83
+ Iceland: "354",
84
+ India: "91",
85
+ Indonesia: "62",
86
+ Iran: "98",
87
+ Iraq: "964",
88
+ Ireland: "353",
89
+ Israel: "972",
90
+ Italy: "39",
91
+ Jamaica: "1-876",
92
+ Japan: "81",
93
+ Jordan: "962",
94
+ Kazakhstan: "7",
95
+ Kenya: "254",
96
+ Kiribati: "686",
97
+ "North Korea": "850",
98
+ "South Korea": "82",
99
+ Kosovo: "383",
100
+ Kuwait: "965",
101
+ Kyrgyzstan: "996",
102
+ Laos: "856",
103
+ Latvia: "371",
104
+ Lebanon: "961",
105
+ Lesotho: "266",
106
+ Liberia: "231",
107
+ Libya: "218",
108
+ Liechtenstein: "423",
109
+ Lithuania: "370",
110
+ Luxembourg: "352",
111
+ Macedonia: "389",
112
+ Madagascar: "261",
113
+ Malawi: "265",
114
+ Malaysia: "60",
115
+ Maldives: "960",
116
+ Mali: "223",
117
+ Malta: "356",
118
+ "Marshall Islands": "692",
119
+ Mauritania: "222",
120
+ Mauritius: "230",
121
+ Mexico: "52",
122
+ Micronesia: "691",
123
+ Moldova: "373",
124
+ Monaco: "377",
125
+ Mongolia: "976",
126
+ Montenegro: "382",
127
+ Morocco: "212",
128
+ Mozambique: "258",
129
+ Myanmar: "95",
130
+ Namibia: "264",
131
+ Nauru: "674",
132
+ Nepal: "977",
133
+ Netherlands: "31",
134
+ "New Zealand": "64",
135
+ Nicaragua: "505",
136
+ Niger: "227",
137
+ Nigeria: "234",
138
+ Norway: "47",
139
+ Oman: "968",
140
+ Pakistan: "92",
141
+ Palau: "680",
142
+ Panama: "507",
143
+ "Papua New Guinea": "675",
144
+ Paraguay: "595",
145
+ Peru: "51",
146
+ Philippines: "63",
147
+ Poland: "48",
148
+ Portugal: "351",
149
+ Qatar: "974",
150
+ Romania: "40",
151
+ Russia: "7",
152
+ Rwanda: "250",
153
+ "Saint Kitts and Nevis": "1-869",
154
+ "Saint Lucia": "1-758",
155
+ "Saint Vincent and the Grenadines": "1-784",
156
+ Samoa: "685",
157
+ "San Marino": "378",
158
+ "Sao Tome and Principe": "239",
159
+ "Saudi Arabia": "966",
160
+ Senegal: "221",
161
+ Serbia: "381",
162
+ Seychelles: "248",
163
+ "Sierra Leone": "232",
164
+ Singapore: "65",
165
+ Slovakia: "421",
166
+ Slovenia: "386",
167
+ "Solomon Islands": "677",
168
+ Somalia: "252",
169
+ "South Africa": "27",
170
+ "South Sudan": "211",
171
+ Spain: "34",
172
+ "Sri Lanka": "94",
173
+ Sudan: "249",
174
+ Suriname: "597",
175
+ Swaziland: "268",
176
+ Sweden: "46",
177
+ Switzerland: "41",
178
+ Syria: "963",
179
+ Taiwan: "886",
180
+ Tajikistan: "992",
181
+ Tanzania: "255",
182
+ Thailand: "66",
183
+ Togo: "228",
184
+ Tonga: "676",
185
+ "Trinidad and Tobago": "1-868",
186
+ Tunisia: "216",
187
+ Turkey: "90",
188
+ Turkmenistan: "993",
189
+ Tuvalu: "688",
190
+ Uganda: "256",
191
+ Ukraine: "380",
192
+ "United Arab Emirates": "971",
193
+ "United Kingdom": "44",
194
+ "United States": "1",
195
+ Uruguay: "598",
196
+ Uzbekistan: "998",
197
+ Vanuatu: "678",
198
+ "Vatican City": "379",
199
+ Venezuela: "58",
200
+ Vietnam: "84",
201
+ Yemen: "967",
202
+ Zambia: "260",
203
+ Zimbabwe: "263"
204
+ }
205
+
206
+
207
+ export const injectCountryCode = async (countryId, mobileId) => {
208
+ id(countryId).addEventListener('change', async (e) => {
209
+ const value = e.target.value;
210
+ id(mobileId).value = await countryCodes[value] || '';
211
+ });
212
+ };
213
+
214
+ injectCountryCode();
package/DateTime.js ADDED
@@ -0,0 +1,101 @@
1
+ /**
2
+ * Returns a string representing the time elapsed since the given date.
3
+ * The string is in the format "[number] [unit]s", where [number] is the number of the unit,
4
+ * and [unit] is the name of the unit. The unit is the largest unit that is at least one,
5
+ * for example "3 days" or "1 year". If the time elapsed is less than one second, returns "0 seconds".
6
+ * @param {Date} date - The date to calculate the time elapsed since.
7
+ * @returns {string} - The time elapsed since the given date.
8
+ */
9
+ export const calTiming = (date) => {
10
+ const seconds = Math.floor((new Date() - date) / 1000);
11
+ const epochs = [
12
+ { name: "year", seconds: 31536000 },
13
+ { name: "month", seconds: 2592000 },
14
+ { name: "day", seconds: 86400 },
15
+ { name: "hour", seconds: 3600 },
16
+ { name: "minute", seconds: 60 },
17
+ { name: "second", seconds: 1 }
18
+ ];
19
+
20
+ for (const epoch of epochs) {
21
+ const interval = Math.floor(seconds / epoch.seconds);
22
+ if (interval > 1) {
23
+ return `${interval} ${epoch.name}s`;
24
+ } else if (interval === 1) {
25
+ return `1 ${epoch.name}`;
26
+ }
27
+ }
28
+ };
29
+ const aDay = 24 * 60 * 60 * 1000;
30
+
31
+
32
+ let element = document.querySelectorAll('time[data-time]')
33
+
34
+ /**
35
+ * Updates all elements with a 'data-time' attribute with the time that has
36
+ * elapsed since the given time, in real time.
37
+ *
38
+ * The function works by looping through all elements with the 'data-time'
39
+ * attribute and calculating the time elapsed since the given time. It then
40
+ * updates the content of the element with the time that has elapsed.
41
+ *
42
+ * The function then waits 60 seconds before calling itself again.
43
+ *
44
+ * @returns {void}
45
+ */
46
+ export const updateTimeRealTime = () => {
47
+ for (const entry of element) {
48
+ const seconds = (Date.now() - new Date(entry.dataset.time).getTime()) / 1000;
49
+ entry.textContent = getDuration(seconds);
50
+ }
51
+ setTimeout(updateTimeRealTime, 1000 * 60);
52
+ };
53
+ setTimeout(updateTimeRealTime, 1000 * 60);
54
+
55
+
56
+ const epochs = [
57
+ ['year', 31536000],
58
+ ['month', 2592000],
59
+ ['day', 86400],
60
+ ['hour', 3600],
61
+ ['minute', 60],
62
+ ['second', 1]
63
+ ];
64
+
65
+ /**
66
+ * Takes a time in seconds and returns an object with the largest unit of time
67
+ * that is at least one, and the number of those units.
68
+ *
69
+ * @param {number} timeAgoInSeconds - The time in seconds to calculate the duration of.
70
+ * @returns {Object} - An object with an interval and epoch, where interval is the number of those units,
71
+ * and epoch is the name of the unit.
72
+ */
73
+ const getDuration = (timeAgoInSeconds) => {
74
+ for (let [name, seconds] of epochs) {
75
+ const interval = Math.floor(timeAgoInSeconds / seconds);
76
+ if (interval >= 1) {
77
+ return {
78
+ interval: interval,
79
+ epoch: name
80
+ };
81
+ }
82
+ }
83
+ };
84
+
85
+ /**
86
+ * Returns a string representing the time elapsed since the given date.
87
+ * The time elapsed is presented as a string in the form of "X epochs ago",
88
+ * where X is the number of epochs, and epochs is the largest unit of time that
89
+ * is at least one, e.g. "1 hour ago", "2 days ago", etc.
90
+ *
91
+ * @param {Date|string} date - The date from which to calculate the time elapsed.
92
+ * @returns {string} - A string representing the time elapsed since the given date.
93
+ */
94
+ export const timeAgo = (date) => {
95
+ const timeAgoInSeconds = Math.floor((new Date() - new Date(date)) / 1000);
96
+ const { interval, epoch } = getDuration(timeAgoInSeconds);
97
+ const suffix = interval === 1 ? '' : 's';
98
+ return `${interval} ${epoch}${suffix} ago`;
99
+ };
100
+
101
+ export const date2String = (date) => new Date().toDateString(date)
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ import { id } from './UtilityHtml.js'
3
+ import { postFormData } from "./Http.js"
4
+ import { emailVal } from "./Utility.js"
5
+ import { showError } from "./ShowResponse.js";
6
+
7
+ // Environment detection
8
+ const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';
9
+
10
+ // Log warning for browser-only features
11
+ const warnNodeEnvironment = (feature) => {
12
+ if (!isBrowser) {
13
+ console.warn(`Warning: ${feature} is only available in browser environments. This code may not work as expected in Node.js.`);
14
+ return false;
15
+ }
16
+ return true;
17
+ };
18
+
19
+ // Initialize UI - only in browser environment
20
+ if (isBrowser) {
21
+ const loaderElement = id("setLoader");
22
+ if (loaderElement) {
23
+ loaderElement.style.display = "none";
24
+ } else {
25
+ console.warn("Element 'setLoader' not found in the DOM");
26
+ }
27
+ } else {
28
+ console.warn("Running in Node.js environment. DOM manipulation is not available.");
29
+ }
30
+
31
+ /**
32
+ * @description Handles the forgot password submission process.
33
+ * @param {string} formId - The ID of the form to submit.
34
+ * @param {string} url - The URL to make the POST request to.
35
+ * @param {string} redirectUrl - The URL to redirect the user to after the submission is complete.
36
+ * @returns {function} - A function that handles the submission process.
37
+ */
38
+ export const forgotPasswordSubmission = (formId, url, redirectUrl) => {
39
+ // Check if we're in a browser environment before creating the helper
40
+ if (!warnNodeEnvironment('forgotPasswordSubmission')) {
41
+ // Return a no-op function in Node.js environment
42
+ return (e) => {
43
+ if (e && typeof e.preventDefault === 'function') {
44
+ e.preventDefault();
45
+ }
46
+ console.warn("Forgot password functionality is only available in browser environments");
47
+ };
48
+ }
49
+
50
+ const emailInput = id('email_id');
51
+ if (!emailInput) {
52
+ console.warn("Email input element not found");
53
+ return (e) => {
54
+ if (e && typeof e.preventDefault === 'function') {
55
+ e.preventDefault();
56
+ }
57
+ console.error("Email input element not found");
58
+ };
59
+ }
60
+
61
+ /**
62
+ * @description Handles the submission of the forgot password form.
63
+ * @param {Event} e - The event object passed by the event listener.
64
+ * @returns {void}
65
+ * @throws {Error} - If there is an error with the submission
66
+ */
67
+ const forgotPasswordSubmissionHelper = (e) => {
68
+ try {
69
+ if (e && typeof e.preventDefault === 'function') {
70
+ e.preventDefault();
71
+ }
72
+
73
+ // Ensure emailInput still exists in the DOM
74
+ if (!emailInput) {
75
+ throw new Error("Email input element not found");
76
+ }
77
+
78
+ const email = emailInput.value.trim();
79
+ if (!emailVal(email)) {
80
+ const loaderElement = id("setLoader");
81
+ if (loaderElement) {
82
+ loaderElement.style.display = "block";
83
+ }
84
+
85
+ // Only use localStorage in browser environment
86
+ if (typeof localStorage !== 'undefined') {
87
+ localStorage.setItem('redirect', redirectUrl);
88
+ }
89
+
90
+ postFormData(url, formId, redirectUrl);
91
+ }
92
+ } catch (error) {
93
+ if (typeof showError === 'function') {
94
+ showError(error);
95
+ } else {
96
+ console.error("Error in forgot password submission:", error);
97
+ }
98
+ }
99
+ };
100
+
101
+ return forgotPasswordSubmissionHelper;
102
+ };