npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-mapper

v1.2.4

Published

Object mapper for TypeScript

Downloads

1,948

Readme

TypeScript Automapper

Inspired by MaDEiN83 mapper

Installation

$ npm install ts-mapper

Usage

Create map

To create a mapping between two objects, you must call the object method createMap of the TypeMapper class.

It takes two interfaces:

  • first interface must be the source interface
  • second interface must be the destination interface

So, if we have an object of type ISource and we want a object of type IDestination, we should create a new mapping like that:

import { TypeMapper } from "ts-mapper";
import { ISource, IDestination } = "../path/of/interfaces.ts";

export class Mapper extends TypeMapper {
   constructor() {
      super();
      this.config();
   }

   private config(): void {
      // put here your mapping configurations
      this.createMap<ISource, IDestination>();
   }
}

const mapper = new Mapper();

Map fields

After we create a mapping between interfaces, we can now create all mappings between all wanted properties of our objects (source & destination).

For example, if we want to map the property sourceObject.srcOther to destinationObjet.other, we can define rule like that:

mapper.createMap<ISource, IDestination>();
  .map(src => src.srcProperty, dest => dest.destProperty)
  .map(src => src.srcOther, dest => dest.other);
  • src type is ISource
  • dest type is IDestination

You can chain your rules !

Conditionnal mapping

conditions method allows you to check if the previous map will be analysed and mapped to the destination object.

Example: We want to map the property srcProperty (source object) into destProperty (destination object) only if the property visible of the source object is true.

mapper.createMap<ISource, IDestination>();
  .map(src => src.srcProperty, dest => dest.destProperty)
  .conditions((s: ISource) => s.visible);

The conditions method takes two arguments:

  • the source object
  • the destination object

Examples:

mapper
   .createMap<ISource, IDestination>()
   .map(src => src.my_prop, dest => dest.myProp)
   .conditions((s: ISource, d: IDestination) => s.visible);

mapper
   .createMap<ISource, IDestination>()
   .map(src => src.my_prop, dest => dest.myProp)
   .conditions((s: ISource, d: IDestination) => s.visible !== d.visible);

Fly and Casts

Sometime the source properties don't match the destination properties.

Example: sourceObject.age can be a string and destinationObject.age is a number.

To work with, you can cast property by chaining the is method after a map.

import { TypeMapper, AutoMapperTypes } from "ts-mapper";

mapper
   .createMap<ISource, IDestination>()
   .map(p => p.age, p => p.age)
   .is(AutoMapperTypes.NUMBER);

Execution and Mapping

mapper
   .createMap<ISource, IDestination>()
   .map(p => p.name, p => p.name)
   .is(AutoMapperTypes.STRING);

const source: ISource = {
   name: "Marluan",
   email: "[email protected]",
   password: "my_sup3r_s3cr3t_p455w0rd"
};

const destination: IDestination = { name: null };

mapper.map<ISource, IDestination>(source, destination);

console.log(destination);
// {
//    name: "Marluan";
// }