js-utils-core 1.0.1 → 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.
- package/README.md +320 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
# js-utils-core
|
|
2
|
+
|
|
3
|
+
A lightweight, functional utility library for JavaScript with clean, reusable functions inspired by lodash and radash.
|
|
4
|
+
|
|
5
|
+
## 📦 Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install js-utils-core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 🚀 Quick Start
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
const { camelCase, head, isEmpty, sum, clone } = require("js-utils-core");
|
|
15
|
+
|
|
16
|
+
// String utilities
|
|
17
|
+
camelCase("hello world"); // 'helloWorld'
|
|
18
|
+
|
|
19
|
+
// Array utilities
|
|
20
|
+
head([1, 2, 3]); // 1
|
|
21
|
+
sum([1, 2, 3, 4]); // 10
|
|
22
|
+
|
|
23
|
+
// Object utilities
|
|
24
|
+
clone({ a: { b: 1 } }); // Deep clone
|
|
25
|
+
|
|
26
|
+
// Type checking
|
|
27
|
+
isEmpty(""); // true
|
|
28
|
+
isEmpty([1, 2]); // false
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## 📚 Features
|
|
32
|
+
|
|
33
|
+
### Array Utilities
|
|
34
|
+
|
|
35
|
+
Transform and manipulate arrays with functional helpers.
|
|
36
|
+
|
|
37
|
+
- **`head(arr, n)`** - Get first element or n elements
|
|
38
|
+
- **`tail(arr, n)`** - Get last element or n elements
|
|
39
|
+
- **`flatten(arr, depth)`** - Flatten array by depth
|
|
40
|
+
- **`unique(arr, key)`** - Remove duplicates
|
|
41
|
+
- **`groupBy(arr, key)`** - Group array elements
|
|
42
|
+
- **`chunk(arr, size)`** - Create chunks of specified size
|
|
43
|
+
- **`minBy(arr, key)`** - Find minimum value
|
|
44
|
+
- **`maxBy(arr, key)`** - Find maximum value
|
|
45
|
+
- **`sum(arr, key)`** - Sum array values
|
|
46
|
+
|
|
47
|
+
```javascript
|
|
48
|
+
const { head, chunk, unique } = require("js-utils-core");
|
|
49
|
+
|
|
50
|
+
head([1, 2, 3]); // 1
|
|
51
|
+
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
|
|
52
|
+
unique([1, 1, 2, 2, 3]); // [1, 2, 3]
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Object Utilities
|
|
56
|
+
|
|
57
|
+
Work with objects safely and efficiently.
|
|
58
|
+
|
|
59
|
+
- **`clone(obj)`** - Deep clone using structuredClone or JSON fallback
|
|
60
|
+
- **`pick(obj, keys)`** - Pick specific keys
|
|
61
|
+
- **`omit(obj, keys)`** - Omit specific keys
|
|
62
|
+
- **`merge(...objects)`** - Shallow merge
|
|
63
|
+
- **`deepMerge(...objects)`** - Deep merge
|
|
64
|
+
- **`get(obj, path, default)`** - Get nested value safely
|
|
65
|
+
- **`set(obj, path, value)`** - Set nested value
|
|
66
|
+
- **`flatten(obj, prefix)`** - Flatten nested object
|
|
67
|
+
|
|
68
|
+
```javascript
|
|
69
|
+
const { pick, get, set } = require("js-utils-core");
|
|
70
|
+
|
|
71
|
+
const user = { name: "John", email: "john@example.com", age: 30 };
|
|
72
|
+
pick(user, ["name", "email"]); // { name: 'John', email: 'john@example.com' }
|
|
73
|
+
|
|
74
|
+
const config = { db: { host: "localhost", port: 5432 } };
|
|
75
|
+
get(config, "db.host"); // 'localhost'
|
|
76
|
+
set(config, "db.port", 3306); // Updates config.db.port
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### String Utilities
|
|
80
|
+
|
|
81
|
+
Transform strings with common case conversions and operations.
|
|
82
|
+
|
|
83
|
+
- **`camelCase(str)`** - Convert to camelCase
|
|
84
|
+
- **`snakeCase(str)`** - Convert to snake_case
|
|
85
|
+
- **`pascalCase(str)`** - Convert to PascalCase
|
|
86
|
+
- **`kebabCase(str)`** - Convert to kebab-case
|
|
87
|
+
- **`capitalize(str)`** - Capitalize first letter
|
|
88
|
+
- **`truncate(str, length, suffix)`** - Truncate with ellipsis
|
|
89
|
+
- **`reverse(str)`** - Reverse string
|
|
90
|
+
- **`repeat(str, n)`** - Repeat string n times
|
|
91
|
+
- **`padStart(str, length, padString)`** - Pad start
|
|
92
|
+
- **`padEnd(str, length, padString)`** - Pad end
|
|
93
|
+
|
|
94
|
+
```javascript
|
|
95
|
+
const { camelCase, snakeCase, capitalize } = require("js-utils-core");
|
|
96
|
+
|
|
97
|
+
camelCase("hello-world"); // 'helloWorld'
|
|
98
|
+
snakeCase("helloWorld"); // 'hello_world'
|
|
99
|
+
capitalize("john"); // 'John'
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Type Utilities
|
|
103
|
+
|
|
104
|
+
Accurate type checking and validation.
|
|
105
|
+
|
|
106
|
+
- **`typeOf(val)`** - Get exact type (array, date, null, etc.)
|
|
107
|
+
- **`isEmpty(val)`** - Check if empty
|
|
108
|
+
- **`isTruthy(val)`** - Check if truthy and not empty
|
|
109
|
+
- **`isArray(val)`** - Check if array
|
|
110
|
+
- **`isObject(val)`** - Check if object
|
|
111
|
+
- **`isString(val)`** - Check if string
|
|
112
|
+
- **`isNumber(val)`** - Check if valid number
|
|
113
|
+
- **`isPlainObject(val)`** - Check if plain object
|
|
114
|
+
- **`isDate(val)`** - Check if valid date
|
|
115
|
+
- **`isNumeric(val)`** - Check if numeric (including strings)
|
|
116
|
+
|
|
117
|
+
```javascript
|
|
118
|
+
const { isEmpty, isPlainObject, typeOf } = require("js-utils-core");
|
|
119
|
+
|
|
120
|
+
isEmpty([]); // true
|
|
121
|
+
isEmpty(""); // true
|
|
122
|
+
isPlainObject({ a: 1 }); // true
|
|
123
|
+
typeOf(new Date()); // 'date'
|
|
124
|
+
typeOf([1, 2, 3]); // 'array'
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Math Utilities
|
|
128
|
+
|
|
129
|
+
Calculate and validate numeric values.
|
|
130
|
+
|
|
131
|
+
- **`sum(numbers)`** - Sum array of numbers
|
|
132
|
+
- **`average(numbers)`** - Calculate average
|
|
133
|
+
- **`min(...numbers)`** - Find minimum
|
|
134
|
+
- **`max(...numbers)`** - Find maximum
|
|
135
|
+
- **`clamp(value, min, max)`** - Clamp between min and max
|
|
136
|
+
- **`round(num, decimals)`** - Round to n decimal places
|
|
137
|
+
- **`percentage(value, total)`** - Calculate percentage
|
|
138
|
+
- **`isEven(num)`** - Check if even
|
|
139
|
+
- **`isOdd(num)`** - Check if odd
|
|
140
|
+
- **`isPrime(num)`** - Check if prime
|
|
141
|
+
- **`random(min, max)`** - Generate random number
|
|
142
|
+
- **`randomInt(min, max)`** - Generate random integer
|
|
143
|
+
|
|
144
|
+
```javascript
|
|
145
|
+
const { sum, clamp, round, isPrime } = require("js-utils-core");
|
|
146
|
+
|
|
147
|
+
sum([1, 2, 3, 4]); // 10
|
|
148
|
+
clamp(5, 0, 10); // 5
|
|
149
|
+
clamp(15, 0, 10); // 10
|
|
150
|
+
round(3.14159, 2); // 3.14
|
|
151
|
+
isPrime(7); // true
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## 💡 Usage Examples
|
|
155
|
+
|
|
156
|
+
### Working with Arrays
|
|
157
|
+
|
|
158
|
+
```javascript
|
|
159
|
+
const { groupBy, chunk, maxBy } = require("js-utils-core");
|
|
160
|
+
|
|
161
|
+
const users = [
|
|
162
|
+
{ id: 1, team: "A", salary: 50000 },
|
|
163
|
+
{ id: 2, team: "B", salary: 60000 },
|
|
164
|
+
{ id: 3, team: "A", salary: 55000 },
|
|
165
|
+
];
|
|
166
|
+
|
|
167
|
+
// Group by team
|
|
168
|
+
const teams = groupBy(users, "team");
|
|
169
|
+
// { A: [...], B: [...] }
|
|
170
|
+
|
|
171
|
+
// Create batches
|
|
172
|
+
const batches = chunk(users, 2);
|
|
173
|
+
|
|
174
|
+
// Find highest salary
|
|
175
|
+
const topPaid = maxBy(users, (u) => u.salary);
|
|
176
|
+
// { id: 2, team: 'B', salary: 60000 }
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Working with Objects
|
|
180
|
+
|
|
181
|
+
```javascript
|
|
182
|
+
const { deepMerge, get, set } = require("js-utils-core");
|
|
183
|
+
|
|
184
|
+
const settings = {
|
|
185
|
+
theme: { dark: false, accent: "blue" },
|
|
186
|
+
notifications: { email: true },
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
// Deep merge
|
|
190
|
+
deepMerge(settings, { theme: { dark: true } });
|
|
191
|
+
|
|
192
|
+
// Safe nested access
|
|
193
|
+
get(settings, "theme.dark"); // false
|
|
194
|
+
get(settings, "theme.invalid", "default"); // 'default'
|
|
195
|
+
|
|
196
|
+
// Set nested value
|
|
197
|
+
set(settings, "notifications.push", true);
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### String Transformations
|
|
201
|
+
|
|
202
|
+
```javascript
|
|
203
|
+
const { camelCase, snakeCase, truncate } = require("js-utils-core");
|
|
204
|
+
|
|
205
|
+
const apiKey = "my-api-key-12345";
|
|
206
|
+
camelCase(apiKey); // 'myApiKey12345'
|
|
207
|
+
|
|
208
|
+
const description = "This is a very long description that needs truncation";
|
|
209
|
+
truncate(description, 20); // 'This is a very long...'
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## 🔍 Type Safety
|
|
213
|
+
|
|
214
|
+
All utilities perform safe type checking internally:
|
|
215
|
+
|
|
216
|
+
```javascript
|
|
217
|
+
const { get, sum } = require("js-utils-core");
|
|
218
|
+
|
|
219
|
+
const obj = null;
|
|
220
|
+
get(obj, "prop"); // undefined (no error)
|
|
221
|
+
|
|
222
|
+
const mixed = [1, "two", null, 4];
|
|
223
|
+
sum(mixed); // 5 (skips non-numbers)
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## 📖 Complete API
|
|
227
|
+
|
|
228
|
+
All functions are exported at the top level:
|
|
229
|
+
|
|
230
|
+
```javascript
|
|
231
|
+
const utils = require("js-utils-core");
|
|
232
|
+
|
|
233
|
+
// Array
|
|
234
|
+
(utils.head,
|
|
235
|
+
utils.tail,
|
|
236
|
+
utils.flatten,
|
|
237
|
+
utils.unique,
|
|
238
|
+
utils.groupBy,
|
|
239
|
+
utils.chunk,
|
|
240
|
+
utils.minBy,
|
|
241
|
+
utils.maxBy,
|
|
242
|
+
utils.sum);
|
|
243
|
+
|
|
244
|
+
// Object
|
|
245
|
+
(utils.clone,
|
|
246
|
+
utils.pick,
|
|
247
|
+
utils.omit,
|
|
248
|
+
utils.merge,
|
|
249
|
+
utils.deepMerge,
|
|
250
|
+
utils.get,
|
|
251
|
+
utils.set,
|
|
252
|
+
utils.flatten);
|
|
253
|
+
|
|
254
|
+
// String
|
|
255
|
+
(utils.camelCase,
|
|
256
|
+
utils.snakeCase,
|
|
257
|
+
utils.pascalCase,
|
|
258
|
+
utils.kebabCase,
|
|
259
|
+
utils.capitalize,
|
|
260
|
+
utils.truncate,
|
|
261
|
+
utils.reverse,
|
|
262
|
+
utils.repeat,
|
|
263
|
+
utils.padStart,
|
|
264
|
+
utils.padEnd,
|
|
265
|
+
utils.trim,
|
|
266
|
+
utils.startsWith,
|
|
267
|
+
utils.endsWith);
|
|
268
|
+
|
|
269
|
+
// Type
|
|
270
|
+
(utils.typeOf,
|
|
271
|
+
utils.isEmpty,
|
|
272
|
+
utils.isTruthy,
|
|
273
|
+
utils.isArray,
|
|
274
|
+
utils.isObject,
|
|
275
|
+
utils.isString,
|
|
276
|
+
utils.isNumber,
|
|
277
|
+
utils.isPlainObject,
|
|
278
|
+
utils.isDate,
|
|
279
|
+
utils.isNumeric,
|
|
280
|
+
utils.isInteger,
|
|
281
|
+
utils.isFinite);
|
|
282
|
+
|
|
283
|
+
// Math
|
|
284
|
+
(utils.sum,
|
|
285
|
+
utils.average,
|
|
286
|
+
utils.min,
|
|
287
|
+
utils.max,
|
|
288
|
+
utils.clamp,
|
|
289
|
+
utils.round,
|
|
290
|
+
utils.percentage,
|
|
291
|
+
utils.percentageOf,
|
|
292
|
+
utils.isEven,
|
|
293
|
+
utils.isOdd,
|
|
294
|
+
utils.isPrime,
|
|
295
|
+
utils.random,
|
|
296
|
+
utils.randomInt,
|
|
297
|
+
utils.difference,
|
|
298
|
+
utils.approximatelyEqual);
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
## 🎯 Why js-utils-core?
|
|
302
|
+
|
|
303
|
+
- **Lightweight** - Minimal bundle size with no dependencies
|
|
304
|
+
- **Functional** - Pure, composable functions
|
|
305
|
+
- **Type-safe** - Safe handling of null, undefined, and edge cases
|
|
306
|
+
- **Well-documented** - Clear JSDoc comments in source
|
|
307
|
+
- **Modern** - Written in ES6+
|
|
308
|
+
- **Battle-tested** - Inspired by industry standards (lodash, radash)
|
|
309
|
+
|
|
310
|
+
## 📄 License
|
|
311
|
+
|
|
312
|
+
ISC
|
|
313
|
+
|
|
314
|
+
## 👤 Author
|
|
315
|
+
|
|
316
|
+
Manasa
|
|
317
|
+
|
|
318
|
+
---
|
|
319
|
+
|
|
320
|
+
**Found a bug or have a suggestion?** Please open an issue on [GitHub](https://github.com/manasabol/js-utils-core).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "js-utils-core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "A lightweight collection of reusable JavaScript utility functions designed to simplify everyday development tasks and promote clean, consistent code.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|