@ntnyq/utils 0.1.2 → 0.1.3

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.cjs CHANGED
@@ -24,9 +24,12 @@ __export(src_exports, {
24
24
  cAF: () => cAF,
25
25
  camelCase: () => camelCase,
26
26
  capitalize: () => capitalize,
27
+ chunk: () => chunk,
28
+ clamp: () => clamp,
27
29
  days: () => days,
28
30
  flatCase: () => flatCase,
29
31
  getObjectType: () => getObjectType,
32
+ hasOwnProperty: () => hasOwnProperty,
30
33
  hours: () => hours,
31
34
  isArray: () => isArray,
32
35
  isBoolean: () => isBoolean,
@@ -45,7 +48,9 @@ __export(src_exports, {
45
48
  lowerFirst: () => lowerFirst,
46
49
  minutes: () => minutes,
47
50
  noop: () => noop,
51
+ omit: () => omit,
48
52
  pascalCase: () => pascalCase,
53
+ pick: () => pick,
49
54
  rAF: () => rAF,
50
55
  seconds: () => seconds,
51
56
  snakeCase: () => snakeCase,
@@ -232,6 +237,11 @@ function weeks(count) {
232
237
  return count * ONE_WEEK;
233
238
  }
234
239
 
240
+ // src/misc/clamp.ts
241
+ function clamp(value, min = Number.NEGATIVE_INFINITY, max = Number.POSITIVE_INFINITY) {
242
+ return Math.min(Math.max(value, min), max);
243
+ }
244
+
235
245
  // src/misc/waitFor.ts
236
246
  function waitFor(ms) {
237
247
  return new Promise((resolve) => setTimeout(resolve, ms));
@@ -247,6 +257,15 @@ var warnOnce = (message) => {
247
257
  console.warn(message);
248
258
  };
249
259
 
260
+ // src/array/chunk.ts
261
+ function chunk(array, size) {
262
+ const result = [];
263
+ for (let i = 0; i < array.length; i += size) {
264
+ result.push(array.slice(i, i + size));
265
+ }
266
+ return result;
267
+ }
268
+
250
269
  // src/array/unique.ts
251
270
  function unique(array) {
252
271
  return Array.from(new Set(array));
@@ -273,15 +292,41 @@ function join(array, options = {}) {
273
292
  if (!Array.isArray(array) || !array.length) return "";
274
293
  return array.filter((v) => Boolean(v) || v === 0).join(separator);
275
294
  }
295
+
296
+ // src/object/omit.ts
297
+ function omit(object, ...keys) {
298
+ keys.forEach((key) => delete object[key]);
299
+ return object;
300
+ }
301
+
302
+ // src/object/hasOwnProperty.ts
303
+ function hasOwnProperty(object, key) {
304
+ return Object.prototype.hasOwnProperty.call(object, key);
305
+ }
306
+
307
+ // src/object/pick.ts
308
+ function pick(object, keys) {
309
+ return Object.assign(
310
+ {},
311
+ ...keys.map((key) => {
312
+ if (object && hasOwnProperty(object, key)) {
313
+ return { [key]: object[key] };
314
+ }
315
+ })
316
+ );
317
+ }
276
318
  // Annotate the CommonJS export names for ESM import in node:
277
319
  0 && (module.exports = {
278
320
  NOOP,
279
321
  cAF,
280
322
  camelCase,
281
323
  capitalize,
324
+ chunk,
325
+ clamp,
282
326
  days,
283
327
  flatCase,
284
328
  getObjectType,
329
+ hasOwnProperty,
285
330
  hours,
286
331
  isArray,
287
332
  isBoolean,
@@ -300,7 +345,9 @@ function join(array, options = {}) {
300
345
  lowerFirst,
301
346
  minutes,
302
347
  noop,
348
+ omit,
303
349
  pascalCase,
350
+ pick,
304
351
  rAF,
305
352
  seconds,
306
353
  snakeCase,
package/dist/index.d.cts CHANGED
@@ -74,6 +74,15 @@ declare function hours(count: number): number;
74
74
  declare function days(count: number): number;
75
75
  declare function weeks(count: number): number;
76
76
 
77
+ /**
78
+ * Clamps a number between a minimum and maximum value
79
+ * @param value the value to clamp within the given range
80
+ * @param min the minimum value to clamp
81
+ * @param max the maximum value to clamp
82
+ * @returns the new value
83
+ */
84
+ declare function clamp(value: number, min?: number, max?: number): number;
85
+
77
86
  /**
78
87
  * Wait for a number of milliseconds
79
88
  *
@@ -91,6 +100,14 @@ declare function waitFor(ms: number): Promise<unknown>;
91
100
 
92
101
  declare const warnOnce: (message: string) => void;
93
102
 
103
+ /**
104
+ * Splits an array into smaller chunks of a given size.
105
+ * @param array The array to split
106
+ * @param size The size of each chunk
107
+ * @returns An array of arrays, where each sub-array has `size` elements from the original array.
108
+ */
109
+ declare function chunk<T>(array: T[], size: number): T[][];
110
+
94
111
  /**
95
112
  * Returns a new array with unique values.
96
113
  * @param array - The array to process.
@@ -134,4 +151,10 @@ interface JoinOptions {
134
151
  */
135
152
  declare function join(array: JoinableValue[], options?: JoinOptions): string;
136
153
 
137
- export { type AnyFn, type Arrayable, type Awaitable, type MayBe, NOOP, type Nullable, type Prettify, cAF, capitalize, days, getObjectType, hours, isArray, isBoolean, isBrowser, isFunction, isInteger, isNativePromise, isNull, isNumber, isPromise, isString, isUndefined, join, minutes, noop, rAF, seconds, toArray, unique, uniqueBy, waitFor, warnOnce, weeks };
154
+ declare function omit<T, K extends keyof T>(object: T, ...keys: K[]): Omit<T, K>;
155
+
156
+ declare function pick<T, K extends keyof T>(object: T, keys: K[]): Pick<T, K>;
157
+
158
+ declare function hasOwnProperty<T, K extends keyof T>(object: T, key: K): boolean;
159
+
160
+ export { type AnyFn, type Arrayable, type Awaitable, type MayBe, NOOP, type Nullable, type Prettify, cAF, capitalize, chunk, clamp, days, getObjectType, hasOwnProperty, hours, isArray, isBoolean, isBrowser, isFunction, isInteger, isNativePromise, isNull, isNumber, isPromise, isString, isUndefined, join, minutes, noop, omit, pick, rAF, seconds, toArray, unique, uniqueBy, waitFor, warnOnce, weeks };
package/dist/index.d.ts CHANGED
@@ -74,6 +74,15 @@ declare function hours(count: number): number;
74
74
  declare function days(count: number): number;
75
75
  declare function weeks(count: number): number;
76
76
 
77
+ /**
78
+ * Clamps a number between a minimum and maximum value
79
+ * @param value the value to clamp within the given range
80
+ * @param min the minimum value to clamp
81
+ * @param max the maximum value to clamp
82
+ * @returns the new value
83
+ */
84
+ declare function clamp(value: number, min?: number, max?: number): number;
85
+
77
86
  /**
78
87
  * Wait for a number of milliseconds
79
88
  *
@@ -91,6 +100,14 @@ declare function waitFor(ms: number): Promise<unknown>;
91
100
 
92
101
  declare const warnOnce: (message: string) => void;
93
102
 
103
+ /**
104
+ * Splits an array into smaller chunks of a given size.
105
+ * @param array The array to split
106
+ * @param size The size of each chunk
107
+ * @returns An array of arrays, where each sub-array has `size` elements from the original array.
108
+ */
109
+ declare function chunk<T>(array: T[], size: number): T[][];
110
+
94
111
  /**
95
112
  * Returns a new array with unique values.
96
113
  * @param array - The array to process.
@@ -134,4 +151,10 @@ interface JoinOptions {
134
151
  */
135
152
  declare function join(array: JoinableValue[], options?: JoinOptions): string;
136
153
 
137
- export { type AnyFn, type Arrayable, type Awaitable, type MayBe, NOOP, type Nullable, type Prettify, cAF, capitalize, days, getObjectType, hours, isArray, isBoolean, isBrowser, isFunction, isInteger, isNativePromise, isNull, isNumber, isPromise, isString, isUndefined, join, minutes, noop, rAF, seconds, toArray, unique, uniqueBy, waitFor, warnOnce, weeks };
154
+ declare function omit<T, K extends keyof T>(object: T, ...keys: K[]): Omit<T, K>;
155
+
156
+ declare function pick<T, K extends keyof T>(object: T, keys: K[]): Pick<T, K>;
157
+
158
+ declare function hasOwnProperty<T, K extends keyof T>(object: T, key: K): boolean;
159
+
160
+ export { type AnyFn, type Arrayable, type Awaitable, type MayBe, NOOP, type Nullable, type Prettify, cAF, capitalize, chunk, clamp, days, getObjectType, hasOwnProperty, hours, isArray, isBoolean, isBrowser, isFunction, isInteger, isNativePromise, isNull, isNumber, isPromise, isString, isUndefined, join, minutes, noop, omit, pick, rAF, seconds, toArray, unique, uniqueBy, waitFor, warnOnce, weeks };
package/dist/index.js CHANGED
@@ -168,6 +168,11 @@ function weeks(count) {
168
168
  return count * ONE_WEEK;
169
169
  }
170
170
 
171
+ // src/misc/clamp.ts
172
+ function clamp(value, min = Number.NEGATIVE_INFINITY, max = Number.POSITIVE_INFINITY) {
173
+ return Math.min(Math.max(value, min), max);
174
+ }
175
+
171
176
  // src/misc/waitFor.ts
172
177
  function waitFor(ms) {
173
178
  return new Promise((resolve) => setTimeout(resolve, ms));
@@ -183,6 +188,15 @@ var warnOnce = (message) => {
183
188
  console.warn(message);
184
189
  };
185
190
 
191
+ // src/array/chunk.ts
192
+ function chunk(array, size) {
193
+ const result = [];
194
+ for (let i = 0; i < array.length; i += size) {
195
+ result.push(array.slice(i, i + size));
196
+ }
197
+ return result;
198
+ }
199
+
186
200
  // src/array/unique.ts
187
201
  function unique(array) {
188
202
  return Array.from(new Set(array));
@@ -209,14 +223,40 @@ function join(array, options = {}) {
209
223
  if (!Array.isArray(array) || !array.length) return "";
210
224
  return array.filter((v) => Boolean(v) || v === 0).join(separator);
211
225
  }
226
+
227
+ // src/object/omit.ts
228
+ function omit(object, ...keys) {
229
+ keys.forEach((key) => delete object[key]);
230
+ return object;
231
+ }
232
+
233
+ // src/object/hasOwnProperty.ts
234
+ function hasOwnProperty(object, key) {
235
+ return Object.prototype.hasOwnProperty.call(object, key);
236
+ }
237
+
238
+ // src/object/pick.ts
239
+ function pick(object, keys) {
240
+ return Object.assign(
241
+ {},
242
+ ...keys.map((key) => {
243
+ if (object && hasOwnProperty(object, key)) {
244
+ return { [key]: object[key] };
245
+ }
246
+ })
247
+ );
248
+ }
212
249
  export {
213
250
  NOOP,
214
251
  cAF,
215
252
  camelCase,
216
253
  capitalize,
254
+ chunk,
255
+ clamp,
217
256
  days,
218
257
  flatCase,
219
258
  getObjectType,
259
+ hasOwnProperty,
220
260
  hours,
221
261
  isArray,
222
262
  isBoolean,
@@ -235,7 +275,9 @@ export {
235
275
  lowerFirst,
236
276
  minutes,
237
277
  noop,
278
+ omit,
238
279
  pascalCase,
280
+ pick,
239
281
  rAF,
240
282
  seconds,
241
283
  snakeCase,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ntnyq/utils",
3
3
  "type": "module",
4
- "version": "0.1.2",
4
+ "version": "0.1.3",
5
5
  "description": "Common used utils.",
6
6
  "keywords": [
7
7
  "utils"
@@ -40,19 +40,19 @@
40
40
  "scule": "^1.3.0"
41
41
  },
42
42
  "devDependencies": {
43
- "@ntnyq/eslint-config": "^3.0.0-beta.17",
43
+ "@ntnyq/eslint-config": "^3.0.0-beta.19",
44
44
  "@ntnyq/prettier-config": "^1.21.3",
45
- "@vitest/coverage-v8": "^2.1.1",
46
- "bumpp": "^9.6.1",
47
- "eslint": "^9.11.1",
45
+ "@vitest/coverage-v8": "^2.1.2",
46
+ "bumpp": "^9.7.1",
47
+ "eslint": "^9.12.0",
48
48
  "husky": "^9.1.6",
49
49
  "nano-staged": "^0.8.0",
50
50
  "npm-run-all2": "^6.2.3",
51
- "pnpm": "^9.11.0",
51
+ "pnpm": "^9.12.1",
52
52
  "prettier": "^3.3.3",
53
53
  "tsup": "^8.3.0",
54
- "typescript": "^5.6.2",
55
- "vitest": "^2.1.1"
54
+ "typescript": "^5.6.3",
55
+ "vitest": "^2.1.2"
56
56
  },
57
57
  "engines": {
58
58
  "node": ">=18.18.0"