@catbee/utils 1.1.0 → 2.0.0-next.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/LICENSE +1 -1
- package/README.md +79 -43
- package/array/index.cjs +355 -0
- package/array/index.d.ts +317 -0
- package/array/index.mjs +327 -0
- package/async/index.cjs +484 -0
- package/async/index.d.ts +299 -0
- package/async/index.mjs +463 -0
- package/cache/index.cjs +292 -0
- package/cache/index.d.ts +179 -0
- package/cache/index.mjs +290 -0
- package/config/index.cjs +150 -0
- package/config/index.d.ts +88 -0
- package/config/index.mjs +143 -0
- package/context-store/index.cjs +267 -0
- package/context-store/index.d.ts +216 -0
- package/context-store/index.mjs +261 -0
- package/crypto/index.cjs +182 -0
- package/crypto/index.d.ts +187 -0
- package/crypto/index.mjs +166 -0
- package/date/index.cjs +340 -0
- package/date/index.d.ts +214 -0
- package/date/index.mjs +326 -0
- package/decorators/index.cjs +2051 -0
- package/decorators/index.d.ts +708 -0
- package/decorators/index.mjs +2010 -0
- package/dir/index.cjs +417 -0
- package/dir/index.d.ts +219 -0
- package/dir/index.mjs +390 -0
- package/env/index.cjs +745 -0
- package/env/index.d.ts +403 -0
- package/env/index.mjs +742 -0
- package/exception/index.cjs +362 -0
- package/exception/index.d.ts +256 -0
- package/exception/index.mjs +338 -0
- package/fs/index.cjs +287 -0
- package/fs/index.d.ts +229 -0
- package/fs/index.mjs +258 -0
- package/http-status-codes/index.cjs +96 -0
- package/http-status-codes/index.d.ts +291 -0
- package/http-status-codes/index.mjs +94 -0
- package/id/index.cjs +62 -0
- package/id/index.d.ts +61 -0
- package/id/index.mjs +56 -0
- package/index.cjs +218 -0
- package/index.d.ts +51 -0
- package/index.mjs +51 -0
- package/logger/index.cjs +334 -0
- package/logger/index.d.ts +213 -0
- package/logger/index.mjs +313 -0
- package/middleware/index.cjs +177 -0
- package/middleware/index.d.ts +127 -0
- package/middleware/index.mjs +170 -0
- package/obj/index.cjs +305 -0
- package/obj/index.d.ts +160 -0
- package/obj/index.mjs +289 -0
- package/package.json +172 -20
- package/performance/index.cjs +231 -0
- package/performance/index.d.ts +162 -0
- package/performance/index.mjs +225 -0
- package/request/index.cjs +202 -0
- package/request/index.d.ts +265 -0
- package/request/index.mjs +194 -0
- package/response/index.cjs +234 -0
- package/response/index.d.ts +342 -0
- package/response/index.mjs +222 -0
- package/server/index.cjs +1631 -0
- package/server/index.d.ts +809 -0
- package/server/index.mjs +1622 -0
- package/stream/index.cjs +151 -0
- package/stream/index.d.ts +114 -0
- package/stream/index.mjs +144 -0
- package/string/index.cjs +109 -0
- package/string/index.d.ts +126 -0
- package/string/index.mjs +95 -0
- package/type/index.cjs +129 -0
- package/type/index.d.ts +131 -0
- package/type/index.mjs +119 -0
- package/types/index.cjs +34 -0
- package/types/index.d.ts +798 -0
- package/types/index.mjs +32 -0
- package/url/index.cjs +199 -0
- package/url/index.d.ts +166 -0
- package/url/index.mjs +187 -0
- package/validation/index.cjs +259 -0
- package/validation/index.d.ts +209 -0
- package/validation/index.mjs +231 -0
- package/build/index.cjs +0 -7694
- package/build/index.d.ts +0 -5792
- package/build/index.mjs +0 -7386
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
The MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2025 Catbee Technologies. https://catbee.
|
|
3
|
+
Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -53,22 +53,58 @@ npm i @catbee/utils
|
|
|
53
53
|
## ⚡ Quick Start
|
|
54
54
|
|
|
55
55
|
```ts
|
|
56
|
-
import {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
56
|
+
import { ServerConfigBuilder, ExpressServer } from '@catbee/utils/server';
|
|
57
|
+
import { Router } from 'express';
|
|
58
|
+
|
|
59
|
+
const config = new ServerConfigBuilder()
|
|
60
|
+
.withPort(3000)
|
|
61
|
+
.withCors({ origin: '*' })
|
|
62
|
+
.enableRateLimit({ max: 50, windowMs: 60000 })
|
|
63
|
+
.enableRequestLogging({ ignorePaths: ['/healthz', '/metrics'] })
|
|
64
|
+
.withHealthCheck({ path: '/health', detailed: true })
|
|
65
|
+
.enableOpenApi('./openapi.yaml', { mountPath: '/docs' })
|
|
66
|
+
.withGlobalHeaders({ 'X-Powered-By': 'Catbee' })
|
|
67
|
+
.withGlobalPrefix('/api')
|
|
68
|
+
.withRequestId({ headerName: 'X-Request-Id', exposeHeader: true })
|
|
69
|
+
.enableResponseTime({ addHeader: true, logOnComplete: true })
|
|
70
|
+
.build();
|
|
71
|
+
|
|
72
|
+
const server = new ExpressServer(config, {
|
|
73
|
+
beforeInit: srv => console.log('Initializing server...'),
|
|
74
|
+
afterInit: srv => console.log('Server initialized'),
|
|
75
|
+
beforeStart: app => console.log('Starting server...'),
|
|
76
|
+
afterStart: srv => console.log('Server started!'),
|
|
77
|
+
beforeStop: srv => console.log('Stopping server...'),
|
|
78
|
+
afterStop: () => console.log('Server stopped.'),
|
|
79
|
+
onRequest: (req, res, next) => {
|
|
80
|
+
console.log('Processing request:', req.method, req.url);
|
|
81
|
+
next();
|
|
82
|
+
},
|
|
83
|
+
onResponse: (req, res, next) => {
|
|
84
|
+
res.setHeader('X-Processed-By', 'ExpressServer');
|
|
85
|
+
next();
|
|
86
|
+
},
|
|
87
|
+
onError: (err, req, res, next) => {
|
|
88
|
+
console.error('Custom error handler:', err);
|
|
89
|
+
res.status(500).json({ error: 'Custom error: ' + err.message });
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Register routes
|
|
94
|
+
const router = server.createRouter('/users');
|
|
95
|
+
router.get('/', (req, res) => res.json({ users: [] }));
|
|
96
|
+
router.post('/', (req, res) => res.json({ created: true }));
|
|
97
|
+
|
|
98
|
+
// Or set a base router for all routes
|
|
99
|
+
const baseRouter = Router();
|
|
100
|
+
baseRouter.use('/users', router);
|
|
101
|
+
server.setBaseRouter(baseRouter);
|
|
102
|
+
|
|
103
|
+
server.registerHealthCheck('database', async () => await checkDatabaseConnection());
|
|
104
|
+
server.useMiddleware(loggingMiddleware, errorMiddleware);
|
|
105
|
+
|
|
106
|
+
await server.start();
|
|
107
|
+
server.enableGracefulShutdown();
|
|
72
108
|
```
|
|
73
109
|
|
|
74
110
|
---
|
|
@@ -77,31 +113,31 @@ console.log(isEmail("user@example.com")); // true
|
|
|
77
113
|
|
|
78
114
|
| Module | Description |
|
|
79
115
|
| ------ | ----------- |
|
|
80
|
-
| [Express Server](https://catbee.
|
|
81
|
-
| [Array Utilities](https://catbee.
|
|
82
|
-
| [Async Utilities](https://catbee.
|
|
83
|
-
| [Cache Utilities](https://catbee.
|
|
84
|
-
| [Context Store](https://catbee.
|
|
85
|
-
| [Crypto Utilities](https://catbee.
|
|
86
|
-
| [Date Utilities](https://catbee.
|
|
87
|
-
| [Decorators Utilities](https://catbee.
|
|
88
|
-
| [Directory Utilities](https://catbee.
|
|
89
|
-
| [Environment Utilities](https://catbee.
|
|
90
|
-
| [Exception Utilities](https://catbee.
|
|
91
|
-
| [File System Utilities](https://catbee.
|
|
92
|
-
| [HTTP Status Codes](https://catbee.
|
|
93
|
-
| [ID Utilities](https://catbee.
|
|
94
|
-
| [Logger Utilities](https://catbee.
|
|
95
|
-
| [Middleware Utilities](https://catbee.
|
|
96
|
-
| [Object Utilities](https://catbee.
|
|
97
|
-
| [Performance Utilities](https://catbee.
|
|
98
|
-
| [Request Utilities](https://catbee.
|
|
99
|
-
| [Response Utilities](https://catbee.
|
|
100
|
-
| [Stream Utilities](https://catbee.
|
|
101
|
-
| [String Utilities](https://catbee.
|
|
102
|
-
| [Type Utilities](https://catbee.
|
|
103
|
-
| [URL Utilities](https://catbee.
|
|
104
|
-
| [Validate Utilities](https://catbee.
|
|
116
|
+
| [Express Server](https://catbee.in/docs/@catbee/utils/express-server) | Fast, secure, and scalable server setup |
|
|
117
|
+
| [Array Utilities](https://catbee.in/docs/@catbee/utils/modules/array) | Advanced array manipulation |
|
|
118
|
+
| [Async Utilities](https://catbee.in/docs/@catbee/utils/modules/async) | Promise helpers, concurrency, timing |
|
|
119
|
+
| [Cache Utilities](https://catbee.in/docs/@catbee/utils/modules/cache) | In-memory caching with TTL |
|
|
120
|
+
| [Context Store](https://catbee.in/docs/@catbee/utils/modules/context-store) | Per-request context via AsyncLocalStorage |
|
|
121
|
+
| [Crypto Utilities](https://catbee.in/docs/@catbee/utils/modules/crypto) | Hashing, encryption, tokens |
|
|
122
|
+
| [Date Utilities](https://catbee.in/docs/@catbee/utils/modules/date) | Date/time manipulation |
|
|
123
|
+
| [Decorators Utilities](https://catbee.in/docs/@catbee/utils/modules/decorators) | TypeScript decorators for Express |
|
|
124
|
+
| [Directory Utilities](https://catbee.in/docs/@catbee/utils/modules/directory) | Directory and file system helpers |
|
|
125
|
+
| [Environment Utilities](https://catbee.in/docs/@catbee/utils/modules/environment) | Env variable management |
|
|
126
|
+
| [Exception Utilities](https://catbee.in/docs/@catbee/utils/modules/exception) | HTTP and error handling |
|
|
127
|
+
| [File System Utilities](https://catbee.in/docs/@catbee/utils/modules/file-system) | File operations |
|
|
128
|
+
| [HTTP Status Codes](https://catbee.in/docs/@catbee/utils/modules/http-status-codes) | Typed status codes |
|
|
129
|
+
| [ID Utilities](https://catbee.in/docs/@catbee/utils/modules/id) | UUID and ID generation |
|
|
130
|
+
| [Logger Utilities](https://catbee.in/docs/@catbee/utils/modules/logger) | Structured logging with Pino |
|
|
131
|
+
| [Middleware Utilities](https://catbee.in/docs/@catbee/utils/modules/middleware) | Express middleware collection |
|
|
132
|
+
| [Object Utilities](https://catbee.in/docs/@catbee/utils/modules/object) | Deep merge, flatten, pick/omit, etc. |
|
|
133
|
+
| [Performance Utilities](https://catbee.in/docs/@catbee/utils/modules/performance) | Timing, memoization, memory tracking |
|
|
134
|
+
| [Request Utilities](https://catbee.in/docs/@catbee/utils/modules/request) | HTTP request parameter parsing/validation |
|
|
135
|
+
| [Response Utilities](https://catbee.in/docs/@catbee/utils/modules/response) | Standardized API response formatting |
|
|
136
|
+
| [Stream Utilities](https://catbee.in/docs/@catbee/utils/modules/stream) | Stream conversion, batching, throttling, line splitting |
|
|
137
|
+
| [String Utilities](https://catbee.in/docs/@catbee/utils/modules/string) | Casing, masking, slugifying, formatting |
|
|
138
|
+
| [Type Utilities](https://catbee.in/docs/@catbee/utils/modules/type) | Type checking, conversion, guards |
|
|
139
|
+
| [URL Utilities](https://catbee.in/docs/@catbee/utils/modules/url) | URL parsing, query manipulation, normalization |
|
|
140
|
+
| [Validate Utilities](https://catbee.in/docs/@catbee/utils/modules/validation) | Input validation functions |
|
|
105
141
|
|
|
106
142
|
---
|
|
107
143
|
|
|
@@ -117,10 +153,10 @@ import { chunk, sleep, TTLCache, getLogger } from "@catbee/utils";
|
|
|
117
153
|
|
|
118
154
|
## 📖 Documentation
|
|
119
155
|
|
|
120
|
-
- [Full API Docs & Examples](https://catbee.
|
|
156
|
+
- [Full API Docs & Examples](https://catbee.in/docs/@catbee/utils/intro)
|
|
121
157
|
|
|
122
158
|
---
|
|
123
159
|
|
|
124
160
|
## 📜 License
|
|
125
161
|
|
|
126
|
-
MIT © Catbee Technologies (see the [LICENSE](https://catbee.
|
|
162
|
+
MIT © Catbee Technologies (see the [LICENSE](https://catbee.in/license/) file for the full text)
|
package/array/index.cjs
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
'use strict';
|
|
26
|
+
|
|
27
|
+
var obj = require('@catbee/utils/obj');
|
|
28
|
+
var crypto = require('crypto');
|
|
29
|
+
|
|
30
|
+
var __defProp = Object.defineProperty;
|
|
31
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
32
|
+
function chunk(array, size) {
|
|
33
|
+
if (!Array.isArray(array)) throw new TypeError("Expected an array");
|
|
34
|
+
if (array.length === 0) return [];
|
|
35
|
+
if (!Number.isInteger(size) || size <= 0) throw new Error("Chunk size must be a positive integer");
|
|
36
|
+
const result = [];
|
|
37
|
+
for (let i = 0; i < array.length; i += size) {
|
|
38
|
+
result.push(array.slice(i, i + size));
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
__name(chunk, "chunk");
|
|
43
|
+
function unique(array, keyFn) {
|
|
44
|
+
if (!Array.isArray(array) || array.length === 0) return [];
|
|
45
|
+
if (!keyFn) return Array.from(new Set(array));
|
|
46
|
+
const seen = /* @__PURE__ */ new Set();
|
|
47
|
+
const result = [];
|
|
48
|
+
for (const item of array) {
|
|
49
|
+
const key = keyFn(item);
|
|
50
|
+
if (!seen.has(key)) {
|
|
51
|
+
seen.add(key);
|
|
52
|
+
result.push(item);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
__name(unique, "unique");
|
|
58
|
+
function flattenDeep(array) {
|
|
59
|
+
if (!Array.isArray(array)) return [];
|
|
60
|
+
const result = [];
|
|
61
|
+
const stack = [
|
|
62
|
+
...array
|
|
63
|
+
];
|
|
64
|
+
while (stack.length) {
|
|
65
|
+
const val = stack.pop();
|
|
66
|
+
if (Array.isArray(val)) {
|
|
67
|
+
stack.push(...val);
|
|
68
|
+
} else {
|
|
69
|
+
result.push(val);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return result.reverse();
|
|
73
|
+
}
|
|
74
|
+
__name(flattenDeep, "flattenDeep");
|
|
75
|
+
function random(array) {
|
|
76
|
+
if (!Array.isArray(array) || array.length === 0) return void 0;
|
|
77
|
+
return array[secureIndex(array.length)];
|
|
78
|
+
}
|
|
79
|
+
__name(random, "random");
|
|
80
|
+
function groupBy(array, keyOrFn) {
|
|
81
|
+
if (!Array.isArray(array) || array.length === 0) return {};
|
|
82
|
+
const keyFn = typeof keyOrFn === "function" ? keyOrFn : (item) => String(obj.getValueByPath(item, keyOrFn));
|
|
83
|
+
const result = {};
|
|
84
|
+
for (const item of array) {
|
|
85
|
+
const key = String(keyFn(item));
|
|
86
|
+
if (Object.hasOwn?.(result, key) ?? Object.prototype.hasOwnProperty.call(result, key)) {
|
|
87
|
+
result[key].push(item);
|
|
88
|
+
} else {
|
|
89
|
+
result[key] = [
|
|
90
|
+
item
|
|
91
|
+
];
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return Object.fromEntries(Object.entries(result).map(([k, v]) => [
|
|
95
|
+
k,
|
|
96
|
+
v
|
|
97
|
+
]));
|
|
98
|
+
}
|
|
99
|
+
__name(groupBy, "groupBy");
|
|
100
|
+
function shuffle(array) {
|
|
101
|
+
if (!Array.isArray(array)) throw new TypeError("Expected an array");
|
|
102
|
+
const copy = array.slice();
|
|
103
|
+
for (let i = copy.length - 1; i > 0; i--) {
|
|
104
|
+
const j = secureIndex(i + 1);
|
|
105
|
+
[copy[i], copy[j]] = [
|
|
106
|
+
copy[j],
|
|
107
|
+
copy[i]
|
|
108
|
+
];
|
|
109
|
+
}
|
|
110
|
+
return copy;
|
|
111
|
+
}
|
|
112
|
+
__name(shuffle, "shuffle");
|
|
113
|
+
function pluck(array, key) {
|
|
114
|
+
if (!Array.isArray(array)) return [];
|
|
115
|
+
return array.map((item) => item?.[key]);
|
|
116
|
+
}
|
|
117
|
+
__name(pluck, "pluck");
|
|
118
|
+
function difference(a, b) {
|
|
119
|
+
if (!Array.isArray(a) || !Array.isArray(b)) return [];
|
|
120
|
+
const setB = new Set(b);
|
|
121
|
+
return a.filter((item) => !setB.has(item));
|
|
122
|
+
}
|
|
123
|
+
__name(difference, "difference");
|
|
124
|
+
function intersect(a, b) {
|
|
125
|
+
if (!Array.isArray(a) || !Array.isArray(b)) return [];
|
|
126
|
+
const setB = new Set(b);
|
|
127
|
+
return a.filter((item) => setB.has(item));
|
|
128
|
+
}
|
|
129
|
+
__name(intersect, "intersect");
|
|
130
|
+
function mergeSort(array, key, direction = "asc", compareFn) {
|
|
131
|
+
if (!Array.isArray(array)) throw new TypeError("Expected array");
|
|
132
|
+
const arr = array.slice();
|
|
133
|
+
if (arr.length <= 1) return arr;
|
|
134
|
+
const keyFn = typeof key === "function" ? key : (item) => obj.getValueByPath(item, key);
|
|
135
|
+
const collator = new Intl.Collator("en", {
|
|
136
|
+
numeric: true
|
|
137
|
+
});
|
|
138
|
+
const compare = compareFn ?? function(a, b) {
|
|
139
|
+
const aVal = keyFn(a);
|
|
140
|
+
const bVal = keyFn(b);
|
|
141
|
+
if (aVal === bVal) return 0;
|
|
142
|
+
if (aVal == null) return direction === "asc" ? 1 : -1;
|
|
143
|
+
if (bVal == null) return direction === "asc" ? -1 : 1;
|
|
144
|
+
return direction === "asc" ? collator.compare(String(aVal), String(bVal)) : collator.compare(String(bVal), String(aVal));
|
|
145
|
+
};
|
|
146
|
+
const merge = /* @__PURE__ */ __name((left, right) => {
|
|
147
|
+
const result = [];
|
|
148
|
+
let i = 0, j = 0;
|
|
149
|
+
while (i < left.length && j < right.length) {
|
|
150
|
+
result.push(compare(left[i], right[j]) <= 0 ? left[i++] : right[j++]);
|
|
151
|
+
}
|
|
152
|
+
while (i < left.length) result.push(left[i++]);
|
|
153
|
+
while (j < right.length) result.push(right[j++]);
|
|
154
|
+
return result;
|
|
155
|
+
}, "merge");
|
|
156
|
+
const sort = /* @__PURE__ */ __name((input) => {
|
|
157
|
+
const len = input.length;
|
|
158
|
+
if (len <= 1) return input;
|
|
159
|
+
const mid = len >> 1;
|
|
160
|
+
const left = sort(input.slice(0, mid));
|
|
161
|
+
const right = sort(input.slice(mid));
|
|
162
|
+
return merge(left, right);
|
|
163
|
+
}, "sort");
|
|
164
|
+
return sort(arr);
|
|
165
|
+
}
|
|
166
|
+
__name(mergeSort, "mergeSort");
|
|
167
|
+
function zip(...arrays) {
|
|
168
|
+
if (arrays.length === 0) return [];
|
|
169
|
+
if (arrays.some((arr) => !Array.isArray(arr))) {
|
|
170
|
+
throw new TypeError("All arguments must be arrays");
|
|
171
|
+
}
|
|
172
|
+
const minLength = Math.min(...arrays.map((arr) => arr.length));
|
|
173
|
+
const result = [];
|
|
174
|
+
for (let i = 0; i < minLength; i++) {
|
|
175
|
+
result.push(arrays.map((arr) => arr[i]));
|
|
176
|
+
}
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
__name(zip, "zip");
|
|
180
|
+
function partition(array, predicate) {
|
|
181
|
+
const pass = [];
|
|
182
|
+
const fail = [];
|
|
183
|
+
if (!Array.isArray(array)) return [
|
|
184
|
+
pass,
|
|
185
|
+
fail
|
|
186
|
+
];
|
|
187
|
+
for (let i = 0; i < array.length; i++) {
|
|
188
|
+
const item = array[i];
|
|
189
|
+
(predicate(item, i, array) ? pass : fail).push(item);
|
|
190
|
+
}
|
|
191
|
+
return [
|
|
192
|
+
pass,
|
|
193
|
+
fail
|
|
194
|
+
];
|
|
195
|
+
}
|
|
196
|
+
__name(partition, "partition");
|
|
197
|
+
function range(start, end, step = 1) {
|
|
198
|
+
if (!Number.isFinite(start) || !Number.isFinite(end) || !Number.isFinite(step)) {
|
|
199
|
+
throw new TypeError("Arguments must be finite numbers");
|
|
200
|
+
}
|
|
201
|
+
if (step === 0) throw new Error("Step cannot be zero");
|
|
202
|
+
const result = [];
|
|
203
|
+
if (step > 0) {
|
|
204
|
+
for (let i = start; i < end; i += step) result.push(i);
|
|
205
|
+
} else {
|
|
206
|
+
for (let i = start; i > end; i += step) result.push(i);
|
|
207
|
+
}
|
|
208
|
+
return result;
|
|
209
|
+
}
|
|
210
|
+
__name(range, "range");
|
|
211
|
+
function take(array, n = 1) {
|
|
212
|
+
if (!Array.isArray(array) || n <= 0) return [];
|
|
213
|
+
return n >= array.length ? array.slice() : array.slice(0, n);
|
|
214
|
+
}
|
|
215
|
+
__name(take, "take");
|
|
216
|
+
function takeWhile(array, predicate) {
|
|
217
|
+
const result = [];
|
|
218
|
+
if (!Array.isArray(array)) return result;
|
|
219
|
+
const len = array.length;
|
|
220
|
+
for (let i = 0; i < len; i++) {
|
|
221
|
+
const item = array[i];
|
|
222
|
+
if (!predicate(item, i)) break;
|
|
223
|
+
result.push(item);
|
|
224
|
+
}
|
|
225
|
+
return result;
|
|
226
|
+
}
|
|
227
|
+
__name(takeWhile, "takeWhile");
|
|
228
|
+
function compact(array) {
|
|
229
|
+
if (!Array.isArray(array)) return [];
|
|
230
|
+
const result = [];
|
|
231
|
+
for (const v of array) {
|
|
232
|
+
if (v) result.push(v);
|
|
233
|
+
}
|
|
234
|
+
return result;
|
|
235
|
+
}
|
|
236
|
+
__name(compact, "compact");
|
|
237
|
+
function countBy(array, keyFn) {
|
|
238
|
+
if (!Array.isArray(array)) return {};
|
|
239
|
+
const result = {};
|
|
240
|
+
for (const item of array) {
|
|
241
|
+
const key = String(keyFn(item));
|
|
242
|
+
result[key] = (result[key] ?? 0) + 1;
|
|
243
|
+
}
|
|
244
|
+
return result;
|
|
245
|
+
}
|
|
246
|
+
__name(countBy, "countBy");
|
|
247
|
+
function toggle(array, item) {
|
|
248
|
+
if (!Array.isArray(array)) return [
|
|
249
|
+
item
|
|
250
|
+
];
|
|
251
|
+
const exists = array.includes(item);
|
|
252
|
+
if (exists) return array.filter((x) => x !== item);
|
|
253
|
+
return [
|
|
254
|
+
...array,
|
|
255
|
+
item
|
|
256
|
+
];
|
|
257
|
+
}
|
|
258
|
+
__name(toggle, "toggle");
|
|
259
|
+
function secureIndex(max) {
|
|
260
|
+
if (!Number.isInteger(max) || max <= 0) throw new RangeError("Max must be a positive integer");
|
|
261
|
+
const limit = 4294967295 - 4294967295 % max;
|
|
262
|
+
let rand;
|
|
263
|
+
do {
|
|
264
|
+
rand = crypto.randomBytes(4).readUInt32BE(0);
|
|
265
|
+
} while (rand >= limit);
|
|
266
|
+
return rand % max;
|
|
267
|
+
}
|
|
268
|
+
__name(secureIndex, "secureIndex");
|
|
269
|
+
var secureRandom = /* @__PURE__ */ __name((array) => {
|
|
270
|
+
if (!Array.isArray(array) || array.length === 0) return void 0;
|
|
271
|
+
const idx = secureIndex(array.length);
|
|
272
|
+
return array[idx];
|
|
273
|
+
}, "secureRandom");
|
|
274
|
+
function findLast(array, predicate) {
|
|
275
|
+
if (!Array.isArray(array)) return void 0;
|
|
276
|
+
for (let i = array.length - 1; i >= 0; i--) {
|
|
277
|
+
if (predicate(array[i], i, array)) return array[i];
|
|
278
|
+
}
|
|
279
|
+
return void 0;
|
|
280
|
+
}
|
|
281
|
+
__name(findLast, "findLast");
|
|
282
|
+
function findLastIndex(array, predicate) {
|
|
283
|
+
if (!Array.isArray(array)) return -1;
|
|
284
|
+
for (let i = array.length - 1; i >= 0; i--) {
|
|
285
|
+
if (predicate(array[i], i, array)) return i;
|
|
286
|
+
}
|
|
287
|
+
return -1;
|
|
288
|
+
}
|
|
289
|
+
__name(findLastIndex, "findLastIndex");
|
|
290
|
+
function chunkBy(array, predicate) {
|
|
291
|
+
if (!Array.isArray(array) || array.length === 0) return [];
|
|
292
|
+
const result = [];
|
|
293
|
+
let chunk2 = [];
|
|
294
|
+
for (let i = 0; i < array.length; i++) {
|
|
295
|
+
if (predicate(array[i], i, array) && chunk2.length) {
|
|
296
|
+
result.push(chunk2);
|
|
297
|
+
chunk2 = [];
|
|
298
|
+
}
|
|
299
|
+
chunk2.push(array[i]);
|
|
300
|
+
}
|
|
301
|
+
if (chunk2.length) result.push(chunk2);
|
|
302
|
+
return result;
|
|
303
|
+
}
|
|
304
|
+
__name(chunkBy, "chunkBy");
|
|
305
|
+
function remove(array, value) {
|
|
306
|
+
if (!Array.isArray(array)) return [];
|
|
307
|
+
return array.filter((item) => item !== value);
|
|
308
|
+
}
|
|
309
|
+
__name(remove, "remove");
|
|
310
|
+
function isSorted(array, direction = "asc", compareFn) {
|
|
311
|
+
if (!Array.isArray(array) || array.length <= 1) return true;
|
|
312
|
+
const cmp = compareFn || ((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
313
|
+
for (let i = 1; i < array.length; i++) {
|
|
314
|
+
const res = cmp(array[i - 1], array[i]);
|
|
315
|
+
if (direction === "asc" && res > 0 || direction === "desc" && res < 0) return false;
|
|
316
|
+
}
|
|
317
|
+
return true;
|
|
318
|
+
}
|
|
319
|
+
__name(isSorted, "isSorted");
|
|
320
|
+
function headOfArr(array) {
|
|
321
|
+
return Array.isArray(array) && array.length > 0 ? array[0] : void 0;
|
|
322
|
+
}
|
|
323
|
+
__name(headOfArr, "headOfArr");
|
|
324
|
+
function lastOfArr(array) {
|
|
325
|
+
return Array.isArray(array) && array.length > 0 ? array.at(-1) : void 0;
|
|
326
|
+
}
|
|
327
|
+
__name(lastOfArr, "lastOfArr");
|
|
328
|
+
|
|
329
|
+
exports.chunk = chunk;
|
|
330
|
+
exports.chunkBy = chunkBy;
|
|
331
|
+
exports.compact = compact;
|
|
332
|
+
exports.countBy = countBy;
|
|
333
|
+
exports.difference = difference;
|
|
334
|
+
exports.findLast = findLast;
|
|
335
|
+
exports.findLastIndex = findLastIndex;
|
|
336
|
+
exports.flattenDeep = flattenDeep;
|
|
337
|
+
exports.groupBy = groupBy;
|
|
338
|
+
exports.headOfArr = headOfArr;
|
|
339
|
+
exports.intersect = intersect;
|
|
340
|
+
exports.isSorted = isSorted;
|
|
341
|
+
exports.lastOfArr = lastOfArr;
|
|
342
|
+
exports.mergeSort = mergeSort;
|
|
343
|
+
exports.partition = partition;
|
|
344
|
+
exports.pluck = pluck;
|
|
345
|
+
exports.random = random;
|
|
346
|
+
exports.range = range;
|
|
347
|
+
exports.remove = remove;
|
|
348
|
+
exports.secureIndex = secureIndex;
|
|
349
|
+
exports.secureRandom = secureRandom;
|
|
350
|
+
exports.shuffle = shuffle;
|
|
351
|
+
exports.take = take;
|
|
352
|
+
exports.takeWhile = takeWhile;
|
|
353
|
+
exports.toggle = toggle;
|
|
354
|
+
exports.unique = unique;
|
|
355
|
+
exports.zip = zip;
|