utilora 1.1.0 → 1.2.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilora",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "A lightweight, reusable JavaScript utility library for modern web and Node.js applications.",
5
5
  "main": "src/index.js",
6
6
  "keywords": [
@@ -0,0 +1,315 @@
1
+
2
+ // 1. Remove duplicate values
3
+ const uniqueArray = (array) => {
4
+ if (!Array.isArray(array)) {
5
+ throw new TypeError("Input must be an array");
6
+ }
7
+
8
+ return [...new Set(array)];
9
+ };
10
+
11
+ // 2. Flatten nested arrays
12
+
13
+ const flattenArray = (array, depth = Infinity) => {
14
+ if (!Array.isArray(array)) {
15
+ throw new TypeError("Input must be an array");
16
+ }
17
+
18
+ if (
19
+ depth !== Infinity &&
20
+ (!Number.isInteger(depth) || depth < 0)
21
+ ) {
22
+ throw new RangeError(
23
+ "Depth must be a non-negative integer or Infinity"
24
+ );
25
+ }
26
+
27
+ return array.flat(depth);
28
+ };
29
+
30
+ // 3. Split array into smaller arrays
31
+ const chunkArray = (array, size) => {
32
+ if (!Array.isArray(array)) {
33
+ throw new TypeError("Input must be an array");
34
+ }
35
+
36
+ if (!Number.isInteger(size) || size <= 0) {
37
+ throw new RangeError("Chunk size must be a positive integer");
38
+ }
39
+
40
+ const result = [];
41
+
42
+ for (let i = 0; i < array.length; i += size) {
43
+ result.push(array.slice(i, i + size));
44
+ }
45
+
46
+ return result;
47
+ };
48
+
49
+ // 4. Randomize array elements (Fisher-Yates shuffle)
50
+ const shuffleArray = (array) => {
51
+ if (!Array.isArray(array)) {
52
+ throw new TypeError("Input must be an array");
53
+ }
54
+
55
+ const result = [...array];
56
+
57
+ for (let i = result.length - 1; i > 0; i--) {
58
+ const j = Math.floor(Math.random() * (i + 1));
59
+
60
+ [result[i], result[j]] = [result[j], result[i]];
61
+ }
62
+
63
+ return result;
64
+ };
65
+
66
+ // 5. Group objects by property
67
+ const groupBy = (array, key) => {
68
+ if (!Array.isArray(array)) {
69
+ throw new TypeError("Input must be an array");
70
+ }
71
+
72
+ if (typeof key !== "string" && typeof key !== "function") {
73
+ throw new TypeError("Key must be a string or function");
74
+ }
75
+
76
+ return array.reduce((result, item) => {
77
+ const value = typeof key === "function" ? key(item) : item?.[key];
78
+ const groupKey = String(value);
79
+
80
+ if (!Object.prototype.hasOwnProperty.call(result, groupKey)) {
81
+ result[groupKey] = [];
82
+ }
83
+
84
+ result[groupKey].push(item);
85
+
86
+ return result;
87
+ }, {});
88
+ };
89
+
90
+ // 6. Sort objects by property
91
+ const sortBy = (array, key, order = "asc") => {
92
+ if (!Array.isArray(array)) {
93
+ throw new TypeError("Input must be an array");
94
+ }
95
+
96
+ if (typeof key !== "string" && typeof key !== "function") {
97
+ throw new TypeError("Key must be a string or function");
98
+ }
99
+
100
+ if (!["asc", "desc"].includes(order)) {
101
+ throw new RangeError("Order must be 'asc' or 'desc'");
102
+ }
103
+
104
+ const getValue = (item) =>
105
+ typeof key === "function" ? key(item) : item?.[key];
106
+
107
+ return [...array].sort((a, b) => {
108
+ const first = getValue(a);
109
+ const second = getValue(b);
110
+
111
+ let comparison;
112
+
113
+ if (first == null && second == null) comparison = 0;
114
+ else if (first == null) comparison = -1;
115
+ else if (second == null) comparison = 1;
116
+ else if (typeof first === "string" && typeof second === "string") {
117
+ comparison = first.localeCompare(second);
118
+ } else {
119
+ comparison = first < second ? -1 : first > second ? 1 : 0;
120
+ }
121
+
122
+ return order === "asc" ? comparison : -comparison;
123
+ });
124
+ };
125
+
126
+ // 7. Sort ascending
127
+ const sortAscending = (array) => {
128
+ if (!Array.isArray(array)) {
129
+ throw new TypeError("Input must be an array");
130
+ }
131
+
132
+ return [...array].sort((a, b) =>
133
+ typeof a === "string" && typeof b === "string"
134
+ ? a.localeCompare(b)
135
+ : a - b
136
+ );
137
+ };
138
+
139
+ // 8. Sort descending
140
+ const sortDescending = (array) => {
141
+ if (!Array.isArray(array)) {
142
+ throw new TypeError("Input must be an array");
143
+ }
144
+
145
+ return [...array].sort((a, b) =>
146
+ typeof a === "string" && typeof b === "string"
147
+ ? b.localeCompare(a)
148
+ : b - a
149
+ );
150
+ };
151
+
152
+ // 9. Find elements in first array but not second
153
+ const arrayDifference = (array1, array2) => {
154
+ if (!Array.isArray(array1) || !Array.isArray(array2)) {
155
+ throw new TypeError("Both inputs must be arrays");
156
+ }
157
+
158
+ const secondSet = new Set(array2);
159
+
160
+ return array1.filter((item) => !secondSet.has(item));
161
+ };
162
+
163
+ // 10. Find common elements
164
+ const arrayIntersection = (array1, array2) => {
165
+ if (!Array.isArray(array1) || !Array.isArray(array2)) {
166
+ throw new TypeError("Both inputs must be arrays");
167
+ }
168
+
169
+ const secondSet = new Set(array2);
170
+
171
+ return uniqueArray(array1.filter((item) => secondSet.has(item)));
172
+ };
173
+
174
+ // 11. Combine arrays without duplicates
175
+ const arrayUnion = (...arrays) => {
176
+ if (!arrays.every(Array.isArray)) {
177
+ throw new TypeError("All inputs must be arrays");
178
+ }
179
+
180
+ return uniqueArray(arrays.flat());
181
+ };
182
+
183
+ // 12. Calculate sum
184
+ const arraySum = (array) => {
185
+ if (!Array.isArray(array) || !array.every(Number.isFinite)) {
186
+ throw new TypeError("Input must contain only finite numbers");
187
+ }
188
+
189
+ return array.reduce((sum, value) => sum + value, 0);
190
+ };
191
+
192
+ // 13. Calculate average
193
+ const arrayAverage = (array) => {
194
+ if (!Array.isArray(array) || !array.every(Number.isFinite)) {
195
+ throw new TypeError("Input must contain only finite numbers");
196
+ }
197
+
198
+ if (array.length === 0) return 0;
199
+
200
+ return arraySum(array) / array.length;
201
+ };
202
+
203
+ // 14. Find minimum
204
+ const arrayMin = (array) => {
205
+ if (!Array.isArray(array) || !array.every(Number.isFinite)) {
206
+ throw new TypeError("Input must contain only finite numbers");
207
+ }
208
+
209
+ return array.length ? Math.min(...array) : undefined;
210
+ };
211
+
212
+ // 15. Find maximum
213
+ const arrayMax = (array) => {
214
+ if (!Array.isArray(array) || !array.every(Number.isFinite)) {
215
+ throw new TypeError("Input must contain only finite numbers");
216
+ }
217
+
218
+ return array.length ? Math.max(...array) : undefined;
219
+ };
220
+
221
+ // 16. Get random element
222
+ const randomItem = (array) => {
223
+ if (!Array.isArray(array)) {
224
+ throw new TypeError("Input must be an array");
225
+ }
226
+
227
+ if (array.length === 0) return undefined;
228
+
229
+ return array[Math.floor(Math.random() * array.length)];
230
+ };
231
+
232
+ // 17. Get first element
233
+ const firstItem = (array) => {
234
+ if (!Array.isArray(array)) {
235
+ throw new TypeError("Input must be an array");
236
+ }
237
+
238
+ return array[0];
239
+ };
240
+
241
+ // 18. Get last element
242
+ const lastItem = (array) => {
243
+ if (!Array.isArray(array)) {
244
+ throw new TypeError("Input must be an array");
245
+ }
246
+
247
+ return array[array.length - 1];
248
+ };
249
+
250
+ // 19. Split array based on condition
251
+ const partitionArray = (array, predicate) => {
252
+ if (!Array.isArray(array)) {
253
+ throw new TypeError("Input must be an array");
254
+ }
255
+
256
+ if (typeof predicate !== "function") {
257
+ throw new TypeError("Predicate must be a function");
258
+ }
259
+
260
+ return array.reduce(
261
+ (result, item, index) => {
262
+ result[predicate(item, index) ? 0 : 1].push(item);
263
+ return result;
264
+ },
265
+ [[], []]
266
+ );
267
+ };
268
+
269
+ // 20. Move an element to another index
270
+ const moveArrayItem = (array, fromIndex, toIndex) => {
271
+ if (!Array.isArray(array)) {
272
+ throw new TypeError("Input must be an array");
273
+ }
274
+
275
+ if (
276
+ !Number.isInteger(fromIndex) ||
277
+ !Number.isInteger(toIndex) ||
278
+ fromIndex < 0 ||
279
+ fromIndex >= array.length ||
280
+ toIndex < 0 ||
281
+ toIndex >= array.length
282
+ ) {
283
+ throw new RangeError("Invalid array index");
284
+ }
285
+
286
+ const result = [...array];
287
+ const [item] = result.splice(fromIndex, 1);
288
+
289
+ result.splice(toIndex, 0, item);
290
+
291
+ return result;
292
+ };
293
+
294
+ module.exports = {
295
+ uniqueArray,
296
+ flattenArray,
297
+ chunkArray,
298
+ shuffleArray,
299
+ groupBy,
300
+ sortBy,
301
+ sortAscending,
302
+ sortDescending,
303
+ arrayDifference,
304
+ arrayIntersection,
305
+ arrayUnion,
306
+ arraySum,
307
+ arrayAverage,
308
+ arrayMin,
309
+ arrayMax,
310
+ randomItem,
311
+ firstItem,
312
+ lastItem,
313
+ partitionArray,
314
+ moveArrayItem,
315
+ };
File without changes
File without changes
File without changes
File without changes
File without changes
package/src/index.js CHANGED
@@ -1,37 +1,27 @@
1
- // Capitalize first letter
2
- const capitalize = (text) => {
3
- if (typeof text !== "string") {
4
- throw new TypeError("Input must be a string");
5
- }
1
+ const stringUtils = require("./string");
2
+ const arrayUtils = require("./array");
3
+ const asyncUtils = require("./async");
4
+ const browserUtils = require("./browser");
5
+ const colorUtils = require("./color");
6
+ const dateUtils = require("./date");
7
+ const fileUtils = require("./file");
8
+ const numberUtils = require("./number");
9
+ const objectUtils = require("./object");
10
+ const securityUtils = require("./security");
11
+ const urlUtils = require("./url");
12
+ const validationUtils = require("./validation");
6
13
 
7
- return text.charAt(0).toUpperCase() + text.slice(1);
8
- };
9
-
10
- // Reverse string
11
- const reverseString = (text) => {
12
- return [...text].reverse().join("");
13
- };
14
-
15
- // Remove duplicate array values
16
- const uniqueArray = (array) => {
17
- return [...new Set(array)];
18
- };
19
-
20
- // Generate random number
21
- const randomNumber = (min = 1, max = 100) => {
22
- return Math.floor(Math.random() * (max - min + 1)) + min;
23
- };
24
-
25
- // Validate email
26
- const isValidEmail = (email) => {
27
- return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
28
- };
29
-
30
- // Export functions
31
14
  module.exports = {
32
- capitalize,
33
- reverseString,
34
- uniqueArray,
35
- randomNumber,
36
- isValidEmail,
15
+ ...stringUtils,
16
+ ...arrayUtils,
17
+ ...asyncUtils,
18
+ ...browserUtils,
19
+ ...colorUtils,
20
+ ...dateUtils,
21
+ ...fileUtils,
22
+ ...numberUtils,
23
+ ...objectUtils,
24
+ ...securityUtils,
25
+ ...urlUtils,
26
+ ...validationUtils
37
27
  };
@@ -0,0 +1,15 @@
1
+ const randomNumber = (min = 1, max = 100) => {
2
+ if (!Number.isFinite(min) || !Number.isFinite(max)) {
3
+ throw new TypeError("Minimum and maximum must be numbers");
4
+ }
5
+
6
+ if (min > max) {
7
+ throw new RangeError("Minimum cannot be greater than maximum");
8
+ }
9
+
10
+ return Math.floor(Math.random() * (max - min + 1)) + min;
11
+ };
12
+
13
+ module.exports = {
14
+ randomNumber,
15
+ };
File without changes
File without changes
@@ -0,0 +1,296 @@
1
+
2
+ // 1. Capitalize first letter
3
+ const capitalize = (text) => {
4
+ if (typeof text !== "string") {
5
+ throw new TypeError("Input must be a string");
6
+ }
7
+
8
+ return text.charAt(0).toUpperCase() + text.slice(1);
9
+ };
10
+
11
+ // 2. Capitalize every word
12
+ const capitalizeWords = (text) => {
13
+ if (typeof text !== "string") {
14
+ throw new TypeError("Input must be a string");
15
+ }
16
+
17
+ return text.replace(/\b\w/g, (char) => char.toUpperCase());
18
+ };
19
+
20
+ // 3. Convert to camelCase
21
+ const camelCase = (text) => {
22
+ if (typeof text !== "string") {
23
+ throw new TypeError("Input must be a string");
24
+ }
25
+
26
+ return text
27
+ .trim()
28
+ .toLowerCase()
29
+ .replace(/[^a-zA-Z0-9]+(.)?/g, (_, char) =>
30
+ char ? char.toUpperCase() : ""
31
+ );
32
+ };
33
+
34
+ // 4. Convert to PascalCase
35
+ const pascalCase = (text) => {
36
+ const result = camelCase(text);
37
+ return capitalize(result);
38
+ };
39
+
40
+ // 5. Convert to kebab-case
41
+ const kebabCase = (text) => {
42
+ if (typeof text !== "string") {
43
+ throw new TypeError("Input must be a string");
44
+ }
45
+
46
+ return text
47
+ .trim()
48
+ .replace(/([a-z0-9])([A-Z])/g, "$1-$2")
49
+ .replace(/[^a-zA-Z0-9]+/g, "-")
50
+ .replace(/^-+|-+$/g, "")
51
+ .toLowerCase();
52
+ };
53
+
54
+ // 6. Convert to snake_case
55
+ const snakeCase = (text) => {
56
+ if (typeof text !== "string") {
57
+ throw new TypeError("Input must be a string");
58
+ }
59
+
60
+ return text
61
+ .trim()
62
+ .replace(/([a-z0-9])([A-Z])/g, "$1_$2")
63
+ .replace(/[^a-zA-Z0-9]+/g, "_")
64
+ .replace(/^_+|_+$/g, "")
65
+ .toLowerCase();
66
+ };
67
+
68
+ // 7. Reverse string
69
+ const reverseString = (text) => {
70
+ if (typeof text !== "string") {
71
+ throw new TypeError("Input must be a string");
72
+ }
73
+
74
+ return [...text].reverse().join("");
75
+ };
76
+
77
+ // 8. Truncate text with ellipsis
78
+ const truncate = (text, length = 50, suffix = "...") => {
79
+ if (typeof text !== "string") {
80
+ throw new TypeError("Input must be a string");
81
+ }
82
+
83
+ if (!Number.isInteger(length) || length < 0) {
84
+ throw new TypeError("Length must be a non-negative integer");
85
+ }
86
+
87
+ if (text.length <= length) return text;
88
+
89
+ return text.slice(0, length) + suffix;
90
+ };
91
+
92
+ // 9. Remove extra whitespace
93
+ const removeWhitespace = (text) => {
94
+ if (typeof text !== "string") {
95
+ throw new TypeError("Input must be a string");
96
+ }
97
+
98
+ return text.trim().replace(/\s+/g, " ");
99
+ };
100
+
101
+ // 10. Generate URL-friendly slug
102
+ const slugify = (text) => {
103
+ if (typeof text !== "string") {
104
+ throw new TypeError("Input must be a string");
105
+ }
106
+
107
+ return text
108
+ .normalize("NFKD")
109
+ .replace(/[\u0300-\u036f]/g, "")
110
+ .toLowerCase()
111
+ .trim()
112
+ .replace(/[^a-z0-9]+/g, "-")
113
+ .replace(/^-+|-+$/g, "");
114
+ };
115
+
116
+ // 11. Count words
117
+ const countWords = (text) => {
118
+ if (typeof text !== "string") {
119
+ throw new TypeError("Input must be a string");
120
+ }
121
+
122
+ const trimmed = text.trim();
123
+
124
+ return trimmed ? trimmed.split(/\s+/).length : 0;
125
+ };
126
+
127
+ // 12. Count characters
128
+ const countCharacters = (text) => {
129
+ if (typeof text !== "string") {
130
+ throw new TypeError("Input must be a string");
131
+ }
132
+
133
+ return [...text].length;
134
+ };
135
+
136
+ // 13. Check palindrome
137
+ const isPalindrome = (text) => {
138
+ if (typeof text !== "string") {
139
+ throw new TypeError("Input must be a string");
140
+ }
141
+
142
+ const cleaned = text
143
+ .toLowerCase()
144
+ .replace(/[^a-z0-9]/g, "");
145
+
146
+ return cleaned === [...cleaned].reverse().join("");
147
+ };
148
+
149
+ // 14. Remove special characters
150
+ const removeSpecialCharacters = (text) => {
151
+ if (typeof text !== "string") {
152
+ throw new TypeError("Input must be a string");
153
+ }
154
+
155
+ return text.replace(/[^a-zA-Z0-9\s]/g, "");
156
+ };
157
+
158
+ // 15. Extract numbers from text
159
+ const extractNumbers = (text) => {
160
+ if (typeof text !== "string") {
161
+ throw new TypeError("Input must be a string");
162
+ }
163
+
164
+ return text.match(/-?\d+(?:\.\d+)?/g)?.map(Number) || [];
165
+ };
166
+
167
+ // 16. Escape HTML characters
168
+ const escapeHTML = (text) => {
169
+ if (typeof text !== "string") {
170
+ throw new TypeError("Input must be a string");
171
+ }
172
+
173
+ const entities = {
174
+ "&": "&amp;",
175
+ "<": "&lt;",
176
+ ">": "&gt;",
177
+ '"': "&quot;",
178
+ "'": "&#39;"
179
+ };
180
+
181
+ return text.replace(/[&<>"']/g, (char) => entities[char]);
182
+ };
183
+
184
+ // 17. Decode HTML entities
185
+ const unescapeHTML = (text) => {
186
+ if (typeof text !== "string") {
187
+ throw new TypeError("Input must be a string");
188
+ }
189
+
190
+ const entities = {
191
+ "&amp;": "&",
192
+ "&lt;": "<",
193
+ "&gt;": ">",
194
+ "&quot;": '"',
195
+ "&#39;": "'",
196
+ "&apos;": "'"
197
+ };
198
+
199
+ return text.replace(
200
+ /&(amp|lt|gt|quot|apos|#39);/g,
201
+ (entity) => entities[entity]
202
+ );
203
+ };
204
+
205
+ // 18. Mask sensitive text
206
+ const maskText = (text, visibleStart = 0, visibleEnd = 0, maskChar = "*") => {
207
+ if (typeof text !== "string") {
208
+ throw new TypeError("Input must be a string");
209
+ }
210
+
211
+ if (
212
+ !Number.isInteger(visibleStart) ||
213
+ !Number.isInteger(visibleEnd) ||
214
+ visibleStart < 0 ||
215
+ visibleEnd < 0
216
+ ) {
217
+ throw new TypeError("Visible lengths must be non-negative integers");
218
+ }
219
+
220
+ if (typeof maskChar !== "string" || [...maskChar].length !== 1) {
221
+ throw new TypeError("Mask character must be a single character");
222
+ }
223
+
224
+ const chars = [...text];
225
+
226
+ const maskLength = Math.max(
227
+ 0,
228
+ chars.length - visibleStart - visibleEnd
229
+ );
230
+
231
+ return (
232
+ chars.slice(0, visibleStart).join("") +
233
+ maskChar.repeat(maskLength) +
234
+ chars.slice(chars.length - visibleEnd).join("")
235
+ );
236
+ };
237
+
238
+ // 19. Repeat string
239
+ const repeatString = (text, count = 1) => {
240
+ if (typeof text !== "string") {
241
+ throw new TypeError("Input must be a string");
242
+ }
243
+
244
+ if (!Number.isInteger(count) || count < 0) {
245
+ throw new TypeError("Count must be a non-negative integer");
246
+ }
247
+
248
+ return text.repeat(count);
249
+ };
250
+
251
+ // 20. Remove duplicate words
252
+ const removeDuplicateWords = (text) => {
253
+ if (typeof text !== "string") {
254
+ throw new TypeError("Input must be a string");
255
+ }
256
+
257
+ const seen = new Set();
258
+
259
+ return text
260
+ .split(/\s+/)
261
+ .filter((word) => {
262
+ if (!word) return false;
263
+
264
+ const key = word.toLowerCase();
265
+
266
+ if (seen.has(key)) return false;
267
+
268
+ seen.add(key);
269
+ return true;
270
+ })
271
+ .join(" ");
272
+ };
273
+
274
+ // Export all string utilities
275
+ module.exports = {
276
+ capitalize,
277
+ capitalizeWords,
278
+ camelCase,
279
+ pascalCase,
280
+ kebabCase,
281
+ snakeCase,
282
+ reverseString,
283
+ truncate,
284
+ removeWhitespace,
285
+ slugify,
286
+ countWords,
287
+ countCharacters,
288
+ isPalindrome,
289
+ removeSpecialCharacters,
290
+ extractNumbers,
291
+ escapeHTML,
292
+ unescapeHTML,
293
+ maskText,
294
+ repeatString,
295
+ removeDuplicateWords
296
+ };
File without changes
@@ -0,0 +1,11 @@
1
+ const isValidEmail = (email) => {
2
+ if (typeof email !== "string") {
3
+ return false;
4
+ }
5
+
6
+ return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
7
+ };
8
+
9
+ module.exports = {
10
+ isValidEmail,
11
+ };