feat(prduct, utils, types): Create soft delete pattern for link module (#4649)

* feat(prouct, utils, types): Create soft delete pattern for link module

* add comment

* add comment

* finalise

* remove linkable keys

* cleanup and tests

* cleanup

* add some comments and renaming

* re work

* fix tests

---------

Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>
This commit is contained in:
Adrien de Peretti
2023-08-02 19:29:01 +02:00
committed by GitHub
co-authored by Riqwan Thamir
parent fc6c9df035
commit ce3326c5fb
17 changed files with 448 additions and 166 deletions
@@ -0,0 +1,43 @@
import { mapObjectTo, MapToConfig } from "../map-object-to"
const input = {
a: [{ id: "1" }, { id: "2" }],
b: [{ id: "3" }, { id: "4", handle: "handle1" }],
c: [{ id: "5", sku: "sku1" }, { id: "6" }],
}
const mapToConfig: MapToConfig = {
a: [{ mapTo: "a.id", valueFrom: "id" }],
b: [
{ mapTo: "b.id", valueFrom: "id" },
{ mapTo: "b.handle", valueFrom: "handle" },
],
c: [
{ mapTo: "c.id", valueFrom: "id" },
{ mapTo: "c.sku", valueFrom: "sku" },
],
}
describe("mapObjectTo", function () {
it("should return a new object with the keys remapped and the values picked from the original object based on the map config", function () {
const remappedObject = mapObjectTo(input, mapToConfig)
expect(remappedObject).toEqual({
"a.id": ["1", "2"],
"b.id": ["3", "4"],
"b.handle": ["handle1"],
"c.id": ["5", "6"],
"c.sku": ["sku1"],
})
})
it("should return a new object with only the picked properties", function () {
const remappedObject = mapObjectTo(input, mapToConfig, {
pick: ["a.id"],
})
expect(remappedObject).toEqual({
"a.id": ["1", "2"],
})
})
})
+1
View File
@@ -22,3 +22,4 @@ export * from "./stringify-circular"
export * from "./to-kebab-case"
export * from "./to-pascal-case"
export * from "./wrap-handler"
export * from "./map-object-to"
@@ -0,0 +1,53 @@
type RemapInputObject = Record<string, unknown[]>
type RemapConfig = { mapTo: string; valueFrom: string }
export type MapToConfig = {
[key: string]: RemapConfig[]
}
/**
* Create a new object with the keys remapped and the values picked from the original object based
* on the map config
*
* @param object input object
* @param mapTo configuration to map the output object
* @param removeIfNotRemapped if true, the keys that are not remapped will be removed from the output object
* @param pick if provided, only the keys in the array will be picked from the output object
*/
export function mapObjectTo<
TResult = any,
T extends RemapInputObject = RemapInputObject
>(
object: T,
mapTo: MapToConfig,
{
removeIfNotRemapped,
pick,
}: { removeIfNotRemapped?: boolean; pick?: string[] } = {}
): TResult {
removeIfNotRemapped ??= false
const newObject: Record<string, any> = {}
for (const key in object) {
const remapConfig = mapTo[key as string]!
if (!remapConfig) {
if (!removeIfNotRemapped) {
newObject[key] = object[key]
}
continue
}
remapConfig.forEach((config) => {
if (pick?.length && !pick.includes(config.mapTo)) {
return
}
newObject[config.mapTo] = object[key]
.map((obj: any) => obj[config.valueFrom])
.filter(Boolean)
})
}
return newObject as TResult
}