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

simple-mapper

v2.2.0

Published

A convention-based mapping library for nodejs and the browser.

Downloads

29

Readme

npm version Build Status dependencies Status devDependencies Status codecov Greenkeeper badge

SimpleMapper

SimpleMapper provides simple, object-to-object mapping by convention. It was created to solve the problem of recursively mapping JSON to models, in order to gain the benefits of those models, particularly their methods and default values. However, it can be used to map from Javascript objects of any type, to objects of any type.

Usage

let myClassVm = mapper.map(MyClass, { /* JSON object */ }, true);
let myClassVmArray = mapper.mapArray(MyClass, [{ /* JSON object array */ }], false);

The optional third argument turns on (default) or off warnings about missing destination properties.

Models

Due to the way Typescript works (as of v2.2), you should define your models so they always have default values. Otherwise, their properties will not be visible to the mapper.

Be sure the default values for iterables are empty iterables (both in the source and destination), otherwise the properties will be mapped like ordinary properties.

export class MyWidget {
    Id: number; /* not visible to the mapper. */
    Name: string = null; /* visible due to null default. */
    get Display(): string { 
        return `${Name} (Id: ${Id})`;
    }

    @mappable("MyWidget")
    Wiggy: MyWidget = null;

    @mappable(MyWidget)
    WigArray: MyWidget[] = [];
}

If providing model names as strings instead of references, then you must provide a model collection during import (see Setup).

If a source property exists while a destination does not, a warning will be issued by default. You can turn this off by providing a third parameter:

let json = {
    Id: 314,
    Name: "Chris",
    ExtraProp: "Missing in the destination model, MyWidget."
};
mapper.map(MyWidget, json, false);

Installation

npm install --save-dev simple-mapper

Setup

// if you are using dependency injection, your setup might look like this:

import { MapperService, IMapperService, IConfig } from 'simple-mapper';
import * as models from './models/barrel/';

export let MapperServiceToken = new Symbol("MapperService");

diContainer.bind<IMapperService>(MapperServiceToken).to(MapperService);

// or with configuration...

let config = <IConfig> {
    models: models,
    unmappedWarnings: true,
    validateOnStartup: true
};
diContainer.bind<IMapperService>(MapperServiceToken).to(() => new MapperService(config, console));

Options

let config: IConfig = {
    /** The dictionary of models to use for recursive mapping. 
      * Not needed if using object references in @mappable() instead of names.
      * Default: empty. */
    models: {},

    /** Validate models provided on instantiation (makes sure mappable names exist in your models collection).
     * Default: false.
     */
    validateOnStartup: false,

    /** Turn off unmapped source property warnings globally. Can be overridden at the method level. */
    unmappedWarnings: false
}

Build

Run npm run build to build the project. The build artifacts will be stored in the dist/ directory.

Running unit tests

Run npm test to execute the unit tests. Run npm run cover to run tests and generate a code coverage report. Code coverage will be available at ./coverage/index.html.

Documentation

The scaffolding exists, but no real documentation, for the moment. Run 'npm run compodoc' to generate documentation. Then run 'npm run compodoc-serve' to see auto-generated documentation and documentation coverage on port 8080.

Further help

Feel free to post issues.