0.0.4 • Published 7 months ago

@luwio/utilities v0.0.4

Weekly downloads
-
License
ISC
Repository
-
Last release
7 months ago

This project provides a set of utility functions to perform various type checks and transformations.

Functions

isNumeric

Checks if the given value is numeric.

Parameters:

  • value: The value to check.

Returns:

  • boolean: True if the value is numeric, false otherwise.
import { isNumeric } from "@/index";

console.log(isNumeric(123)); // true
console.log(isNumeric("123")); // true
console.log(isNumeric("abc")); // false
console.log(isNumeric(null)); // false

isBoolean

Checks if the given value is a boolean.

Parameters:

  • value: The value to check.

Returns:

  • boolean: True if the value is a boolean, false otherwise.
import { isBoolean } from "@/index";

console.log(isBoolean(true)); // true
console.log(isBoolean(false)); // true
console.log(isBoolean("true")); // false
console.log(isBoolean(0)); // false
console.log(isBoolean(1)); // false

isNil

Checks if the given value is null or undefined.

Parameters:

  • value: The value to check.

Returns:

  • boolean: True if the value is null or undefined, false otherwise.
import { isNil } from "@/index";

console.log(isNil(null)); // true   
console.log(isNil(undefined)); // true  
console.log(isNil(0)); // false

isNull

Checks if the given value is null.

Parameters:

  • value: The value to check.

Returns:

  • boolean: True if the value is null, false otherwise.
import { isNull } from "@/index";

console.log(isNull(null)); // true
console.log(isNull(undefined)); // false

isString

Checks if the given value is a string.

Parameters:

  • value: The value to check.

Returns:

  • boolean: True if the value is a string, false otherwise.
import { isString } from "@/index";

console.log(isString("abc")); // true
console.log(isString(123)); // false

isUndef

Checks if the given value is undefined.

Parameters:

  • value: The value to check.

Returns:

  • boolean: True if the value is undefined, false otherwise.
import { isUndef } from "@/index";

console.log(isUndef(undefined)); // true
console.log(isUndef(null)); // false

isFunction

Checks if the given value is a function.

Parameters:

  • value: The value to check.

Returns:

  • boolean: True if the value is a function, false otherwise.
import { isFunction } from "@/index";

console.log(isFunction(() => {})); // true
console.log(isFunction("abc")); // false

isObject

Checks if the given value is an object.

Parameters:

  • value: The value to check.

Returns:

  • boolean: True if the value is an object, false otherwise.
import { isObject } from "@/index";

console.log(isObject({})); // true
console.log(isObject([])); // true
console.log(isObject([], true)); // false

console.log(isObject("abc")); // false

is

Checks if the given value matches the specified type.

Parameters:

  • value: The value to check.
  • predicate: The predicate function to check the type.

Returns:

  • boolean: True if the value matches the type, false otherwise.
import { is } from "@/index";

console.log(is<number>(123, isNumber)); // true
console.log(is<string>("abc", isString)); // true
console.log(is<number>("abc", isNumber)); // false

isNumber

Checks if the given value is a number.

Parameters:

  • value: The value to check.

Returns:

  • boolean: True if the value is a number, false otherwise.
import { isNumber } from "@/index";

console.log(isNumber(123)); // true
console.log(isNumber("123")); // false

hasProp

Checks if the given object has the specified property.

Parameters:

  • obj: The object to check.
  • prop: The property to check for.

Returns:

  • boolean: True if the object has the property, false otherwise.
import { hasProp } from "@/index";

console.log(hasProp({ a: 1 }, "a")); // true
console.log(hasProp({ a: 1 }, "b")); // false

hasPropOfType

Checks if the given object has the specified property of the specified type.

Parameters:

  • obj: The object to check.
  • prop: The property to check for.
  • type: The type to check for.

Returns:

  • boolean: True if the object has the property of the specified type, false otherwise.
import { hasPropOfType } from "@/index";

console.log(hasPropOfType({ a: 1 }, "a", "number")); // true
console.log(hasPropOfType({ a: 1 }, "a", "string")); // false

StringToArray

Converts a string to an array, splitting by the specified delimiter.

Parameters:

  • items: The string to convert.
  • split: The delimiter to split by.
  • callback: Optional callback to transform each item.

Returns:

  • T[]: The resulting array.
import { StringToArray } from "@/index";

console.log(StringToArray("a,b,c", ",")); // ["a", "b", "c"]
console.log(StringToArray("a,b,c", ",", (item) => item.toUpperCase())); // ["A", "B", "C"]

NameOf

