Coder Social home page Coder Social logo

How to map moment to number? about mapper HOT 11 CLOSED

nartc avatar nartc commented on September 2, 2024
How to map moment to number?

from mapper.

Comments (11)

krukru avatar krukru commented on September 2, 2024 1

Hey, it is not crucial :)
I don't really like moment due to the way it is written, but decided to go with it since it seemed most used/popular. Maybe it is time to reconsider :)
Thanks anyway, I am closing the issue (and maybe it would be worth noting that moment is not supported)

from mapper.

nartc avatar nartc commented on September 2, 2024 1

@krukru plainToClass from class-transformer initializes a new Moment object under the hood. That's a bummer. I don't think Moment is working with this library as of the moment.

from mapper.

nartc avatar nartc commented on September 2, 2024

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.

from mapper.

nartc avatar nartc commented on September 2, 2024

@krukru Though it is the most popular one, it’s a heavy library.

  • Luxon: is written using Moment’s concept but is redesigned to be lighter and less-clunky than Moment, written by one of the old Moment’s maintainer
  • date-fns: like Lodash for Date
  • dayjs: extremely lightweight with plugin to enable various features.

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

from mapper.

nartc avatar nartc commented on September 2, 2024

@krukru I found a workaround for Moment object. Read the Date Time section on the README to learn about it. Though, I would still suggest going with vanilla JS Date object if you can.

from mapper.

krukru avatar krukru commented on September 2, 2024

Hey, thanks for updating the docs, I almost got this to work after updating to 3.1.2 :)
Here is what I am working with

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
But mapping Domain model -> Dto fails and always creates a moment object with the current timestamp. Now that's really odd, isn't it! :)

from mapper.

krukru avatar krukru commented on September 2, 2024

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!

from mapper.

nartc avatar nartc commented on September 2, 2024

hmmm I'll give that a quick run on my test bed.

from mapper.

nartc avatar nartc commented on September 2, 2024

@krukru Another point is I run plainToClass against the source (that was passed in Mapper.map(source, DestinationModel)) every time. I just modified the code to where plainToClass is only ran if source isn't a true instance of mapping.source (aka if source is a plain object). If source is a true instance of mapping.source, then it seems like moment is working.

Let me know (if you're still interested) if the latest version works out for you.

from mapper.

krukru avatar krukru commented on September 2, 2024

I don't know what you did, but in 3.1.2 this now works in both directions!
Thanks very much for all the support, you are awesome :)

from mapper.

nartc avatar nartc commented on September 2, 2024

@krukru I simply changed the way Automapper interacts with class-transformer a bit. Thanks for reporting.

from mapper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.