js-powerkit 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Niketa Jain
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,519 @@
1
+ # JS PowerKit
2
+
3
+ [![npm version](https://badge.fury.io/js/js-powerkit.svg)](https://badge.fury.io/js/js-powerkit)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+ [![Build Status](https://travis-ci.org/yourusername/js-powerkit.svg?branch=master)](https://travis-ci.org/yourusername/js-powerkit)
6
+
7
+ A comprehensive JavaScript utility library providing essential functions for strings, arrays, and objects. Built with modern ES6+ syntax and designed for performance and ease of use.
8
+
9
+ ## Features
10
+
11
+ - **String Utilities**: Capitalization, case conversion, truncation, palindrome checking, and more
12
+ - **Array Utilities**: Chunking, flattening, unique filtering, sorting, grouping, and set operations
13
+ - **Object Utilities**: Deep cloning, merging, property picking/omitting, and transformation
14
+ - **TypeScript Support**: Full type definitions included
15
+ - **Zero Dependencies**: Lightweight and fast
16
+ - **ES6+ Modules**: Modern JavaScript with tree-shaking support
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ npm install js-powerkit
22
+ ```
23
+
24
+ ## Quick Start
25
+
26
+ ```javascript
27
+ import { capitalize, chunk, deepClone } from 'js-powerkit';
28
+
29
+ // String utilities
30
+ console.log(capitalize('hello world')); // 'Hello world'
31
+
32
+ // Array utilities
33
+ console.log(chunk([1, 2, 3, 4, 5], 2)); // [[1, 2], [3, 4], [5]]
34
+
35
+ // Object utilities
36
+ const obj = { a: 1, b: { c: 2 } };
37
+ const cloned = deepClone(obj);
38
+ console.log(cloned); // { a: 1, b: { c: 2 } }
39
+ ```
40
+
41
+ ## API Documentation
42
+
43
+ ### String Utilities
44
+
45
+ #### `capitalize(str)`
46
+ Capitalizes the first letter of a string.
47
+ ```javascript
48
+ capitalize('hello world'); // 'Hello world'
49
+ ```
50
+
51
+ #### `toCamelCase(str)`
52
+ Converts a string to camelCase.
53
+ ```javascript
54
+ toCamelCase('hello world'); // 'helloWorld'
55
+ ```
56
+
57
+ #### `toKebabCase(str)`
58
+ Converts a string to kebab-case.
59
+ ```javascript
60
+ toKebabCase('Hello World'); // 'hello-world'
61
+ ```
62
+
63
+ #### `toSnakeCase(str)`
64
+ Converts a string to snake_case.
65
+ ```javascript
66
+ toSnakeCase('Hello World'); // 'hello_world'
67
+ ```
68
+
69
+ #### `toPascalCase(str)`
70
+ Converts a string to PascalCase.
71
+ ```javascript
72
+ toPascalCase('hello world'); // 'HelloWorld'
73
+ ```
74
+
75
+ #### `toTitleCase(str)`
76
+ Converts a string to TitleCase.
77
+ ```javascript
78
+ toTitleCase('hello world'); // 'Hello World'
79
+ ```
80
+
81
+ #### `reverse(str)`
82
+ Reverses a string.
83
+ ```javascript
84
+ reverse('hello'); // 'olleh'
85
+ ```
86
+
87
+ #### `truncate(str, length, suffix, addSuffix)`
88
+ Truncates a string to a specified length with an optional suffix.
89
+ ```javascript
90
+ truncate('Hello World', 5); // 'He...'
91
+ truncate('Hello World', 5, '***'); // 'He***'
92
+ truncate('Hello World', 5, '***', false); // 'Hello'
93
+ ```
94
+
95
+ #### `isPalindrome(str)`
96
+ Checks if a string is a palindrome (case-insensitive, ignores non-alphanumeric).
97
+ ```javascript
98
+ isPalindrome('racecar'); // true
99
+ isPalindrome('A man a plan a canal Panama'); // true
100
+ ```
101
+
102
+ #### `mask(str, visibleChars, maskChar)`
103
+ Masks a string (useful for sensitive data).
104
+ ```javascript
105
+ mask('1234567890', 2); // '12******90'
106
+ mask('1234567890', 2, '.'); // '12......90'
107
+ ```
108
+
109
+ #### `slugify(str)`
110
+ Converts a string to a URL-friendly slug.
111
+ ```javascript
112
+ slugify('Hello World!'); // 'hello-world'
113
+ ```
114
+
115
+ #### `countWords(str)`
116
+ Counts the number of words in a string.
117
+ ```javascript
118
+ countWords('Hello world!'); // 2
119
+ ```
120
+
121
+ #### `removeDuplicates(str)`
122
+ Removes duplicate characters from a string.
123
+ ```javascript
124
+ removeDuplicates('hello'); // 'helo'
125
+ ```
126
+
127
+ #### `removeWhitespace(str)`
128
+ Removes all whitespace from a string.
129
+ ```javascript
130
+ removeWhitespace(' hello world '); // 'helloworld'
131
+ ```
132
+
133
+ #### `extractEmails(str)`
134
+ Extracts all email addresses from a string.
135
+ ```javascript
136
+ extractEmails('Contact us at test@example.com or support@test.org'); // ['test@example.com', 'support@test.org']
137
+ ```
138
+
139
+ #### `extractUrls(str)`
140
+ Extracts all URLs addresses from a string.
141
+ ```javascript
142
+ extractUrls('Visit https://example.com or http://test.org'); // ['https://example.com', 'http://test.org']
143
+ ```
144
+
145
+ #### `stripHtml(str)`
146
+ Removes HTML tags from a string.
147
+ ```javascript
148
+ stripHtml('<p>Hello <b>World</b></p>'); // 'Hello World'
149
+ ```
150
+
151
+ #### `escapeHtml(str)`
152
+ Escapes HTML special characters.
153
+ ```javascript
154
+ escapeHtml('<div>Test & "quotes"</div>'); // '&lt;div&gt;Test &amp; &quot;quotes&quot;&lt;/div&gt;'
155
+ ```
156
+
157
+ #### `isNumeric(str)`
158
+ Checks if a string contains only numbers.
159
+ ```javascript
160
+ isNumeric('12345'); // true
161
+ ```
162
+
163
+ #### `isEmail(str)`
164
+ Checks if a string is a valid email.
165
+ ```javascript
166
+ isEmail('test@example.com'); // true
167
+ ```
168
+
169
+ #### `isUrl(str)`
170
+ Checks if a string is a valid URL.
171
+ ```javascript
172
+ isUrl('https://example.com'); // true
173
+ ```
174
+
175
+ #### `repeatString(str, times)`
176
+ Repeats a string n times
177
+ ```javascript
178
+ repeatString('ab', 3); // 'ababab'
179
+ ```
180
+
181
+ ### Array Utilities
182
+
183
+ #### `chunk(arr, size)`
184
+ Splits an array into chunks of specified size.
185
+ ```javascript
186
+ chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
187
+ ```
188
+
189
+ #### `flatten(arr, depth)`
190
+ Flattens a nested array to a specified depth.
191
+ ```javascript
192
+ flatten([1, [2, [3, 4]], 5]); // [1, 2, [3, 4], 5]
193
+ flatten([1, [2, [3, 4]], 5], 2); // [1, 2, 3, 4, 5]
194
+ ```
195
+
196
+ #### `unique(arr)`
197
+ Removes duplicate elements from an array.
198
+ ```javascript
199
+ unique([1, 2, 2, 3, 4, 4]); // [1, 2, 3, 4]
200
+ ```
201
+
202
+ #### `shuffle(arr)`
203
+ Shuffles the elements of an array randomly.
204
+ ```javascript
205
+ shuffle([1, 2, 3, 4]); // [3, 1, 4, 2] (random order)
206
+ ```
207
+
208
+ #### `randomElement(arr)`
209
+ Gets a random element from an array.
210
+ ```javascript
211
+ randomElement([1, 2, 3, 4, 5]); // 5 (random element)
212
+ ```
213
+
214
+ #### `compact(arr)`
215
+ Removes falsy values from an array.
216
+ ```javascript
217
+ compact([0, 1, false, 2, '', 3, null, undefined, NaN]); // [1, 2, 3]
218
+ ```
219
+
220
+ #### `sortBy(arr, prop, order)`
221
+ Sorts an array of objects by a property.
222
+ ```javascript
223
+ sortBy([{a: 2}, {a: 1}], 'a'); // [{a: 1}, {a: 2}]
224
+ sortBy([{a: 2}, {a: 1}], 'a', 'desc'); // [{a: 2}, {a: 1}]
225
+ ```
226
+
227
+ #### `groupBy(arr, prop)`
228
+ Groups an array of objects by a property.
229
+ ```javascript
230
+ groupBy([{type: 'a', val: 1}, {type: 'b', val: 2}, {type: 'a', val: 3}], 'type');
231
+ // {a: [{type: 'a', val: 1}, {type: 'a', val: 3}], b: [{type: 'b', val: 2}]}
232
+ ```
233
+
234
+ #### `intersection(arr1, arr2)`
235
+ Returns the intersection of two arrays.
236
+ ```javascript
237
+ intersection([1, 2, 3], [2, 3, 4]); // [2, 3]
238
+ ```
239
+
240
+ #### `difference(arr1, arr2)`
241
+ Returns the difference of two arrays.
242
+ ```javascript
243
+ difference([1, 2, 3], [2, 3, 4]); // [1]
244
+ ```
245
+
246
+ #### `union(arr1, arr2)`
247
+ Returns the union of two arrays.
248
+ ```javascript
249
+ union([1, 2, 3], [2, 3, 4]); // [1, 2, 3, 4]
250
+ ```
251
+
252
+ #### `zip(...arrays)`
253
+ Zips multiple arrays together.
254
+ ```javascript
255
+ zip([1, 2], ['a', 'b'], [true, false]); // [[1, 'a', true], [2, 'b', false]]
256
+ ```
257
+
258
+ #### `range(start, end, step)`
259
+ Creates an array of numbers from start to end.
260
+ ```javascript
261
+ range(1, 5); // [1, 2, 3, 4, 5]
262
+ range(0, 10, 2); // [0, 2, 4, 6, 8, 10]
263
+ ```
264
+
265
+ #### `max(arr)`
266
+ Finds the maximum value in an array.
267
+ ```javascript
268
+ max([1, 5, 3, 9, 2]); // 9
269
+ ```
270
+
271
+ #### `min(arr)`
272
+ Finds the minimum value in an array.
273
+ ```javascript
274
+ min([1, 5, 3, 9, 2]); // 1
275
+ ```
276
+
277
+ #### `sum(arr)`
278
+ Calculates the sum of array elements.
279
+ ```javascript
280
+ sum([1, 2, 3, 4, 5]); // 15
281
+ ```
282
+
283
+ #### `average(arr)`
284
+ Calculates the average of array elements.
285
+ ```javascript
286
+ average([1, 2, 3, 4, 5]); // 3
287
+ ```
288
+
289
+ #### `countOccurrences(arr)`
290
+ Counts occurrences of each element.
291
+ ```javascript
292
+ countOccurrences([1, 2, 2, 3, 3, 3]); // { '1': 1, '2': 2, '3': 3 }
293
+ ```
294
+
295
+ #### `remove(arr, value)`
296
+ Removes elements from array by value.
297
+ ```javascript
298
+ remove([1, 2, 3, 2, 4], 2); // [1, 3, 4]
299
+ ```
300
+
301
+ #### `take(arr, n)`
302
+ Takes first n elements from array.
303
+ ```javascript
304
+ take([10, 21, 31, 41, 51], 4); // [10, 21, 31, 41]
305
+ ```
306
+
307
+ #### `drop(arr, n)`
308
+ Drops first n elements from array.
309
+ ```javascript
310
+ drop([1, 2, 3, 4, 5], 2); // [3, 4, 5]
311
+ ```
312
+
313
+ #### `includesAll(arr, values)`
314
+ Checks if array includes all values.
315
+ ```javascript
316
+ includesAll([1, 2, 3, 4], [2, 3]); // true
317
+ includesAll([1, 2, 3], [2, 5]); // false
318
+ ```
319
+
320
+ #### `includesAny(arr, values)`
321
+ Checks if array includes any of the values.
322
+ ```javascript
323
+ includesAny([1, 2, 3], [3, 4, 5]); // true
324
+ includesAny([1, 2, 3], [4, 5, 6]); // false
325
+ ```
326
+
327
+ #### `rotate(arr, positions)`
328
+ Rotates array elements.
329
+ ```javascript
330
+ rotate([1, 2, 3, 4, 5], 2); // [3, 4, 5, 1, 2]
331
+ rotate([1, 2, 3, 4, 5], -2); // [4, 5, 1, 2, 3]
332
+ ```
333
+
334
+
335
+ ### Object Utilities
336
+
337
+ #### `deepClone(obj)`
338
+ Creates a deep clone of an object.
339
+ ```javascript
340
+ const obj = { a: 1, b: { c: 2 } };
341
+ const cloned = deepClone(obj);
342
+ // cloned is a deep copy of obj
343
+ ```
344
+
345
+ #### `deepMerge(...objects)`
346
+ Merges multiple objects into one.
347
+ ```javascript
348
+ deepMerge({a: 1}, {b: 2}); // {a: 1, b: 2}
349
+ deepMerge({a: {b: 1}}, {a: {c: 2}}); // {a: {b: 1, c: 2}}
350
+ ```
351
+
352
+ #### `pick(obj, keys)`
353
+ Picks specified properties from an object.
354
+ ```javascript
355
+ pick({a: 1, b: 2, c: 3}, ['a', 'c']); // {a: 1, c: 3}
356
+ ```
357
+
358
+ #### `omit(obj, keys)`
359
+ Omits specified properties from an object.
360
+ ```javascript
361
+ omit({a: 1, b: 2, c: 3}, ['b']); // {a: 1, c: 3}
362
+ ```
363
+
364
+ #### `isEmpty(obj)`
365
+ Checks if an object is empty.
366
+ ```javascript
367
+ isEmpty({}); // true
368
+ isEmpty({a: 1}); // false
369
+ ```
370
+
371
+ #### `invert(obj)`
372
+ Inverts the keys and values of an object.
373
+ ```javascript
374
+ invert({a: 1, b: 2}); // {1: 'a', 2: 'b'}
375
+ ```
376
+
377
+ #### `mapKeys(obj, fn)`
378
+ Maps the keys of an object using a function.
379
+ ```javascript
380
+ mapKeys({a: 1, b: 2}, key => key.toUpperCase()); // {A: 1, B: 2}
381
+ ```
382
+
383
+ #### `mapValues(obj, fn)`
384
+ Maps the values of an object using a function.
385
+ ```javascript
386
+ mapValues({a: 1, b: 2}, val => val * 2); // {a: 2, b: 4}
387
+ ```
388
+
389
+ #### `defaults(obj, defaults)`
390
+ Sets default values for an object.
391
+ ```javascript
392
+ defaults({a: 1}, {a: 2, b: 3}); // {a: 1, b: 3}
393
+ ```
394
+
395
+ #### `getNestedValue(obj, path, defaultValue)`
396
+ Gets a nested property value using dot notation.
397
+ ```javascript
398
+ getNestedValue({ a: { b: { c: 42 } } }, 'a.b.c'); // 42
399
+ getNestedValue({ a: { b: { c: 42 } } }, 'a.c', 'default'); // 'default'
400
+ ```
401
+
402
+ #### `setNestedValue(obj, path, value)`
403
+ Sets a nested property value using dot notation.
404
+ ```javascript
405
+ let obj = { a: { b: 1 } };
406
+ setNestedValue(obj, 'a.c.d', 42);
407
+ console.log(obj.a.c.d); // 42
408
+ ```
409
+
410
+ #### `getAllKeys(obj, prefix)`
411
+ Gets all keys from nested object.
412
+ ```javascript
413
+ getAllKeys({ a: 1, b: { c: 2, d: { e: 3 } } }); // ['a', 'b.c', 'b.d.e']
414
+ ```
415
+
416
+ #### `flattenObject(obj, prefix)`
417
+ Flattens a nested object.
418
+ ```javascript
419
+ flattenObject({ a: 1, b: { c: 2, d: { e: 3 } } }); // {'a': 1, 'b.c': 2, 'b.d.e': 3 }
420
+ ```
421
+
422
+ #### `unflattenObject(obj, prefix)`
423
+ Unflattens a flattened object.
424
+ ```javascript
425
+ unflattenObject({ 'a': 1, 'b.c': 2, 'b.d.e': 3 }); // { a: 1, b: { c: 2, d: { e: 3 } } }
426
+ ```
427
+
428
+ #### `removeNullish(obj)`
429
+ Removes null and undefined values from object.
430
+ ```javascript
431
+ removeNullish({ a: 1, b: null, c: undefined, d: 0, e: '' }); // { a: 1, d: 0, e: '' }
432
+ ```
433
+
434
+ #### `isEqual(obj1, obj2)`
435
+ Checks if two objects are deeply equal.
436
+ ```javascript
437
+ isEqual({ a: 1 }, { a: 1 }); // true
438
+ ```
439
+
440
+ #### `filterObject(obj, fn)`
441
+ Filters object by predicate function.
442
+ ```javascript
443
+ filterObject({ a: 1, b: 2, c: 3, d: 4 }, val => val % 2 === 0); // { b: 2, d: 4 }
444
+ ```
445
+
446
+ #### `toQueryString(obj)`
447
+ Converts object to query string.
448
+ ```javascript
449
+ toQueryString({ name: 'John', age: 30 }); // 'name=John&age=30'
450
+ ```
451
+
452
+ #### `fromQueryString(queryString)`
453
+ Converts query string to object.
454
+ ```javascript
455
+ fromQueryString('name=John&age=30'); // { name: 'John', age: 30 }
456
+ ```
457
+
458
+ #### `size(obj)`
459
+ Gets object size (number of properties).
460
+ ```javascript
461
+ size({ a: 1, b: 2, c: 3 }); // 3
462
+ size({}); // 0
463
+ ```
464
+
465
+ #### `has(obj, prop)`
466
+ Checks if an object has a property.
467
+ ```javascript
468
+ has({a: 1}, 'a'); // true
469
+ has({a: 1}, 'b'); // false
470
+ ```
471
+
472
+ #### `hasPath(obj, path)`
473
+ Checks if object has a nested property.
474
+ ```javascript
475
+ hasPath({ a: { b: { c: 1 } } }, 'a.b.c'); // true
476
+ hasPath({ a: { b: { c: 1 } } }, 'a.b.d'); // false
477
+ ```
478
+
479
+
480
+ ## Contributing
481
+
482
+ We welcome contributions! Please follow these steps:
483
+
484
+ 1. Fork the repository
485
+ 2. Create a feature branch (`git checkout -b feature/amazing-feature`)
486
+ 3. Commit your changes (`git commit -m 'Add amazing feature'`)
487
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
488
+ 5. Open a Pull Request
489
+
490
+ ## Testing
491
+
492
+ Run the test suite:
493
+
494
+ ```bash
495
+ npm test
496
+ ```
497
+
498
+ Run tests with coverage:
499
+
500
+ ```bash
501
+ npm run test:coverage
502
+ ```
503
+
504
+ ## License
505
+
506
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
507
+
508
+ ## Changelog
509
+
510
+ ### [1.0.0] - 2025-01-22
511
+ - Initial release with comprehensive string, array, and object utilities
512
+ - Full test coverage
513
+ - ES6+ module support
514
+
515
+ ## Support
516
+
517
+ If you find this library helpful, please give it a ⭐ on GitHub!
518
+
519
+ For issues or questions, please [open an issue](https://github.com/NiketaJain/js-powerkit/issues) on GitHub.
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "js-powerkit",
3
+ "version": "1.0.0",
4
+ "description": "A comprehensive JavaScript utility library providing essential functions for strings, arrays, and objects.",
5
+ "main": "src/index.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
9
+ "test:watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch",
10
+ "test:coverage": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage"
11
+ },
12
+ "keywords": [
13
+ "javascript",
14
+ "utils",
15
+ "utility",
16
+ "utilities",
17
+ "strings",
18
+ "arrays",
19
+ "objects",
20
+ "es6",
21
+ "npm",
22
+ "library",
23
+ "helpers",
24
+ "toolkit",
25
+ "manipulation",
26
+ "powerkit",
27
+ "formatting",
28
+ "validation"
29
+ ],
30
+ "author": "Niketa Jain",
31
+ "license": "MIT",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "https://github.com/NiketaJain/js-powerkit.git"
35
+ },
36
+ "bugs": {
37
+ "url": "https://github.com/NiketaJain/js-powerkit/issues"
38
+ },
39
+ "homepage": "https://github.com/NiketaJain/js-powerkit#readme",
40
+ "jest": {
41
+ "moduleNameMapper": {
42
+ "^(\\.{1,2}/.*)\\.js$": "$1"
43
+ }
44
+ },
45
+ "engines": {
46
+ "node": ">=14.0.0"
47
+ }
48
+ }