Gets the name of the specified property.

Parameters:

  • prop: The property to get the name of.

Returns:

  • string: The name of the property.
import { NameOf } from "@/index";

console.log(NameOf("a")); // "a"
console.log(NameOf(123)); // "123"

UniqueBy

Removes duplicate objects from an array based on a specified property.

Parameters:

  • array: The array to process.
  • key: The property to check for uniqueness.

Returns:

  • T[]: The array with unique objects.
import { UniqueBy } from "@/index";

console.log(UniqueBy([{ id: 1 }, { id: 2 }, { id: 1 }], "id")); // [{ id: 1 }, { id: 2 }]

ChunkArray

Splits an array into chunks of a specified size.

Parameters:

  • array: The array to split.
  • size: The size of each chunk.

Returns:

  • T[][]: The array split into chunks.
import { ChunkArray } from "@/index";

console.log(ChunkArray([1, 2, 3, 4, 5], 2)); // [[1, 2], [3, 4], [5]]

DeepClone

Creates a deep clone of the given object.

Parameters:

  • obj: The object to clone.

Returns:

  • T: The deep-cloned object.
import { DeepClone } from "@/index";

const input = { a: 1, b: { c: 2 } };
const output = DeepClone(input);
console.log(output); // { a: 1, b: { c: 2 } }
console.log(output !== input); // true

DeepMerge

Merges two objects deeply.

Parameters:

  • obj1: The first object.
  • obj2: The second object.

Returns:

  • T: The merged object.
import { DeepMerge } from "@/index";

const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { b: { d: 3 }, e: 4 };
console.log(DeepMerge(obj1, obj2)); // { a: 1, b: { c: 2, d: 3 }, e: 4 }

GroupBy

Groups an array of objects by a specified property.

Parameters:

  • array: The array to group.
  • key: The property to group by.

Returns:

  • Record<string, T[]>: The grouped object.
import { GroupBy } from "@/index";

const input = [{ category: "A" }, { category: "B" }, { category: "A" }];
console.log(GroupBy(input, "category")); 
// { A: [{ category: "A" }, { category: "A" }], B: [{ category: "B" }] }

Sleep

Pauses execution for a specified duration.

Parameters:

  • ms: The duration to sleep in milliseconds.

Returns:

  • Promise<void>: A promise that resolves after the specified duration.
import { Sleep } from "@/index";

const start = Date.now();
await Sleep(100);
const end = Date.now();
console.log(end - start >= 100); // true

Capitalize

Capitalizes the first letter of the given string.

Parameters:

  • value: The string to capitalize.

Returns:

  • string: The capitalized string.
import { Capitalize } from "@/index";

console.log(Capitalize("hello")); // "Hello"
console.log(Capitalize("Hello")); // "Hello"
console.log(Capitalize("hELLO")); // "HELLO"
console.log(Capitalize("")); // ""

Debounce

Creates a debounced function that delays invoking the provided function until after the specified wait time has elapsed since the last time the debounced function was invoked.

Parameters:

  • func: The function to debounce.
  • wait: The number of milliseconds to delay.

Returns:

  • Function: The debounced function.
import { Debounce } from "@/index";

let counter = 0;
const increment = () => {
  counter++;
};
const debouncedIncrement = Debounce(increment, 100);

debouncedIncrement();
debouncedIncrement();
debouncedIncrement();

setTimeout(() => {
  console.log(counter); // 1
}, 150);

FlattenedValue

Gets the value of a nested property in an object using a dot-separated path.

Parameters:

  • obj: The object to query.
  • separator: The separator used in the path.
  • path: The path of the property to get.

Returns:

  • T | undefined: The value of the nested property, or undefined if the property does not exist.
import { FlattenedValue } from "@/index";

const input = { a: { b: { c: 1 } }, d: 2 };
console.log(FlattenedValue<number>(input, '.', 'a.b.c')); // 1
console.log(FlattenedValue<number>(input, '.', 'd')); // 2
console.log(FlattenedValue<number>(input, '.', 'a.b')); // { c: 1 }
console.log(FlattenedValue<number>(input, '.', 'a.b.x')); // undefined

PickKeys

Selects specified keys from an object and returns a new object with only those keys.

Parameters:

  • obj: The object to pick keys from.
  • keys: An array of keys to pick.

Returns:

  • Partial<T>: A new object with only the specified keys.
import { PickKeys } from "@/index";

const input = { a: 1, b: 2, c: 3 };
const keys = ["a", "c"];
console.log(PickKeys(input, keys)); // { a: 1, c: 3 }