-
-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to map moment to number? #60
Comments
Hi, thanks for reporting the issue. I thought about Moment object but ended up not supporting it since it would mean I would need to support another 3rd party library and there are many different choices regarding Date now. If I could suggest, try: date-fns, dayjs or luxon. However, if Moment is crucial for your project, I’ll see what I can do. |
Hey, it is not crucial :) |
@krukru Though it is the most popular one, it’s a heavy library.
I’ve used date-fns and dayjs before and they’re great. Working with native JS Date object is awesome with those libs. I would definitely reconsider and will write a small section regarding DateTime on the Readme. Thanks again |
@krukru I found a workaround for |
Hey, thanks for updating the docs, I almost got this to work after updating to 3.1.2 :) import "reflect-metadata";
import { AutoMap, Mapper } from "@nartc/automapper";
import { Moment } from "moment";
const moment = require("moment");
class Foo {
@AutoMap(() => moment)
public get foo(): Moment {
return this._foo;
}
public set foo(value: Moment) {
this._foo = value;
}
private _foo!: Moment;
}
class FooDTO {
@AutoMap()
public foo!: number; // the unix timestamp
}
Mapper.initialize(cfg => {
cfg.createMap(FooDTO, Foo).forMember(
(dest: Foo) => dest.foo,
(opts: any) => opts.mapFrom((source) => moment(source.foo)),
).reverseMap().forPath(
(dest: FooDTO) => dest.foo,
(opts: any) => opts.mapFrom((source) => source.foo.unix()))
});
const fooDomainObjectMapped = Mapper.map({ foo: 12345 * 1000}, Foo, FooDTO);
expect(fooDomainObjectMapped.foo.unix()).equals(12345); // this passes as true!
const fooDomainObject = new Foo();
fooDomainObject.foo = moment(54321 * 1000);
const fooDtoObjectMapped = Mapper.map(fooDomainObject, FooDTO);
expect(fooDtoObjectMapped.foo).equals(54321); // this fails, fooDtoObjectMapped.foo is equal to the current system timestamp So mapping from Dto(Vm) -> Domain model works |
What's maybe more interesting is how this snippet works on 3.1.0 const fooDomainObjectMapped = Mapper.map({ foo: 12345 * 1000}, Foo, FooDTO);
expect(fooDomainObjectMapped.foo.unix()).equals(12345); // this fails, fooDomainObjectMapped.foo.unix() is equal to the current system timestamp const fooDtoObjectMapped = Mapper.map(fooDomainObject, FooDTO);
expect(fooDtoObjectMapped.foo).equals(54321); // this throws a runtime error: TypeError: Cannot read property '_isAMomentObject' of undefined I am trying to deduce from the source and commits between versions what has changed, but it is a bit over my head. Hope this piece of info helps! |
hmmm I'll give that a quick run on my test bed. |
@krukru |
@krukru Another point is I run Let me know (if you're still interested) if the latest version works out for you. |
I don't know what you did, but in 3.1.2 this now works in both directions! |
@krukru I simply changed the way Automapper interacts with |
Hi again :)
This time I am wondering how to map between moment object and number
Here is my snippet
This fails with the error "TypeError Cannot read property '_isAMomentObject' of undefined".
The issue is related to class-transformer (see typestack/class-transformer#176) but I don't know how to get this to work using your mapper lib.
The text was updated successfully, but these errors were encountered: