@bikiran/utils 2.2.9 → 2.3.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.
package/dist/index.d.ts CHANGED
@@ -27,3 +27,7 @@ export { showCurrencySign, showInt } from "./lib/utils/show";
27
27
  export { addOption, addOption2 } from "./lib/utils/option";
28
28
  export { getBikiranUrl, getAccountUrl, getApiUrl, getApi2Url, getApi3Url, getBaseDomain, getSupportUrl, generateAnyUrl, getAdManageUrl, getAppoceanUrl, getDBConnectionString, getDocsUrl, getDomainUrl, getHostingUrl, getSiteUrl, SUB_DOMAIN_NAMES, isDev, getMode, } from "./lib/utils/Env";
29
29
  export type { TInputChangeEvent, TTextAreaChangeEvent, TMouseEvent, TFormEvent, TKeyboardEvent, TFocusEvent, TDragEvent, TState, } from "./lib/types/GlobalType";
30
+ export { default as country } from "./lib/utils/country.json";
31
+ export { win, doc, storage } from "./lib/utils/dom";
32
+ export { setLocalStorage, getLocalStorage } from "./lib/utils/storage";
33
+ export { mkQueryString, mkToken, mkStrongPassword, } from "./lib/utils/StringOperation";
package/dist/index.js CHANGED
@@ -26,3 +26,7 @@ export { convertToYears } from "./lib/utils/convertToYears";
26
26
  export { showCurrencySign, showInt } from "./lib/utils/show";
27
27
  export { addOption, addOption2 } from "./lib/utils/option";
28
28
  export { getBikiranUrl, getAccountUrl, getApiUrl, getApi2Url, getApi3Url, getBaseDomain, getSupportUrl, generateAnyUrl, getAdManageUrl, getAppoceanUrl, getDBConnectionString, getDocsUrl, getDomainUrl, getHostingUrl, getSiteUrl, SUB_DOMAIN_NAMES, isDev, getMode, } from "./lib/utils/Env";
29
+ export { default as country } from "./lib/utils/country.json";
30
+ export { win, doc, storage } from "./lib/utils/dom";
31
+ export { setLocalStorage, getLocalStorage } from "./lib/utils/storage";
32
+ export { mkQueryString, mkToken, mkStrongPassword, } from "./lib/utils/StringOperation";
package/dist/index.ts CHANGED
@@ -61,3 +61,11 @@ export type {
61
61
  TDragEvent,
62
62
  TState,
63
63
  } from "./lib/types/GlobalType";
64
+ export { default as country } from "./lib/utils/country.json";
65
+ export { win, doc, storage } from "./lib/utils/dom";
66
+ export { setLocalStorage, getLocalStorage } from "./lib/utils/storage";
67
+ export {
68
+ mkQueryString,
69
+ mkToken,
70
+ mkStrongPassword,
71
+ } from "./lib/utils/StringOperation";
@@ -0,0 +1,5 @@
1
+ export declare const mkQueryString: (obj: Record<string, any>) => string;
2
+ export declare function mkToken(length?: number): string;
3
+ export declare function mkStrongPassword(length: number): string;
4
+ declare const StringOperation: null;
5
+ export default StringOperation;
@@ -0,0 +1,36 @@
1
+ export const mkQueryString = (obj) => {
2
+ const str = Object.keys(obj)
3
+ .map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`)
4
+ .join("&");
5
+ return str;
6
+ };
7
+ export function mkToken(length = 32) {
8
+ let result = "";
9
+ const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
10
+ const charactersLength = characters.length;
11
+ for (let i = 0; i < length; i += 1) {
12
+ result += characters.charAt(Math.floor(Math.random() * charactersLength));
13
+ }
14
+ return result;
15
+ }
16
+ export function mkStrongPassword(length) {
17
+ function mkString(charset, l) {
18
+ let str = "";
19
+ for (let i = 0; i < l; i += 1) {
20
+ str += charset.charAt(Math.floor(Math.random() * charset.length));
21
+ }
22
+ return str;
23
+ }
24
+ const charset1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
25
+ const charset2 = "0123456789";
26
+ const charset3 = "!@#$%^&*";
27
+ const pass1 = mkString(charset1, 1);
28
+ const pass2 = mkString(charset1 + (charset1 || "").toLowerCase() + charset2 + charset3, length - 6);
29
+ const pass3 = mkString(charset2, 1);
30
+ const pass4 = mkString(charset3, 1);
31
+ const pass5 = mkString(charset2, 1);
32
+ const pass6 = mkString(charset1, 1);
33
+ return pass1 + pass2 + pass3 + pass4 + pass5 + pass6;
34
+ }
35
+ const StringOperation = null;
36
+ export default StringOperation;
@@ -0,0 +1,245 @@
1
+ [
2
+ { "name": "Afghanistan", "code": "AF" },
3
+ { "name": "Åland Islands", "code": "AX" },
4
+ { "name": "Albania", "code": "AL" },
5
+ { "name": "Algeria", "code": "DZ" },
6
+ { "name": "American Samoa", "code": "AS" },
7
+ { "name": "AndorrA", "code": "AD" },
8
+ { "name": "Angola", "code": "AO" },
9
+ { "name": "Anguilla", "code": "AI" },
10
+ { "name": "Antarctica", "code": "AQ" },
11
+ { "name": "Antigua and Barbuda", "code": "AG" },
12
+ { "name": "Argentina", "code": "AR" },
13
+ { "name": "Armenia", "code": "AM" },
14
+ { "name": "Aruba", "code": "AW" },
15
+ { "name": "Australia", "code": "AU" },
16
+ { "name": "Austria", "code": "AT" },
17
+ { "name": "Azerbaijan", "code": "AZ" },
18
+ { "name": "Bahamas", "code": "BS" },
19
+ { "name": "Bahrain", "code": "BH" },
20
+ { "name": "Bangladesh", "code": "BD" },
21
+ { "name": "Barbados", "code": "BB" },
22
+ { "name": "Belarus", "code": "BY" },
23
+ { "name": "Belgium", "code": "BE" },
24
+ { "name": "Belize", "code": "BZ" },
25
+ { "name": "Benin", "code": "BJ" },
26
+ { "name": "Bermuda", "code": "BM" },
27
+ { "name": "Bhutan", "code": "BT" },
28
+ { "name": "Bolivia", "code": "BO" },
29
+ { "name": "Bosnia and Herzegovina", "code": "BA" },
30
+ { "name": "Botswana", "code": "BW" },
31
+ { "name": "Bouvet Island", "code": "BV" },
32
+ { "name": "Brazil", "code": "BR" },
33
+ { "name": "British Indian Ocean Territory", "code": "IO" },
34
+ { "name": "Brunei Darussalam", "code": "BN" },
35
+ { "name": "Bulgaria", "code": "BG" },
36
+ { "name": "Burkina Faso", "code": "BF" },
37
+ { "name": "Burundi", "code": "BI" },
38
+ { "name": "Cambodia", "code": "KH" },
39
+ { "name": "Cameroon", "code": "CM" },
40
+ { "name": "Canada", "code": "CA" },
41
+ { "name": "Cape Verde", "code": "CV" },
42
+ { "name": "Cayman Islands", "code": "KY" },
43
+ { "name": "Central African Republic", "code": "CF" },
44
+ { "name": "Chad", "code": "TD" },
45
+ { "name": "Chile", "code": "CL" },
46
+ { "name": "China", "code": "CN" },
47
+ { "name": "Christmas Island", "code": "CX" },
48
+ { "name": "Cocos (Keeling) Islands", "code": "CC" },
49
+ { "name": "Colombia", "code": "CO" },
50
+ { "name": "Comoros", "code": "KM" },
51
+ { "name": "Congo", "code": "CG" },
52
+ { "name": "Congo, The Democratic Republic of the", "code": "CD" },
53
+ { "name": "Cook Islands", "code": "CK" },
54
+ { "name": "Costa Rica", "code": "CR" },
55
+ { "name": "Cote D'Ivoire", "code": "CI" },
56
+ { "name": "Croatia", "code": "HR" },
57
+ { "name": "Cuba", "code": "CU" },
58
+ { "name": "Cyprus", "code": "CY" },
59
+ { "name": "Czech Republic", "code": "CZ" },
60
+ { "name": "Denmark", "code": "DK" },
61
+ { "name": "Djibouti", "code": "DJ" },
62
+ { "name": "Dominica", "code": "DM" },
63
+ { "name": "Dominican Republic", "code": "DO" },
64
+ { "name": "Ecuador", "code": "EC" },
65
+ { "name": "Egypt", "code": "EG" },
66
+ { "name": "El Salvador", "code": "SV" },
67
+ { "name": "Equatorial Guinea", "code": "GQ" },
68
+ { "name": "Eritrea", "code": "ER" },
69
+ { "name": "Estonia", "code": "EE" },
70
+ { "name": "Ethiopia", "code": "ET" },
71
+ { "name": "Falkland Islands (Malvinas)", "code": "FK" },
72
+ { "name": "Faroe Islands", "code": "FO" },
73
+ { "name": "Fiji", "code": "FJ" },
74
+ { "name": "Finland", "code": "FI" },
75
+ { "name": "France", "code": "FR" },
76
+ { "name": "French Guiana", "code": "GF" },
77
+ { "name": "French Polynesia", "code": "PF" },
78
+ { "name": "French Southern Territories", "code": "TF" },
79
+ { "name": "Gabon", "code": "GA" },
80
+ { "name": "Gambia", "code": "GM" },
81
+ { "name": "Georgia", "code": "GE" },
82
+ { "name": "Germany", "code": "DE" },
83
+ { "name": "Ghana", "code": "GH" },
84
+ { "name": "Gibraltar", "code": "GI" },
85
+ { "name": "Greece", "code": "GR" },
86
+ { "name": "Greenland", "code": "GL" },
87
+ { "name": "Grenada", "code": "GD" },
88
+ { "name": "Guadeloupe", "code": "GP" },
89
+ { "name": "Guam", "code": "GU" },
90
+ { "name": "Guatemala", "code": "GT" },
91
+ { "name": "Guernsey", "code": "GG" },
92
+ { "name": "Guinea", "code": "GN" },
93
+ { "name": "Guinea-Bissau", "code": "GW" },
94
+ { "name": "Guyana", "code": "GY" },
95
+ { "name": "Haiti", "code": "HT" },
96
+ { "name": "Heard Island and Mcdonald Islands", "code": "HM" },
97
+ { "name": "Holy See (Vatican City State)", "code": "VA" },
98
+ { "name": "Honduras", "code": "HN" },
99
+ { "name": "Hong Kong", "code": "HK" },
100
+ { "name": "Hungary", "code": "HU" },
101
+ { "name": "Iceland", "code": "IS" },
102
+ { "name": "India", "code": "IN" },
103
+ { "name": "Indonesia", "code": "ID" },
104
+ { "name": "Iran, Islamic Republic Of", "code": "IR" },
105
+ { "name": "Iraq", "code": "IQ" },
106
+ { "name": "Ireland", "code": "IE" },
107
+ { "name": "Isle of Man", "code": "IM" },
108
+ { "name": "Israel", "code": "IL" },
109
+ { "name": "Italy", "code": "IT" },
110
+ { "name": "Jamaica", "code": "JM" },
111
+ { "name": "Japan", "code": "JP" },
112
+ { "name": "Jersey", "code": "JE" },
113
+ { "name": "Jordan", "code": "JO" },
114
+ { "name": "Kazakhstan", "code": "KZ" },
115
+ { "name": "Kenya", "code": "KE" },
116
+ { "name": "Kiribati", "code": "KI" },
117
+ { "name": "Korea, Democratic People'S Republic of", "code": "KP" },
118
+ { "name": "Korea, Republic of", "code": "KR" },
119
+ { "name": "Kuwait", "code": "KW" },
120
+ { "name": "Kyrgyzstan", "code": "KG" },
121
+ { "name": "Lao People'S Democratic Republic", "code": "LA" },
122
+ { "name": "Latvia", "code": "LV" },
123
+ { "name": "Lebanon", "code": "LB" },
124
+ { "name": "Lesotho", "code": "LS" },
125
+ { "name": "Liberia", "code": "LR" },
126
+ { "name": "Libyan Arab Jamahiriya", "code": "LY" },
127
+ { "name": "Liechtenstein", "code": "LI" },
128
+ { "name": "Lithuania", "code": "LT" },
129
+ { "name": "Luxembourg", "code": "LU" },
130
+ { "name": "Macao", "code": "MO" },
131
+ { "name": "Macedonia, The Former Yugoslav Republic of", "code": "MK" },
132
+ { "name": "Madagascar", "code": "MG" },
133
+ { "name": "Malawi", "code": "MW" },
134
+ { "name": "Malaysia", "code": "MY" },
135
+ { "name": "Maldives", "code": "MV" },
136
+ { "name": "Mali", "code": "ML" },
137
+ { "name": "Malta", "code": "MT" },
138
+ { "name": "Marshall Islands", "code": "MH" },
139
+ { "name": "Martinique", "code": "MQ" },
140
+ { "name": "Mauritania", "code": "MR" },
141
+ { "name": "Mauritius", "code": "MU" },
142
+ { "name": "Mayotte", "code": "YT" },
143
+ { "name": "Mexico", "code": "MX" },
144
+ { "name": "Micronesia, Federated States of", "code": "FM" },
145
+ { "name": "Moldova, Republic of", "code": "MD" },
146
+ { "name": "Monaco", "code": "MC" },
147
+ { "name": "Mongolia", "code": "MN" },
148
+ { "name": "Montserrat", "code": "MS" },
149
+ { "name": "Morocco", "code": "MA" },
150
+ { "name": "Mozambique", "code": "MZ" },
151
+ { "name": "Myanmar", "code": "MM" },
152
+ { "name": "Namibia", "code": "NA" },
153
+ { "name": "Nauru", "code": "NR" },
154
+ { "name": "Nepal", "code": "NP" },
155
+ { "name": "Netherlands", "code": "NL" },
156
+ { "name": "Netherlands Antilles", "code": "AN" },
157
+ { "name": "New Caledonia", "code": "NC" },
158
+ { "name": "New Zealand", "code": "NZ" },
159
+ { "name": "Nicaragua", "code": "NI" },
160
+ { "name": "Niger", "code": "NE" },
161
+ { "name": "Nigeria", "code": "NG" },
162
+ { "name": "Niue", "code": "NU" },
163
+ { "name": "Norfolk Island", "code": "NF" },
164
+ { "name": "Northern Mariana Islands", "code": "MP" },
165
+ { "name": "Norway", "code": "NO" },
166
+ { "name": "Oman", "code": "OM" },
167
+ { "name": "Pakistan", "code": "PK" },
168
+ { "name": "Palau", "code": "PW" },
169
+ { "name": "Palestinian Territory, Occupied", "code": "PS" },
170
+ { "name": "Panama", "code": "PA" },
171
+ { "name": "Papua New Guinea", "code": "PG" },
172
+ { "name": "Paraguay", "code": "PY" },
173
+ { "name": "Peru", "code": "PE" },
174
+ { "name": "Philippines", "code": "PH" },
175
+ { "name": "Pitcairn", "code": "PN" },
176
+ { "name": "Poland", "code": "PL" },
177
+ { "name": "Portugal", "code": "PT" },
178
+ { "name": "Puerto Rico", "code": "PR" },
179
+ { "name": "Qatar", "code": "QA" },
180
+ { "name": "Reunion", "code": "RE" },
181
+ { "name": "Romania", "code": "RO" },
182
+ { "name": "Russian Federation", "code": "RU" },
183
+ { "name": "RWANDA", "code": "RW" },
184
+ { "name": "Saint Helena", "code": "SH" },
185
+ { "name": "Saint Kitts and Nevis", "code": "KN" },
186
+ { "name": "Saint Lucia", "code": "LC" },
187
+ { "name": "Saint Pierre and Miquelon", "code": "PM" },
188
+ { "name": "Saint Vincent and the Grenadines", "code": "VC" },
189
+ { "name": "Samoa", "code": "WS" },
190
+ { "name": "San Marino", "code": "SM" },
191
+ { "name": "Sao Tome and Principe", "code": "ST" },
192
+ { "name": "Saudi Arabia", "code": "SA" },
193
+ { "name": "Senegal", "code": "SN" },
194
+ { "name": "Serbia and Montenegro", "code": "CS" },
195
+ { "name": "Seychelles", "code": "SC" },
196
+ { "name": "Sierra Leone", "code": "SL" },
197
+ { "name": "Singapore", "code": "SG" },
198
+ { "name": "Slovakia", "code": "SK" },
199
+ { "name": "Slovenia", "code": "SI" },
200
+ { "name": "Solomon Islands", "code": "SB" },
201
+ { "name": "Somalia", "code": "SO" },
202
+ { "name": "South Africa", "code": "ZA" },
203
+ { "name": "South Georgia and the South Sandwich Islands", "code": "GS" },
204
+ { "name": "Spain", "code": "ES" },
205
+ { "name": "Sri Lanka", "code": "LK" },
206
+ { "name": "Sudan", "code": "SD" },
207
+ { "name": "Suriname", "code": "SR" },
208
+ { "name": "Svalbard and Jan Mayen", "code": "SJ" },
209
+ { "name": "Swaziland", "code": "SZ" },
210
+ { "name": "Sweden", "code": "SE" },
211
+ { "name": "Switzerland", "code": "CH" },
212
+ { "name": "Syrian Arab Republic", "code": "SY" },
213
+ { "name": "Taiwan, Province of China", "code": "TW" },
214
+ { "name": "Tajikistan", "code": "TJ" },
215
+ { "name": "Tanzania, United Republic of", "code": "TZ" },
216
+ { "name": "Thailand", "code": "TH" },
217
+ { "name": "Timor-Leste", "code": "TL" },
218
+ { "name": "Togo", "code": "TG" },
219
+ { "name": "Tokelau", "code": "TK" },
220
+ { "name": "Tonga", "code": "TO" },
221
+ { "name": "Trinidad and Tobago", "code": "TT" },
222
+ { "name": "Tunisia", "code": "TN" },
223
+ { "name": "Turkey", "code": "TR" },
224
+ { "name": "Turkmenistan", "code": "TM" },
225
+ { "name": "Turks and Caicos Islands", "code": "TC" },
226
+ { "name": "Tuvalu", "code": "TV" },
227
+ { "name": "Uganda", "code": "UG" },
228
+ { "name": "Ukraine", "code": "UA" },
229
+ { "name": "United Arab Emirates", "code": "AE" },
230
+ { "name": "United Kingdom", "code": "GB" },
231
+ { "name": "United States", "code": "US" },
232
+ { "name": "United States Minor Outlying Islands", "code": "UM" },
233
+ { "name": "Uruguay", "code": "UY" },
234
+ { "name": "Uzbekistan", "code": "UZ" },
235
+ { "name": "Vanuatu", "code": "VU" },
236
+ { "name": "Venezuela", "code": "VE" },
237
+ { "name": "Viet Nam", "code": "VN" },
238
+ { "name": "Virgin Islands, British", "code": "VG" },
239
+ { "name": "Virgin Islands, U.S.", "code": "VI" },
240
+ { "name": "Wallis and Futuna", "code": "WF" },
241
+ { "name": "Western Sahara", "code": "EH" },
242
+ { "name": "Yemen", "code": "YE" },
243
+ { "name": "Zambia", "code": "ZM" },
244
+ { "name": "Zimbabwe", "code": "ZW" }
245
+ ]
@@ -0,0 +1,21 @@
1
+ type TWin = {
2
+ innerWidth: number;
3
+ innerHeight: number;
4
+ addEventListener: (event: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions) => void;
5
+ removeEventListener: (event: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions) => void;
6
+ };
7
+ type TDoc = {
8
+ body: {
9
+ scrollHeight: number;
10
+ scrollWidth: number;
11
+ };
12
+ };
13
+ export declare const win: TWin;
14
+ export declare const doc: TDoc;
15
+ export declare const storage: Storage | {
16
+ getItem: () => null;
17
+ setItem: () => void;
18
+ removeItem: () => void;
19
+ clear: () => void;
20
+ };
21
+ export {};
@@ -0,0 +1,26 @@
1
+ // Type casting with fallback to an empty object if undefined
2
+ export const win = typeof window !== "undefined"
3
+ ? window
4
+ : {
5
+ innerWidth: 0,
6
+ innerHeight: 0,
7
+ addEventListener: () => { },
8
+ removeEventListener: () => { },
9
+ };
10
+ export const doc = typeof document !== "undefined"
11
+ ? document
12
+ : {
13
+ body: {
14
+ scrollHeight: 0,
15
+ scrollWidth: 0,
16
+ },
17
+ };
18
+ // Use localStorage if available, or provide a fallback
19
+ export const storage = typeof localStorage !== "undefined"
20
+ ? localStorage
21
+ : {
22
+ getItem: () => null,
23
+ setItem: () => { },
24
+ removeItem: () => { },
25
+ clear: () => { },
26
+ };
@@ -0,0 +1,56 @@
1
+ /* eslint-disable no-undef */
2
+ /* eslint-disable no-unused-vars */
3
+ type TWin = {
4
+ innerWidth: number;
5
+ innerHeight: number;
6
+ addEventListener: (
7
+ event: string,
8
+ listener: EventListenerOrEventListenerObject,
9
+ options?: boolean | AddEventListenerOptions
10
+ ) => void;
11
+ removeEventListener: (
12
+ event: string,
13
+ listener: EventListenerOrEventListenerObject,
14
+ options?: boolean | EventListenerOptions
15
+ ) => void;
16
+ };
17
+
18
+ // Type definition for document body
19
+ type TDoc = {
20
+ body: {
21
+ scrollHeight: number;
22
+ scrollWidth: number; // Corrected from scrollWeight to scrollWidth
23
+ };
24
+ };
25
+
26
+ // Type casting with fallback to an empty object if undefined
27
+ export const win: TWin =
28
+ typeof window !== "undefined"
29
+ ? window
30
+ : ({
31
+ innerWidth: 0,
32
+ innerHeight: 0,
33
+ addEventListener: () => {},
34
+ removeEventListener: () => {},
35
+ } as TWin);
36
+
37
+ export const doc: TDoc =
38
+ typeof document !== "undefined"
39
+ ? document
40
+ : ({
41
+ body: {
42
+ scrollHeight: 0,
43
+ scrollWidth: 0,
44
+ },
45
+ } as TDoc);
46
+
47
+ // Use localStorage if available, or provide a fallback
48
+ export const storage =
49
+ typeof localStorage !== "undefined"
50
+ ? localStorage
51
+ : {
52
+ getItem: () => null,
53
+ setItem: () => {},
54
+ removeItem: () => {},
55
+ clear: () => {},
56
+ };
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Safely sets a value in localStorage (client-side only)
3
+ * @param {string} key - The key to set in localStorage
4
+ * @param {any} value - The value to store (will be stringified)
5
+ * @returns {boolean} - True if successful, false if not (or not on client-side)
6
+ */
7
+ export declare const setLocalStorage: (key: any, value: any) => boolean;
8
+ /**
9
+ * Safely gets a value from localStorage (client-side only)
10
+ * @param {string} key - The key to get from localStorage
11
+ * @returns {any} - The parsed value or null if not found or on server-side
12
+ */
13
+ export declare const getLocalStorage: (key: any) => any;
@@ -0,0 +1,50 @@
1
+ "use client";
2
+ /**
3
+ * Safely sets a value in localStorage (client-side only)
4
+ * @param {string} key - The key to set in localStorage
5
+ * @param {any} value - The value to store (will be stringified)
6
+ * @returns {boolean} - True if successful, false if not (or not on client-side)
7
+ */
8
+ export const setLocalStorage = (key, value) => {
9
+ // Only run on client side
10
+ if (typeof window === "undefined") {
11
+ console.warn("localStorage is not available on server-side");
12
+ return false;
13
+ }
14
+ try {
15
+ const valueToStore = typeof value === "string" ? value : JSON.stringify(value);
16
+ window.localStorage.setItem(key, valueToStore);
17
+ return true;
18
+ }
19
+ catch (error) {
20
+ console.error("Error setting localStorage:", error);
21
+ return false;
22
+ }
23
+ };
24
+ /**
25
+ * Safely gets a value from localStorage (client-side only)
26
+ * @param {string} key - The key to get from localStorage
27
+ * @returns {any} - The parsed value or null if not found or on server-side
28
+ */
29
+ export const getLocalStorage = (key) => {
30
+ // Only run on client side
31
+ if (typeof window === "undefined") {
32
+ console.warn("localStorage is not available on server-side");
33
+ return null;
34
+ }
35
+ try {
36
+ const item = window.localStorage.getItem(key);
37
+ if (item === null)
38
+ return null;
39
+ try {
40
+ return JSON.parse(item);
41
+ }
42
+ catch (_a) {
43
+ return item;
44
+ }
45
+ }
46
+ catch (error) {
47
+ console.error("Error getting localStorage:", error);
48
+ return null;
49
+ }
50
+ };
@@ -0,0 +1,52 @@
1
+ "use client";
2
+
3
+ /**
4
+ * Safely sets a value in localStorage (client-side only)
5
+ * @param {string} key - The key to set in localStorage
6
+ * @param {any} value - The value to store (will be stringified)
7
+ * @returns {boolean} - True if successful, false if not (or not on client-side)
8
+ */
9
+ export const setLocalStorage = (key: any, value: any) => {
10
+ // Only run on client side
11
+ if (typeof window === "undefined") {
12
+ console.warn("localStorage is not available on server-side");
13
+ return false;
14
+ }
15
+
16
+ try {
17
+ const valueToStore =
18
+ typeof value === "string" ? value : JSON.stringify(value);
19
+ window.localStorage.setItem(key, valueToStore);
20
+ return true;
21
+ } catch (error) {
22
+ console.error("Error setting localStorage:", error);
23
+ return false;
24
+ }
25
+ };
26
+
27
+ /**
28
+ * Safely gets a value from localStorage (client-side only)
29
+ * @param {string} key - The key to get from localStorage
30
+ * @returns {any} - The parsed value or null if not found or on server-side
31
+ */
32
+ export const getLocalStorage = (key: any) => {
33
+ // Only run on client side
34
+ if (typeof window === "undefined") {
35
+ console.warn("localStorage is not available on server-side");
36
+ return null;
37
+ }
38
+
39
+ try {
40
+ const item = window.localStorage.getItem(key);
41
+ if (item === null) return null;
42
+
43
+ try {
44
+ return JSON.parse(item);
45
+ } catch {
46
+ return item;
47
+ }
48
+ } catch (error) {
49
+ console.error("Error getting localStorage:", error);
50
+ return null;
51
+ }
52
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bikiran/utils",
3
- "version": "2.2.9",
3
+ "version": "2.3.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [