Skip to content

Commit

Permalink
refactor: change functionality to omitBy
Browse files Browse the repository at this point in the history
  • Loading branch information
behzadam committed Dec 6, 2022
1 parent 9dc28fe commit 50fd073
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export { isObjectLike } from "./is-object-like";
export { isSet } from "./is-set";
export { isString } from "./is-string";
export { isNull } from "./is-null";
export { removeEmptyKeys } from "./remove-empty-keys";
export { omitBy } from "./omit-by";
export { toChunks } from "./to-chunks";
export { indexBy } from "./index-by";
export { wrapInArray } from "./wrap-in-array";
Expand Down
21 changes: 21 additions & 0 deletions src/omit-by.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { omitBy } from ".";

describe("omitBy cases", () => {
const input = {
key1: "1",
key2: "2",
key3: "",
key4: { key5: "" },
};
const expected = {
key1: "1",
key2: "2",
key4: {},
};
test.each`
input | expected
${input} | ${expected}
`("should return $expected when input is: $input", ({ input, expected }) => {
expect(omitBy(input, (item) => item !== null)).toStrictEqual(expected);
});
});
10 changes: 10 additions & 0 deletions src/omit-by.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Dictionary, PredicateFunction } from "./types";

export function omitBy<T extends Dictionary<T>>(
object: T,
fun: PredicateFunction
): Dictionary<T> {
return Object.keys(object)
.filter((key) => !fun(object[key], key))
.reduce((acc, key) => ((acc[key] = object[key]), acc), {} as Dictionary<T>);
}
11 changes: 0 additions & 11 deletions src/remove-empty-keys.test.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/remove-empty-keys.ts

This file was deleted.

0 comments on commit 50fd073

Please sign in to comment.