Skip to content

Commit

Permalink
chore(utils): Move generic utils from region module to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
sradevski committed Feb 21, 2024
1 parent 269be1b commit 1deedc7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 24 deletions.
28 changes: 4 additions & 24 deletions packages/region/src/services/region-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import {
MedusaError,
ModulesSdkUtils,
promiseAll,
DefaultsUtils,
removeUndefined,
getDuplicates,
} from "@medusajs/utils"

import { Country, Currency, Region } from "@models"

import { DefaultsUtils } from "@medusajs/utils"
import { CreateCountryDTO, CreateCurrencyDTO } from "@types"
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"

Expand Down Expand Up @@ -304,7 +306,7 @@ export default class RegionModuleService<
if (uniqueCountries.length !== countries.length) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Countries with codes: "${getDuplicateEntries(countries).join(
`Countries with codes: "${getDuplicates(countries).join(
", "
)}" are already assigned to a region`
)
Expand Down Expand Up @@ -413,25 +415,3 @@ export default class RegionModuleService<
}
}
}

// microORM complains if undefined fields are present in the passed data object
const removeUndefined = <T extends Record<string, any>>(obj: T): T => {
return Object.fromEntries(
Object.entries(obj).filter(([_, v]) => v !== undefined)
) as T
}

const getDuplicateEntries = (collection: string[]): string[] => {
const uniqueElements = new Set()
const duplicates: string[] = []

collection.forEach((item) => {
if (uniqueElements.has(item)) {
duplicates.push(item)
} else {
uniqueElements.add(item)
}
})

return duplicates
}
20 changes: 20 additions & 0 deletions packages/utils/src/common/__tests__/get-duplicates.spec.ts
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 packages/utils/src/common/__tests__/remove-undefined.spec.ts
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({})
})
})
16 changes: 16 additions & 0 deletions packages/utils/src/common/get-duplicates.ts
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)
}
2 changes: 2 additions & 0 deletions packages/utils/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ export * from "./to-pascal-case"
export * from "./transaction"
export * from "./upper-case-first"
export * from "./wrap-handler"
export * from "./remove-undefined"
export * from "./get-duplicates"
6 changes: 6 additions & 0 deletions packages/utils/src/common/remove-undefined.ts
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
}

0 comments on commit 1deedc7

Please sign in to comment.