Skip to content
0

katanakit-js / UtilsService

Class: UtilsService ​

Defined in: src/core/services/utils.service.ts:9

UtilsService - Singleton para utilidades genéricas.

Aplica el patrón Singleton: una única instancia que centraliza utilidades de arrays, objetos, tiempo y configuración. Puro, sin I/O, seguro para SSR.

Methods ​

useAverage() ​

ts
useAverage(numbers): number;

Defined in: src/core/services/utils.service.ts:360

Computes the arithmetic mean of an array of numbers. Returns 0 for an empty array.

Parameters ​

ParameterTypeDescription
numbersnumber[]The input numbers.

Returns ​

number

The arithmetic mean.

Example ​

ts
useAverage([1, 2, 3, 4, 5]); // 3
useAverage([]);               // 0

useChunk() ​

ts
useChunk<T>(array, size): T[][];

Defined in: src/core/services/utils.service.ts:67

Splits an array into chunks of the given size.

Type Parameters ​

Type ParameterDescription
TElement type.

Parameters ​

ParameterTypeDescription
arrayT[]The input array.
sizenumberNumber of elements per chunk (must be > 0).

Returns ​

T[][]

An array of chunks.

Throws ​

If size is less than or equal to 0.

Example ​

ts
useChunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
useChunk(["a", "b", "c"], 1);  // [["a"], ["b"], ["c"]]

useCopyToClipboard() ​

ts
useCopyToClipboard(text): Promise<boolean>;

Defined in: src/core/services/utils.service.ts:292

Copies text to the clipboard (browser only). Returns false on failure or in non-browser environments (SSR).

Parameters ​

ParameterTypeDescription
textstringThe text to copy.

Returns ​

Promise<boolean>

true if the text was copied, false otherwise.

Example ​

ts
const ok = await useCopyToClipboard("Hello, world!");
console.log(ok); // true (in a browser with clipboard API)

useDeepClone() ​

ts
useDeepClone<T>(value): T;

Defined in: src/core/services/utils.service.ts:141

Deep clones a value using structuredClone with a JSON fallback for environments that do not support it.

Type Parameters ​

Type ParameterDescription
TValue type.

Parameters ​

ParameterTypeDescription
valueTThe value to clone.

Returns ​

T

A deep copy of the value.

Example ​

ts
const original = { a: { b: 1 } };
const copy = useDeepClone(original);
copy.a.b = 2;
original.a.b; // 1 (unchanged)

useDeepMerge() ​

ts
useDeepMerge<T>(target, source): T;

Defined in: src/core/services/utils.service.ts:166

Deep merges source into a shallow copy of target. Nested plain objects are merged recursively. Skips prototype-pollution keys (__proto__, constructor, prototype).

Type Parameters ​

Type ParameterDescription
T extends Record<string, unknown>Target object type.

Parameters ​

ParameterTypeDescription
targetTThe base object (not mutated).
sourceRecord<string, unknown>The object to merge in.

Returns ​

T

A new merged object.

Example ​

ts
const base = { a: 1, b: { c: 2, d: 3 } };
const override = { b: { c: 99, e: 4 }, f: 5 };
useDeepMerge(base, override);
// { a: 1, b: { c: 99, d: 3, e: 4 }, f: 5 }

useGetUrlParams() ​

ts
useGetUrlParams(urlString): Record<string, string>;

Defined in: src/core/services/utils.service.ts:317

Parses query parameters from a URL string into a plain object. Returns an empty object for invalid URLs.

Parameters ​

ParameterTypeDescription
urlStringstringThe URL to parse.

Returns ​

Record<string, string>

An object mapping parameter names to values.

Example ​

ts
useGetUrlParams("https://example.com?q=hello&page=1");
// { q: "hello", page: "1" }

useGroupBy() ​

ts
useGroupBy<T>(array, key): Record<string, T[]>;

Defined in: src/core/services/utils.service.ts:93

Groups array items by a key property or key selector function.

Type Parameters ​

Type ParameterDescription
TElement type.

Parameters ​

ParameterTypeDescription
arrayT[]The input array.
keykeyof T | ((item) => string)A property name or a function that returns the group key.

Returns ​

Record<string, T[]>

An object mapping group keys to arrays of items.

Example ​

ts
const items = [
  { type: "fruit", name: "apple" },
  { type: "veggie", name: "carrot" },
  { type: "fruit", name: "banana" },
];
useGroupBy(items, "type");
// { fruit: [{ type: "fruit", name: "apple" }, { type: "fruit", name: "banana" }], veggie: [...] }

useIsObject() ​

ts
useIsObject(item): item is Record<string, unknown>;

Defined in: src/core/services/utils.service.ts:121

Type guard that checks whether a value is a plain object (not null, not an array).

Parameters ​

ParameterTypeDescription
itemunknownThe value to test.

Returns ​

item is Record<string, unknown>

true if the value is a plain Record<string, unknown>.

Example ​

ts
useIsObject({ a: 1 });       // true
useIsObject([1, 2]);          // false
useIsObject(null);            // false

