@basalt-lab/basalt-helper
    Preparing search index...

    Function filterByKeyInclusion

    • Filters the provided data by including only the specified keys, with an option to exclude null or undefined values. Uses a generic type parameter to control the return type based on the excludeNullUndefined flag.

      Type Parameters

      • TObject extends object

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

      • TIncludedKeys extends string | number | symbol = never

        The keys to include from the data object.

      • TExcludeNullUndefined extends boolean = false

        Boolean flag type to determine if properties with null or undefined values should be excluded.

      Parameters

      • data: Readonly<TObject>

        The data object to be filtered.

      • keys: readonly TIncludedKeys[]

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

      • OptionalexcludeNullUndefined: TExcludeNullUndefined

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

      Returns TExcludeNullUndefined extends true
          ? Partial<Pick<TObject, TIncludedKeys>>
          : Pick<TObject, TIncludedKeys>

      The filtered data object with only the specified keys included. (TObject)

      (BasaltError) - Throws an error if the data is null or undefined. (dataErrorKeys.dataIsNull)

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

      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' }