@oviirup/utils 1.0.6 → 1.0.7

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.
@@ -1,5 +1,5 @@
1
1
  declare namespace index_d_exports {
2
- export { at, first, last, move, range, toArray, toFiltered, unique };
2
+ export { at, chunk, first, last, move, range, toArray, toFiltered, unique };
3
3
  }
4
4
  /**
5
5
  * Converts a given value to represent itself in an array
@@ -58,5 +58,12 @@ declare function toFiltered<T>(array: T[], predicate: Predicate<T>): T[];
58
58
  * @category Array
59
59
  */
60
60
  declare function move<T>(array: T[], from: number, to: number): T[];
61
+ /**
62
+ * Chunk an array into smaller arrays of a given size
63
+ * @param array The array to chunk
64
+ * @param size The size of the chunks
65
+ * @category Array
66
+ */
67
+ declare function chunk<T>(array: T[], size: number): T[][];
61
68
  //#endregion
62
- export { at, first, last, move, range, index_d_exports as t, toArray, toFiltered, unique };
69
+ export { at, chunk, first, last, move, range, index_d_exports as t, toArray, toFiltered, unique };
@@ -1,9 +1,10 @@
1
- import { t as __exportAll } from "../chunk-BN_g-Awi.js";
1
+ import { t as __exportAll } from "../chunk-BYypO7fO.js";
2
2
  import { isEmptyArray } from "../assertions/index.js";
3
3
 
4
4
  //#region src/array/index.ts
