-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(utils): Move generic utils from region module to utils
- Loading branch information
Showing
6 changed files
with
70 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/utils/src/common/__tests__/get-duplicates.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { getDuplicates } from "../get-duplicates" | ||
|
||
describe("getDuplicates", function () { | ||
it("should return an empty array if there are no duplicates", function () { | ||
const output = getDuplicates(["foo", "bar", "baz"]) | ||
expect(output).toHaveLength(0) | ||
}) | ||
|
||
it("should return a singular duplicate", function () { | ||
const output = getDuplicates(["foo", "bar", "baz", "baz", "baz"]) | ||
expect(output).toHaveLength(1) | ||
expect(output[0]).toEqual("baz") | ||
}) | ||
|
||
it("should return all duplicates in the array", function () { | ||
const output = getDuplicates(["foo", "bar", "bar", "baz", "baz", "baz"]) | ||
expect(output).toHaveLength(2) | ||
expect(output).toEqual(expect.arrayContaining(["baz", "bar"])) | ||
}) | ||
}) |
22 changes: 22 additions & 0 deletions
22
packages/utils/src/common/__tests__/remove-undefined.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { removeUndefined } from "../remove-undefined" | ||
|
||
describe("removeUndefined", function () { | ||
it("should remove all undefined fields from an object", function () { | ||
const withUndefined = { | ||
foo: undefined, | ||
bar: "baz", | ||
foo2: null, | ||
} | ||
expect(withUndefined.hasOwnProperty("foo")).toBe(true) | ||
|
||
const output = removeUndefined(withUndefined) | ||
expect(output.hasOwnProperty("foo")).toBe(false) | ||
expect(output.hasOwnProperty("bar")).toBe(true) | ||
expect(output.hasOwnProperty("foo2")).toBe(true) | ||
}) | ||
|
||
it("should return an empty object as-is", function () { | ||
const output = removeUndefined({}) | ||
expect(output).toEqual({}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// This function is intentionally not generic, as we will likely need a comparator function in that case, which will make it more complex than necessary | ||
// Revisit if there is such use-case in the future | ||
export const getDuplicates = (collection: string[]): string[] => { | ||
const uniqueElements = new Set<string>() | ||
const duplicates = new Set<string>() | ||
|
||
collection.forEach((item) => { | ||
if (uniqueElements.has(item)) { | ||
duplicates.add(item) | ||
} else { | ||
uniqueElements.add(item) | ||
} | ||
}) | ||
|
||
return Array.from(duplicates) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// useful in cases where presence of undefined is not desired (eg. in microORM operations) | ||
export const removeUndefined = <T extends Record<string, any>>(obj: T): T => { | ||
return Object.fromEntries( | ||
Object.entries(obj).filter(([_, v]) => v !== undefined) | ||
) as T | ||
} |