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-automapper

v2.0.5

Published

Magic library for node to be able to transform object to another object

Downloads

227

Readme

ts-automapper npm version npm npm npm bundle size CircleCI

Table of Contents

Installing

Via yarn

$ yarn add ts-automapper

Via npm

$ npm i ts-automapper

Usage

Before, you had to create a method or utility class in order to convert an object (generally from an API) to another one.

For instance:

// person.service.ts
const createPerson = (rawPersonFromAPI: RawPerson): Promise<Person> => {
  const person: DTOPerson = {
    fullName = `${rawPersonFromAPI.firstname} ${rawPersonFromAPI.lastname}`;
    firstname = rawPersonFromAPI.firstname.trim();
    lastname = rawPersonFromAPI.lastname.trim().toUpperCase();
    email = rawPersonFromAPI.email.trim().toLowerCase();
    tel = rawPersonFromAPI.tel.replaceAll(" ", "");
    address01 = persrawPersonFromAPIonInput.address01.trim();
    address02 = rawPersonFromAPI.address02.trim();
    postalCode = rawPersonFromAPI.address03.split("_")[0];
    city = rawPersonFromAPI.address03.split("_")[1] || "Saint-Raphaël";
    // Other properties mapping.
  };

  return this.service.createPerson(person);
}

Example

Now, you just need to:

  • create a mapping definition (like schema)
  • import the schema
  • use it! 🦄

Ready?

  1. Creating a mapping is quit easy and similar to what you had before

You can map all properties you want. The first argument is an unique key to be used to retrieve and apply the schema from everywhere. The second argument is an array with your mappings.

// ./src/core/mappings/index.ts
import AutoMapper from "ts-automapper";

AutoMapper.create<FromType, ToTYpe>("createPerson", [
  ["person.firstname", ({ person }) => person.firstname.trim()],
  ["person.lastname", ({ person }) => person.lastname.trim().toUpperCase()],
  ["person.email", ({ person }) => person.email.trim().toLowerCase()],
  // ...
  ["person.postalCode", ({ person }) => person.address03.split("_")[0]],
  ["person.city", ({ person }) => person.address03.split("_")[1]],
  // ...
]);
  1. Import your schema(s)

You can import your schema(s) where you want. Just be sure it's imported before you want to use them.

// ./src/index.ts
import "@/core/mappings";
  1. Use your created schema

Then, you only need one line to transform your object from FromType to ToType. From everywhere.

const person = AutoMapper.apply<FromType, ToType>("createPerson", raw);

API

create

AutoMapper.create<A, B>(key, mappings);

| Parameter | Description | | ---------- | ----------------------------------------------------------------------- | | A | Your raw object type you want to convert. | | B | Your target object type you want to convert to. | | key | A unique identifier for the mapping. | | mappings | An array of field mappings from source type (A) to target type (B). |

Example

type A = { first_name: string };
type B = { firstname: string };

AutoMapper.create<A, B>("UNIK_KEY", [
  ["firstname", (rawObject: A) => rawObject.first_name.trim()],
]);

The firstname is not an hardcoded string. It's like a enum-like string type. It means that all keys are inferred from your B type.

From here, you're telling to AutoMapper that you want to convert A to B by:

  • setting up an unique identifier UNIK_KEY
  • taking the first_name property (from A) and injecting it to firstname property of B
  • applying a trim() to it before the injection

Nothing more. You can add all needed property mappings you need. 🎯

apply

AutoMapper.apply<A, B>(key, rawObject);

| Parameter | Description | | ----------- | ----------------------------------------------------------------- | | A | Your raw object type you want to convert. | | B | Your target object type you want to convert to. | | key | A unique identifier for the mapping you created with .create(). | | rawObject | Your raw object of type A. |

Example

// Import your schema(s) where you want, but before trying to apply it.
import "./schema.ts";

const output: B = AutoMapper.apply<A, B>("UNIK_KEY", {
  first_name: "Anthony   ",
}); // output is: { firstname: "Anthony" }

Output will be:

{ "firstname": "Anthony" }

From here, you want to apply an existing schema and get a valid B type.

That's all 🤭