5
5
  var array_exports = /* @__PURE__ */ __exportAll({
6
6
  at: () => at,
7
+ chunk: () => chunk,
7
8
  first: () => first,
8
9
  last: () => last,
9
10
  move: () => move,
@@ -79,6 +80,20 @@ function move(array, from, to) {
79
80
  array.splice(to, 0, item);
80
81
  return array;
81
82
  }
83
+ /**
84
+ * Chunk an array into smaller arrays of a given size
85
+ * @param array The array to chunk
86
+ * @param size The size of the chunks
87
+ * @category Array
88
+ */
89
+ function chunk(array, size) {
90
+ return array.reduce((acc, item, index) => {
91
+ const chunkIndex = Math.floor(index / size);
92
+ if (!acc[chunkIndex]) acc[chunkIndex] = [];
93
+ acc[chunkIndex].push(item);
94
+ return acc;
95
+ }, []);
96
+ }
82
97
 
83
98
  //#endregion
84
- export { at, first, last, move, range, array_exports as t, toArray, toFiltered, unique };
99
+ export { at, chunk, first, last, move, range, array_exports as t, toArray, toFiltered, unique };
@@ -1,4 +1,4 @@
1
- import { a as Dictionary, o as NegatePredicate, r as AnyFunction, s as Predicate } from "../types-C7M9sZlw.js";
1
+ import { AnyFunction, Dictionary, NegatePredicate, Predicate } from "../types.js";
2
2
 
3
3
  //#region src/assertions/index.d.ts
4
4
  /** Check if given value is a string */
@@ -1,6 +1,6 @@
1
- //#region rolldown:runtime
1
+ //#region \0rolldown/runtime.js
2
2
  var __defProp = Object.defineProperty;
3
- var __exportAll = (all, symbols) => {
3
+ var __exportAll = (all, no_symbols) => {
4
4
  let target = {};
5
5
  for (var name in all) {
6
6
  __defProp(target, name, {
@@ -8,7 +8,7 @@ var __exportAll = (all, symbols) => {
8
8
  enumerable: true
9
9
  });
10
10
  }
11
- if (symbols) {
11
+ if (!no_symbols) {
12
12
  __defProp(target, Symbol.toStringTag, { value: "Module" });
13
13
  }
14
14
  return target;
@@ -1,4 +1,4 @@
1
- import { i as ClassNameValue } from "../types-C7M9sZlw.js";
1
+ import { ClassNameValue } from "../types.js";
2
2
 
3
3
  //#region src/clsx/index.d.ts
4
4
  /**
package/dist/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import { t as index_d_exports } from "./array/index.js";
2
- import { i as ClassNameValue } from "./types-C7M9sZlw.js";
2
+ import { ClassNameValue } from "./types.js";
3
3
  import { isArray, isBrowser, isEmpty, isEmptyArray, isEmptyObject, isFloat, isFunction, isInteger, isNumber, isObject, isRegex, isString, isTruthy, not } from "./assertions/index.js";
4
4
  import { clsx } from "./clsx/index.js";
5
5
  import { charset, nanoid } from "./nanoid/index.js";
6
6
  import { t as index_d_exports$1 } from "./number/index.js";
7
7
  import { t as object_d_exports } from "./object.js";
8
- import { t as promise_d_exports } from "./promise.js";
9
- import { t as index_d_exports$2 } from "./string/index.js";
10
- export { ClassNameValue, index_d_exports as array, charset, clsx, isArray, isBrowser, isEmpty, isEmptyArray, isEmptyObject, isFloat, isFunction, isInteger, isNumber, isObject, isRegex, isString, isTruthy, nanoid, not, index_d_exports$1 as number, object_d_exports as object, promise_d_exports as promise, index_d_exports$2 as string };
8
+ import { t as index_d_exports$2 } from "./promise/index.js";
9
+ import { t as index_d_exports$3 } from "./string/index.js";
10
+ export { ClassNameValue, index_d_exports as array, charset, clsx, isArray, isBrowser, isEmpty, isEmptyArray, isEmptyObject, isFloat, isFunction, isInteger, isNumber, isObject, isRegex, isString, isTruthy, nanoid, not, index_d_exports$1 as number, object_d_exports as object, index_d_exports$2 as promise, index_d_exports$3 as string };
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@ import { clsx } from "./clsx/index.js";
4
4
  import { charset, nanoid } from "./nanoid/index.js";
5
5
  import { t as number_exports } from "./number/index.js";
6
6
  import { t as object_exports } from "./object.js";
7
- import { t as promise_exports } from "./promise.js";
7
+ import { t as promise_exports } from "./promise/index.js";
8
8
  import { t as string_exports } from "./string/index.js";
9
9
 
10
10
  export { array_exports as array, charset, clsx, isArray, isBrowser, isEmpty, isEmptyArray, isEmptyObject, isFloat, isFunction, isInteger, isNumber, isObject, isRegex, isString, isTruthy, nanoid, not, number_exports as number, object_exports as object, promise_exports as promise, string_exports as string };
@@ -1,4 +1,4 @@
1
- import { n as AbbreviationSymbols, t as AbbreviateOptions } from "../types-C7M9sZlw.js";
1
+ import { AbbreviateOptions, AbbreviationSymbols } from "../types.js";
2
2
 
3
3
  //#region src/number/index.d.ts
4
4
  declare namespace index_d_exports {
@@ -1,4 +1,4 @@
1
- import { t as __exportAll } from "../chunk-BN_g-Awi.js";
1
+ import { t as __exportAll } from "../chunk-BYypO7fO.js";
2
2
  import { isNumber, isObject } from "../assertions/index.js";
3
3
 
4
4
  //#region src/number/index.ts
package/dist/object.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { a as Dictionary } from "./types-C7M9sZlw.js";
1
+ import { Dictionary } from "./types.js";
2
2
 
3
3
  //#region src/object.d.ts
4
4
  declare namespace object_d_exports {
package/dist/object.js CHANGED
@@ -1,4 +1,4 @@
1
- import { t as __exportAll } from "./chunk-BN_g-Awi.js";
1
+ import { t as __exportAll } from "./chunk-BYypO7fO.js";
2
2
  import { isObject } from "./assertions/index.js";
3
3
  import { toArray } from "./array/index.js";
4
4
 
@@ -0,0 +1,31 @@
1
+ import { MaybePromise } from "../types.js";
2
+
3
+ //#region src/promise/index.d.ts
4
+ declare namespace index_d_exports {
5
+ export { retry, sleep, tryCatch };
6
+ }
7
+ /**
8
+ * Delays execution for a specified number of milliseconds
9
+ * @param delay The delay in milliseconds (must be a positive finite number)
10
+ * @returns A promise that resolves after the specified delay
11
+ * @category promise
12
+ */
13
+ declare function sleep(delay: number): Promise<void>;
14
+ /**
15
+ * Retries a function until it succeeds or the maximum number of retries is reached
16
+ * @param func The function to retry
17
+ * @param retries The number of retries
18
+ * @param delay The delay between retries (optional)
19
+ * @returns The result of the function
20
+ * @category promise
21
+ */
22
+ declare function retry<T>(func: () => Promise<T>, retries: number, delay?: number): Promise<T>;
23
+ /**
24
+ * Wraps a promise or function and returns a result object with discriminated union
25
+ * @param input A promise or function that returns a promise/value
26
+ * @returns A tuple [value, error] where value is the result or null, and error is undefined or the error
27
+ * @category promise
28
+ */
29
+ declare function tryCatch<T, E = Error>(input: Promise<T> | (() => MaybePromise<T>)): Promise<readonly [Awaited<T>, undefined] | readonly [null, E]>;
30
+ //#endregion
31
+ export { retry, sleep, index_d_exports as t, tryCatch };
@@ -0,0 +1,56 @@
1
+ import { t as __exportAll } from "../chunk-BYypO7fO.js";
2
+ import { isNumber } from "../assertions/index.js";
3
+
4
+ //#region src/promise/index.ts
5
+ var promise_exports = /* @__PURE__ */ __exportAll({
6
+ retry: () => retry,
7
+ sleep: () => sleep,
8
+ tryCatch: () => tryCatch
9
+ });
10
+ /**
11
+ * Delays execution for a specified number of milliseconds
12
+ * @param delay The delay in milliseconds (must be a positive finite number)
13
+ * @returns A promise that resolves after the specified delay
14
+ * @category promise
15
+ */
16
+ function sleep(delay) {
17
+ if (!isNumber(delay)) throw new TypeError("sleep: delay must be a number");
18
+ else if (!Number.isFinite(delay) || delay < 0) throw new RangeError("sleep: delay must be a positive finite number");
19
+ if (delay === 0) return Promise.resolve();
20
+ return new Promise((resolve) => setTimeout(resolve, delay));
21
+ }
22
+ /**
23
+ * Retries a function until it succeeds or the maximum number of retries is reached
24
+ * @param func The function to retry
25
+ * @param retries The number of retries
26
+ * @param delay The delay between retries (optional)
27
+ * @returns The result of the function
28
+ * @category promise
29
+ */
30
+ async function retry(func, retries, delay = 0) {
31
+ try {
32
+ return await func();
33
+ } catch (error) {
34
+ if (retries > 0) {
35
+ await sleep(delay);
36
+ return retry(func, retries - 1, delay);
37
+ }
38
+ throw error;
39
+ }
40
+ }
41
+ /**
42
+ * Wraps a promise or function and returns a result object with discriminated union
43
+ * @param input A promise or function that returns a promise/value
44
+ * @returns A tuple [value, error] where value is the result or null, and error is undefined or the error
45
+ * @category promise
46
+ */
47
+ async function tryCatch(input) {
48
+ try {
49
+ return [typeof input === "function" ? await input() : await input, void 0];
50
+ } catch (error) {
51
+ return [null, error];
52
+ }
53
+ }
54
+
55
+ //#endregion
56
+ export { retry, sleep, promise_exports as t, tryCatch };
@@ -1,4 +1,4 @@
1
- import { t as __exportAll } from "../chunk-BN_g-Awi.js";
1
+ import { t as __exportAll } from "../chunk-BYypO7fO.js";
2
2
 
3
3
  //#region src/string/index.ts
4
4
  var string_exports = /* @__PURE__ */ __exportAll({
package/dist/types.d.ts CHANGED
@@ -1,2 +1,17 @@
1
- import { a as Dictionary, i as ClassNameValue, n as AbbreviationSymbols, o as NegatePredicate, r as AnyFunction, s as Predicate, t as AbbreviateOptions } from "./types-C7M9sZlw.js";
2
- export { AbbreviateOptions, AbbreviationSymbols, AnyFunction, ClassNameValue, Dictionary, NegatePredicate, Predicate };
1
+ //#region src/types.d.ts
2
+ type Dictionary<T = any> = Record<string, T>;
3
+ type AnyFunction<T = any> = (...args: any[]) => T;
4
+ type ClassValue = string | number | bigint | null | boolean | undefined;
5
+ type ClassArray = ClassValue[];
6
+ type ClassRecord = Record<string, any>;
7
+ type ClassNameValue = ClassValue | ClassArray | ClassRecord;
8
+ type AbbreviationSymbols = Dictionary<number> | string[];
9
+ type AbbreviateOptions = {
10
+ symbols?: AbbreviationSymbols;
11
+ precision?: number;
12
+ };
13
+ type Predicate = ((val: unknown) => boolean) | ((val: unknown) => val is unknown);
14
+ type NegatePredicate<T> = T extends ((val: unknown) => val is infer U) ? <V>(val: V) => val is Exclude<V, U> : T extends ((val: unknown) => boolean) ? (val: unknown) => boolean : never;
15
+ type MaybePromise<T> = T | Promise<T>;
16
+ //#endregion
17
+ export { AbbreviateOptions, AbbreviationSymbols, AnyFunction, ClassNameValue, Dictionary, MaybePromise, NegatePredicate, Predicate };
package/dist/types.ts CHANGED
@@ -23,3 +23,5 @@ export type NegatePredicate<T> = T extends (val: unknown) => val is infer U
23
23
  : T extends (val: unknown) => boolean
24
24
  ? (val: unknown) => boolean
25
25
  : never;
26
+
27
+ export type MaybePromise<T> = T | Promise<T>;
package/license CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 Avirup Ghosh
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.
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Avirup Ghosh
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oviirup/utils",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "Collection of common JavaScript / TypeScript utilities bt @oviirup",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/oviirup/utils",
@@ -14,7 +14,7 @@
14
14
  "./nanoid": "./dist/nanoid/index.js",
15
15
  "./number": "./dist/number/index.js",
16
16
  "./object": "./dist/object.js",
17
- "./promise": "./dist/promise.js",
17
+ "./promise": "./dist/promise/index.js",
18
18
  "./string": "./dist/string/index.js",
19
19
  "./string/casing": "./dist/string/casing.js",
20
20
  "./types": "./dist/types.js",
@@ -29,10 +29,12 @@
29
29
  "typecheck": "tsc --noEmit"
30
30
  },
31
31
  "devDependencies": {
32
- "@biomejs/biome": "^2.3.13",
33
- "@types/bun": "^1.3.8",
34
- "lefthook": "^2.0.16",
35
- "tsdown": "^0.20.1",
32
+ "@biomejs/biome": "^2.3.14",
33
+ "@changesets/changelog-github": "^0.5.2",
34
+ "@changesets/cli": "^2.29.8",
35
+ "@types/bun": "^1.3.9",
36
+ "lefthook": "^2.1.0",
37
+ "tsdown": "^0.20.3",
36
38
  "typescript": "^5.9.3"
37
39
  },
38
40
  "publishConfig": {
package/dist/promise.d.ts DELETED
@@ -1,14 +0,0 @@
1
- declare namespace promise_d_exports {
2
- export { retry, sleep };
3
- }
4
- declare function sleep(delay: number): Promise<void>;
5
- /**
6
- * Retries a function until it succeeds or the maximum number of retries is reached
7
- * @param func The function to retry
8
- * @param retries The number of retries
9
- * @param delay The delay between retries (optional)
10
- * @returns The result of the function
11
- */
12
- declare function retry<T>(func: () => Promise<T>, retries: number, delay?: number): Promise<T>;
13
- //#endregion
14
- export { retry, sleep, promise_d_exports as t };
package/dist/promise.js DELETED
@@ -1,35 +0,0 @@
1
- import { t as __exportAll } from "./chunk-BN_g-Awi.js";
2
- import { isNumber } from "./assertions/index.js";
3
-
4
- //#region src/promise.ts
5
- var promise_exports = /* @__PURE__ */ __exportAll({
6
- retry: () => retry,
7
- sleep: () => sleep
8
- });
9
- function sleep(delay) {
10
- if (!isNumber(delay)) throw new TypeError("sleep: delay must be a number");
11
- else if (!Number.isFinite(delay) || delay < 0) throw new RangeError("sleep: delay must be a positive finite number");
12
- if (delay === 0) return Promise.resolve();
13
- return new Promise((resolve) => setTimeout(resolve, delay));
14
- }
15
- /**
16
- * Retries a function until it succeeds or the maximum number of retries is reached
17
- * @param func The function to retry
18
- * @param retries The number of retries
19
- * @param delay The delay between retries (optional)
20
- * @returns The result of the function
21
- */
22
- async function retry(func, retries, delay = 0) {
23
- try {
24
- return await func();
25
- } catch (error) {
26
- if (retries > 0) {
27
- await sleep(delay);
28
- return retry(func, retries - 1, delay);
29
- }
30
- throw error;
31
- }
32
- }
33
-
34
- //#endregion
35
- export { retry, sleep, promise_exports as t };
@@ -1,16 +0,0 @@
1
- //#region src/types.d.ts
2
- type Dictionary<T = any> = Record<string, T>;
3
- type AnyFunction<T = any> = (...args: any[]) => T;
4
- type ClassValue = string | number | bigint | null | boolean | undefined;
5
- type ClassArray = ClassValue[];
6
- type ClassRecord = Record<string, any>;
7
- type ClassNameValue = ClassValue | ClassArray | ClassRecord;
8
- type AbbreviationSymbols = Dictionary<number> | string[];
9
- type AbbreviateOptions = {
10
- symbols?: AbbreviationSymbols;
11
- precision?: number;
12
- };
13
- type Predicate = ((val: unknown) => boolean) | ((val: unknown) => val is unknown);
14
- type NegatePredicate<T> = T extends ((val: unknown) => val is infer U) ? <V>(val: V) => val is Exclude<V, U> : T extends ((val: unknown) => boolean) ? (val: unknown) => boolean : never;
15
- //#endregion
16
- export { Dictionary as a, ClassNameValue as i, AbbreviationSymbols as n, NegatePredicate as o, AnyFunction as r, Predicate as s, AbbreviateOptions as t };