UNPKG

@js-data-tools/js-helpers

Version:

A set of JavaScript / TypeScript helper functions for parsing, converting, transforming and formatting data.

154 lines (153 loc) 6.83 kB
export type Predicate<T> = (value: T) => boolean; /** * A predicate function, always returning true, regardless of the input value. * * @since 0.1.2 * @category predicates * @param {*} input - The input value (ignored) * @returns true */ export declare function alwaysTrue<T>(input: T): boolean; /** * Check if the given value is undefined. * * @since 0.3.0 * @category predicates * @param input - The value to inspect. * @returns {boolean} true if the input parameter is undefined, otherwise false. */ export declare function isUndefined<T>(input: T | undefined): boolean; /** * Check if the given value is null or undefined. * * @since 0.1.2 * @category predicates * @param input - The value to inspect. * @returns {boolean} true if the input parameter is null or undefined, otherwise false. */ export declare function isNullOrUndefined<T>(input: T | null | undefined): boolean; /** * Check if the given value is not null and not undefined. This function can also be used as a type predicate. * * @since 0.1.2 * @category predicates * @param input - The value to inspect. * @returns {boolean} false if the input value is null or undefined, otherwise true. */ export declare function notNullOrUndefined<T>(input: T | null | undefined): input is T; /** * Check if the given array is null / undefined or contains no elements. * * @since 0.1.2 * @category predicates * @param {Array|null|undefined} input - The array object to inspect. * @returns {boolean} true if the given array is null, undefined or contains no elements. */ export declare function isEmptyArray<T>(input: Array<T> | null | undefined): boolean; /** * Check if the given array is not null / undefined and contains at least one element. This function can also be used as a type predicate. * * @since 0.1.2 * @category predicates * @param {Array|null|undefined} input - The array object to inspect. * @returns {boolean} true if the given array exists (not null / undefined) and contains at least one element. */ export declare function isNonEmptyArray<T>(input: Array<T> | null | undefined): input is Array<T>; /** * Check if the given set is null / undefined or contains no elements. * * @since 0.1.2 * @category predicates * @param {Set|null|undefined} input - The set object to inspect. * @returns {boolean} true if given set is null, undefined or contains no elements. */ export declare function isEmptySet<T>(input: Set<T> | null | undefined): boolean; /** * Check if the given set is not null / undefined and contains at least one element. This function can also be used as a type predicate. * * @since 0.1.2 * @category predicates * @param {Set|null|undefined} input - The set object to inspect. * @returns {boolean} true if the given set is not null / undefined and contains at least one element. */ export declare function isNonEmptySet<T>(input: Set<T> | null | undefined): input is Set<T>; /** * Check if the given map is null / undefined or contains no elements. * * @since 0.1.2 * @category predicates * @param {Map|null|undefined} input - The map object to inspect. * @returns {boolean} true if the given map object is null, undefined or contains no elements. */ export declare function isEmptyMap<K, V>(input: Map<K, V> | null | undefined): boolean; /** * Check if the given map is not null / undefined and contains at least one element. This function can also be used as a type predicate. * * @since 0.1.2 * @category predicates * @param {Map|null|undefined} input - The map object to inspect. * @returns {boolean} true if the given map is not null / undefined and contains at least one element. */ export declare function isNonEmptyMap<K, V>(input: Map<K, V> | null | undefined): input is Map<K, V>; /** * Check if the given object is null / undefined or contains no public properties. * * @since 0.1.2 * @category predicates * @param input - The object to inspect. * @returns {boolean} true if the given object is null, undefined or contains no public properties. */ export declare function isEmptyObject<T extends object>(input: T | null | undefined): boolean; /** * Check if the given object is not null / undefined and contains at least one public property. This function can also be used as a type predicate. * * @since 0.1.2 * @category predicates * @param input - The object to inspect. * @returns {boolean} true if the given object is not null / undefined and contains at least one property. */ export declare function isNonEmptyObject<T extends object>(input: T | null | undefined): input is T; /** * Checks if given value is empty (usually to omit it from serialization). Empty values are null, undefined, empty string, empty array, empty object. * @param value - The value to inspect * @returns true if value is empty; otherwise false. * @category predicates * @since 0.3.0 */ export declare function isEmptyValue(value: unknown): boolean; /** * Checks if given value is default and can be safely omitted from serialization. Empty values are null, undefined, empty string, * empty array, empty object, zero (numeric), false (boolean). * @param value - The value to inspect * @returns true if value is empty; otherwise false. * @category predicates * @since 0.3.0 */ export declare function isDefaultValue(value: unknown): boolean; /** * Creates a new predicate function, checking if an input value satisfies all given conditions (a logical AND between other predicates). * * @since 0.1.2 * @category predicates * @param predicates - The list of condition functions to combine with a logical AND. * @returns A new {@link Predicate}, which returns true if all inner conditions are met for the given input value. */ export declare function and<T>(...predicates: Array<Predicate<T> | null | undefined>): Predicate<T>; /** * Creates a new predicate function, checking if an input value satisfies any of the given conditions (a logical OR between other predicates). * * @since 0.1.2 * @category predicates * @param predicates - A list of functions, checking the "inner" conditions. The "outer" predicate will return true if at least one of inner conditions is met. * @returns A new {@link Predicate}, which evaluate given conditions on an input value and return true if at least one of inner conditions is met. */ export declare function or<T>(...predicates: Array<Predicate<T> | null | undefined>): Predicate<T>; /** * Creates a new predicate function, which will negate the given one. * * @since 0.1.2 * @category predicates * @param predicate - The "inner" predicate to negate. * @returns A new {@link Predicate}, which evaluates the given condition and returns the negated value (e.g. false if the inner condition is true and vice versa). */ export declare function not<T>(predicate: Predicate<T>): Predicate<T>;