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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@eherve/model-mapper

v6.3.4

Published

Map json data to javascript object

Readme

@eherve/model-mapper

Map plain JavaScript data into class instances using property decorators, and serialize those instances back to plain data. The mapper supports nested models, arrays, dates, Moment values, inheritance, and discriminators.

Install

npm install @eherve/model-mapper

Enable legacy property decorators in your TypeScript configuration:

{
  "compilerOptions": {
    "experimentalDecorators": true
  }
}

Map and serialize a model

import { ModelMapper, PropertyMap } from '@eherve/model-mapper';

class User {
  @PropertyMap({ source: '_id' })
  id!: string;

  @PropertyMap()
  name!: string;

  @PropertyMap({ type: Date })
  createdAt!: Date;

  @PropertyMap({ default: [] })
  roles!: string[];
}

const mapper = new ModelMapper(User);
const user = mapper.map({
  _id: 'user-1',
  name: 'Ada',
  createdAt: '2026-01-01T00:00:00.000Z',
});

user.id; // 'user-1'
user.createdAt instanceof Date; // true
user.roles; // []

mapper.serialize(user);
// {
//   _id: 'user-1',
//   name: 'Ada',
//   createdAt: '2026-01-01T00:00:00.000Z',
//   roles: []
// }

Only decorated properties are mapped and serialized. source names the input path and the serialized output path; dotted paths such as profile.name are supported. An absent source property leaves the destination property unchanged unless a default is defined. A null source value remains null.

Mutable default values are copied for each mapped object. A function supplied as default is called for each mapped object.

Nested models and arrays

class Address {
  @PropertyMap()
  city!: string;
}

class Customer {
  @PropertyMap({ type: Address })
  address!: Address;

  @PropertyMap({ type: [Address], default: [] })
  previousAddresses!: Address[];
}

const customer = new ModelMapper(Customer).map({
  address: { city: 'Paris' },
  previousAddresses: [{ city: 'Lyon' }],
});

The type option also accepts Date, 'Moment', and 'Moment.Duration'. Import moment in your application if you work with Moment values directly.

Inheritance and discriminators

Decorated properties on a base class are available to subclasses. A subclass can add decorated properties or override a parent's mapping options.

import { Model } from '@eherve/model-mapper';

@Model({ discriminatorKey: 'kind' })
class Animal {
  @PropertyMap()
  kind!: string;
}

class Cat extends Animal {
  @PropertyMap()
  lives!: number;
}

const mapper = new ModelMapper(Animal, { cat: Cat });
const animal = mapper.map({ kind: 'cat', lives: 9 });
animal instanceof Cat; // true

Pass discriminators alongside type in @PropertyMap() to select subclasses for nested models or arrays. The base type must declare its discriminator key with @Model().

Custom transformations

@PropertyMap() accepts map and serialize callbacks. Each receives the full source, the converted value, the target under construction, and the property or source path. Mapping callbacks receive the resolved source path; serialization callbacks receive the class property name.

class Product {
  @PropertyMap({
    source: 'price.cents',
    map: (_source, cents) => cents / 100,
    serialize: (_source, euros) => Math.round(euros * 100),
  })
  price!: number;
}

map() returns undefined when called without a source or with a falsy source. TypeScript reflects this for omitted and explicitly nullable sources. When a source is typed any, check it before mapping if it may be absent.

Development

npm ci
npm test
npm pack --dry-run

npm test builds lib, checks the package entry point, runs the tests, and checks source coverage. The published package contains the generated files in lib; npm run build regenerates them.

License

MIT. See LICENSE.