|
import { Expose, instanceToPlain, plainToInstance } from "class-transformer"; |
|
import { ToMap } from "./src"; |
|
|
|
class Item { |
|
constructor(prop: number) { |
|
this.prop = prop; |
|
} |
|
|
|
@Expose() |
|
prop!: number; |
|
} |
|
|
|
class TestNumberModel { |
|
@Expose() |
|
@ToMap({ mapValueClass: Number }) |
|
prop!: Map<string, number>; |
|
} |
|
|
|
class TestStringModel { |
|
@Expose() |
|
@ToMap({ mapValueClass: String }) |
|
prop!: Map<string, string>; |
|
} |
|
|
|
class TestBooleanModel { |
|
@Expose() |
|
@ToMap({ mapValueClass: Boolean }) |
|
prop!: Map<string, boolean>; |
|
} |
|
|
|
class TestItemModel { |
|
@Expose() |
|
@ToMap({ mapValueClass: Item }) |
|
prop!: Map<string, Item>; |
|
} |
|
|
|
describe(ToMap.name, () => { |
|
describe("should work with Number value class", () => { |
|
const plain = { |
|
prop: { |
|
"1": 11, |
|
"2": 22, |
|
}, |
|
}; |
|
|
|
it("should transform Record<string, number> to Map<string, number>", () => { |
|
return expect(plainToInstance(TestNumberModel, plain).prop).toEqual( |
|
(new TestNumberModel().prop = new Map([ |
|
["1", 11], |
|
["2", 22], |
|
])) |
|
); |
|
}); |
|
|
|
it("should transform Map<string, number> to Record<string, number>", () => { |
|
return expect(instanceToPlain(plainToInstance(TestNumberModel, plain))).toEqual(plain); |
|
}); |
|
}); |
|
|
|
describe("should work with String value class", () => { |
|
const plain = { |
|
prop: { |
|
"1": "11", |
|
"2": "22", |
|
}, |
|
}; |
|
|
|
it("should transform Record<string, string> to Map<string, string>", () => { |
|
return expect(plainToInstance(TestStringModel, plain).prop).toEqual( |
|
(new TestStringModel().prop = new Map([ |
|
["1", "11"], |
|
["2", "22"], |
|
])) |
|
); |
|
}); |
|
|
|
it("should transform Map<string, string> to Record<string, string>", () => { |
|
return expect(instanceToPlain(plainToInstance(TestStringModel, plain))).toEqual(plain); |
|
}); |
|
}); |
|
|
|
describe("should work with Boolean value class", () => { |
|
const plain = { |
|
prop: { |
|
"1": true, |
|
"2": false, |
|
}, |
|
}; |
|
|
|
it("should transform Record<string, boolean> to Map<string, boolean>", () => { |
|
return expect(plainToInstance(TestBooleanModel, plain).prop).toEqual( |
|
(new TestBooleanModel().prop = new Map([ |
|
["1", true], |
|
["2", false], |
|
])) |
|
); |
|
}); |
|
|
|
it("should transform Map<string, boolean> to Record<string, boolean>", () => { |
|
return expect(instanceToPlain(plainToInstance(TestBooleanModel, plain))).toEqual(plain); |
|
}); |
|
}); |
|
|
|
describe("should work with custom value class", () => { |
|
const plain = { |
|
prop: { |
|
"1": { prop: 11 }, |
|
"2": { prop: 22 }, |
|
}, |
|
}; |
|
|
|
it("should transform Record<string, Item> to Map<string, Item>", () => { |
|
return expect(plainToInstance(TestItemModel, plain).prop).toEqual( |
|
(new TestItemModel().prop = new Map([ |
|
["1", new Item(11)], |
|
["2", new Item(22)], |
|
])) |
|
); |
|
}); |
|
|
|
it("should transform Map<string, Item> to Record<string, Item>", () => { |
|
return expect(instanceToPlain(plainToInstance(TestItemModel, plain))).toEqual(plain); |
|
}); |
|
}); |
|
}); |
Thanks for this gist. I tried your gist but unfortunately it doesn't work in the
CLASS_TO_PLAINcase. It only converts the map to string property keys with an empty object as property value.transformedObjectcontaints the correct object structure though but it seems it doesn't end up correctly in the resulting JSON.The JSON looks like:
{"myMap":{"5533034343435190C072":{}}}The
PLAIN_TO_CLASScase works perfectly.EDIT
It was because I set the
{ strategy: 'excludeAll' }option.