useOmit() ​

ts
useOmit<T, K>(obj, keys): Omit<T, K>;

Defined in: src/core/services/utils.service.ts:228

Omits the listed keys from an object, returning a shallow copy without them. Shallow on purpose to avoid structuredClone failures on non-cloneable values.

Type Parameters ​

Type ParameterDescription
T extends objectSource object type.
K extends string | number | symbolKeys to omit.

Parameters ​

ParameterTypeDescription
objTThe source object.
keysK[]Array of keys to exclude.

Returns ​

Omit<T, K>

A new object without the omitted keys.

Example ​

ts
const user = { id: 1, name: "Alice", password: "secret" };
useOmit(user, ["password"]); // { id: 1, name: "Alice" }

usePick() ​

ts
usePick<T, K>(obj, keys): Pick<T, K>;

Defined in: src/core/services/utils.service.ts:201

Picks the listed keys from an object into a new object.

Type Parameters ​

Type ParameterDescription
T extends objectSource object type.
K extends string | number | symbolKeys to pick.

Parameters ​

ParameterTypeDescription
objTThe source object.
keysK[]Array of keys to include.

Returns ​

Pick<T, K>

A new object with only the picked keys.

Example ​

ts
const user = { id: 1, name: "Alice", email: "a@b.com" };
usePick(user, ["id", "name"]); // { id: 1, name: "Alice" }

useRetry() ​

ts
useRetry<T>(
   fn, 
   retries?, 
   delayMs?
): Promise<T>;

Defined in: src/core/services/utils.service.ts:269

Retries an async function with a fixed delay between attempts.

Type Parameters ​

Type ParameterDescription
TThe resolved value type.

Parameters ​

ParameterTypeDefault valueDescription
fn() => Promise<T>undefinedThe async function to retry.
retriesnumber3Maximum number of retries (defaults to 3).
delayMsnumber1000Delay between retries in milliseconds (defaults to 1000).

Returns ​

Promise<T>

The resolved value of fn.

Throws ​

The last error if all retries are exhausted.

Example ​

ts
const data = await useRetry(() => fetch("/api").then(r => r.json()), 3, 500);

useRound() ​

ts
useRound(value, decimals?): number;

Defined in: src/core/services/utils.service.ts:340

Rounds a number (or numeric string) to the specified number of decimal places. Returns 0 for non-numeric input.

Parameters ​

ParameterTypeDefault valueDescription
valuestring | numberundefinedThe number or numeric string to round.
decimalsnumber2Number of decimal places (defaults to 2).

Returns ​

number

The rounded number.

Example ​

ts
useRound(3.14159, 2);   // 3.14
useRound("5.678", 1);   // 5.7

useSleep() ​

ts
useSleep(ms): Promise<void>;

Defined in: src/core/services/utils.service.ts:250

Returns a promise that resolves after ms milliseconds.

Parameters ​

ParameterTypeDescription
msnumberDelay in milliseconds.

Returns ​

Promise<void>

A promise that resolves after the delay.

Example ​

ts
await useSleep(1000); // waits 1 second

useSplitConfig() ​

ts
useSplitConfig<T>(input): {
  config: ConfigData<T>;
  methods: ConfigMethodEntries<T>;
};

Defined in: src/core/services/utils.service.ts:388

Splits a define*Config literal into its config half (props and booleans) and its method half (values declared with function).

Shared by every define*Config so they all read the same way: register config, then return { config, ...methods } as a ConfigFacade.

Type Parameters ​

Type ParameterDescription
T extends objectThe literal passed to a define*Config call.

Parameters ​

ParameterTypeDescription
inputTObject literal holding props, booleans and methods.

Returns ​

ts
{
  config: ConfigData<T>;
  methods: ConfigMethodEntries<T>;
}

The config entries and the method entries, split apart.

config ​
ts
config: ConfigData<T>;
methods ​
ts
methods: ConfigMethodEntries<T>;

Example ​

ts
const { config, methods } = useSplitConfig({
  token: "abc",
  debug: true,
  send: async function () {},
});
// config  → { token: "abc", debug: true }
// methods → { send }

useUnique() ​

ts
useUnique<T>(array): T[];

Defined in: src/core/services/utils.service.ts:48

Returns a new array with duplicate values removed, preserving first-seen order.

Type Parameters ​

Type ParameterDescription
TElement type.

Parameters ​

ParameterTypeDescription
arrayT[]The input array.

Returns ​

T[]

A new array with unique values.

Example ​

ts
useUnique([1, 2, 2, 3, 1]); // [1, 2, 3]
useUnique(["a", "b", "a"]); // ["a", "b"]

getInstance() ​

ts
static getInstance(): UtilsService;

Defined in: src/core/services/utils.service.ts:28

Obtiene la instancia única del UtilsService (Singleton).

Returns ​

UtilsService

La instancia única de UtilsService.

Example ​

ts
const utils = UtilsService.getInstance();
utils.useUnique([1, 2, 2, 3, 1]);

Released under the MIT License.