mapped
TypeScript icon, indicating that this package has built-in type declarations

1.1.4 • Public • Published

Mapped

Mapped is a lightweight TypeScript based library for implementing basic mapping between two objects.

Example

A basic example is shown here:

class SourceCls {
  public flag?: boolean;
  public other?: string;
}

class DestCls {
  public flag?: boolean;
  public other?: string;
}

function mapSomething(sourceObj: SourceCls): DestCls {
  const mapper = new Mapper();

  mapper.register(SourceCls, DestCls)
      .forMember((dest: DestCls) => dest.flag, (src: SourceCls) => src.flag)
      .forMember((dest: DestCls) => dest.other, (src: SourceCls) => src.other);

  const destObj = mapper.map(sourceObj, DestCls);

  return destObj;
}

Child objects will be created with all mappings provided; but types will not be created from their constructors (with this being JavaScript the type cannot be obtained). Anything can be returned from the mapped source object - including a new object.

Examples of both these cases are shown here. dest.child will be created with dest.child.further being a straight forward map from src.param. dest.child.random will be set to the new object created here.

mapper.register(Source, Dest)
      .forMember((dest: Dest) => dest.child!.further, (src: Source) => src.param)
      .forMember(
        (dest: Dest) => dest.child!.random,
        (src: Source) => { return {
                      prop: src.other,
                      nope: src.param,
                    } as RandomClass; },
      );

Package Sidebar

Install

npm i mapped

Weekly Downloads

1

Version

1.1.4

License

ISC

Unpacked Size

5.93 kB

Total Files

8

Last publish

Collaborators

  • tomburbeck