UNPKG

codetrix

Version:

A lightweight lodash-style utility library

69 lines (68 loc) 1.93 kB
/** * Checks if a value is null or undefined. * @param value - The value to check. * @returns True if the value is null or undefined. */ export function isNil(value) { return value === null || value === undefined; } /** * Returns one of two values based on a condition. * @param condition - Boolean condition to evaluate. * @param ifTrue - Value to return if condition is true. * @param ifFalse - Value to return if condition is false. * @returns The selected value. */ export function ifElse(condition, ifTrue, ifFalse) { return condition ? ifTrue : ifFalse; } /** * Checks if a value is a function. * @param value - The value to check. * @returns True if it's a function. */ export function isFunction(value) { return typeof value === 'function'; } /** * Checks if a value is a plain object. * @param value - The value to check. * @returns True if it's a non-null object and not an array. */ export function isObject(value) { return typeof value === 'object' && value !== null && !Array.isArray(value); } /** * Checks if the given value is a boolean. * @param value - The value to check. * @returns True if it's a boolean. */ export function isBoolean(value) { return typeof value === 'boolean'; } /** * Checks if a value is a Promise. * @param value - The value to check. * @returns True if it's a Promise. */ export function isPromise(value) { return value instanceof Promise || (value !== null && typeof value === 'object' && typeof value.then === 'function'); } /** * Creates a function that is invoked only once. * Useful for initialization logic. * @param fn - The function to wrap. * @returns A new function that runs only once. */ export function once(fn) { let called = false; let result; return function (...args) { if (!called) { called = true; result = fn(...args); } return result; }; }