🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

mapped

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mapped

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

latest
npmnpm
Version
1.1.4
Version published
Weekly downloads
14
366.67%
Maintainers
1
Weekly downloads
 
Created
Source

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; },
      );

Keywords

Mapping

FAQs

Package last updated on 01 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts