• Filters the provided data by including only the specified keys. The resulting object will only have properties that match the keys provided. Properties with null or undefined values can optionally be excluded based on the 'excludeNullUndefined' flag.

    Type Parameters

    • T extends object

      The type of the data object to filter, must be an object.

    Parameters

    • data: Readonly<T>

      The data object to be filtered.

    • keys: readonly (keyof T)[]

      The array of keys to include in the resulting data object. (Can be empty)

    • excludeNullUndefined: boolean = false

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

    Returns T

    The filtered data object with only the specified keys included. (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 = filterByKeyInclusion(object, ['test']);
    console.log(filtered); // { test: 'test' }
    const object = { test: 'test', exclude: null };
    const filtered = filterByKeyInclusion(object, ['test'], true);
    console.log(filtered); // { test: 'test' }