• Filters the provided data based on a predicate applied to its values. The resulting object will only include properties whose values satisfy the predicate function. Properties with null or undefined values can be optionally excluded based on the 'excludeNullUndefined' flag.

    Type Parameters

    • T extends object

      The type of the data to be filtered, constrained to an object type.

    Parameters

    • data: Readonly<T>

      The data object to be filtered.

    • predicate: (value: T[keyof T]) => boolean

      The predicate function to apply to the values.

    • excludeNullUndefined: boolean = false

      Flag to determine if properties with null or undefined values should be excluded. Default is false.

    Returns T

    The filtered data object with properties satisfying the predicate. (T)

    (BasaltError) - Throws an error if the data is null or undefined. (CORE_DATA_KEY_ERROR.DATA_IS_NULL)

    (BasaltError) - Throws an error if the data is not a plain object. (CORE_DATA_KEY_ERROR.DATA_MUST_BE_OBJECT)

    const object = { test: 'test', exclude: 'exclude' };
    const filtered = filterByValue(object, (value: unknown): boolean => value === 'test');
    console.log(filtered); // { test: 'test' }
    const object = { test: 'test', exclude: null };
    const filtered = filterByValue(object, (value: unknown): boolean => value === 'test', true);
    console.log(filtered); // { test: 'test' }