Skip to content

Commit

Permalink
feat(excludeObjectValues): add excludeObjectValues function
Browse files Browse the repository at this point in the history
  • Loading branch information
djcsdy committed Feb 22, 2023
1 parent 489360a commit 287d67a
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,62 @@ export function excludeObjectKeysFn<T extends object>(
return object => excludeObjectKeys(object, predicate);
}

export type ExcludeObjectValues<
TObject extends object,
TExclude extends StringKeyedValue<TObject>
> = {
[K in StringKey<TObject>]: TObject[K] extends TExclude ? never : TObject[K];
};

/** Creates a new object that contains the string-keyed properties of the
* specified object, excluded by the specified predicate. */
export function excludeObjectValues<
TObject extends object,
TExclude extends StringKeyedValue<TObject>
>(
object: Readonly<TObject>,
predicate: (value: StringKeyedValue<TObject>) => value is TExclude
): ExcludeObjectValues<TObject, TExclude>;

/** Creates a new object that contains the string-keyed properties of the
* specified object, excluded by the specified predicate. */
export function excludeObjectValues<T extends object>(
object: Readonly<T>,
predicate: (value: StringKeyedValue<T>) => boolean
): Partial<StringKeyedProperties<T>>;

export function excludeObjectValues<T extends object>(
object: Readonly<T>,
predicate: (value: StringKeyedValue<T>) => boolean
): Partial<StringKeyedProperties<T>> {
return filterObjectValues(object, value => !predicate(value));
}

/** Curried variant of {@link excludeObjectValues}.
*
* Returns a function that creates a new object that contains the string-keyed
* properties of the specified object, excluded by the specified predicate. */
export function excludeObjectValuesFn<
TObject extends object,
TExclude extends StringKeyedValue<TObject>
>(
predicate: (value: StringKeyedValue<TObject>) => value is TExclude
): (object: Readonly<TObject>) => ExcludeObjectValues<TObject, TExclude>;

/** Curried variant of {@link excludeObjectValues}.
*
* Returns a function that creates a new object that contains the string-keyed
* properties of the specified object, excluded by the specified predicate. */
export function excludeObjectValuesFn<T extends object>(
predicate: (value: StringKeyedValue<T>) => boolean
): (object: Readonly<T>) => Partial<StringKeyedProperties<T>>;

export function excludeObjectValuesFn<T extends object>(
predicate: (value: StringKeyedValue<T>) => boolean
): (object: Readonly<T>) => Partial<StringKeyedProperties<T>> {
return object => excludeObjectValues(object, predicate);
}

export function excludeNull<T>(
dictionary: Readonly<Record<string, T | undefined | null>>
): Record<string, T> {
Expand Down

0 comments on commit 287d67a

Please sign in to comment.