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

mapper-ts

v2.0.0

Published

A convention-based object mapper that utilizes TypeScript decorators to simplify the mapping of objects between themselves.

Downloads

28

Readme

mapper-ts

A convention-based object mapper that utilizes TypeScript decorators to simplify the mapping of objects between themselves.

Usage

Importing modules

// importing modules
import { Mapper, AddMap, IgnoreMap } from 'mapper-ts';

Update tsconfig.json to enable decorators, if not already enabled.

{
  "compilerOptions": {
    "experimentalDecorators": true,
    // ...
  }
}

Examples

Mapping properties in a class

class AClass {
  // Mapping a property by name
  @AddMap('prop1B')
  prop1A: string;
  @AddMap('prop2B')
  prop2A: number;

  constructor(prop1A: string, prop2A: number) {
    this.prop1A = prop1A;
    this.prop2A = prop2A;
  }
}
class BClass {
  @AddMap('prop1A')
  prop1B: string;
  @AddMap('prop2A')
  prop2B: number;

  // Any properties that don't need to be mapped simply shouldn't have the @AddMap() decorator.
  unmappedProp1B: number;
  unmappedProp2B: string;

  // Mapping an object property by passing it's type
  @AddMap(AClass)
  propAClass: AClass;
}
// Any property name in @IgnoreMap() will be ignored from the source object if it exists in it
@IgnoreMap('ignoredObj', 'ignoredProp', 'nonexistantProp')
class CClass {
  @AddMap('prop1A')
  prop1C: string;
  @AddMap('prop2A')
  prop2C: number;

  // Also valid if an object's property name also needs to be mapped
  @AddMap(BClass, 'anyPropName')
  propBClass: BClass;
}

Calling the Mapper.map() method

const unmappedA = { prop1B: 'PROP 1B', prop2B: 10 };

const unmappedB = {
  prop1A: 'PROP 1B',
  prop2A: 10,
  unmappedProp1B: 20,
  unmappedProp2B: 'UNMAPPED 2B',
  propAClass:  { prop1B: 'PROP 1B in A', prop2B: 300 };
};

const unmappedC = {
  prop1A: 'PROP 1A',
  prop2A: 20,
  anyPropName: {
    prop1A: 'PROP 1A in C',
    prop2A: 20,
    unmappedProp1B: 20,
    unmappedProp2B: 'UNMAPPED 2B in C',
  },
  ignoredObj: {
    ignoredProp1: 'IGNORED PROP in obj',
    ignoredProp2: 'IGNORED PROP in obj2',
  },
  ignoredProp: 'IGNORED PROP',
};


const mappedA = new Mapper(AClass).map(unmappedA);

const mappedB = new Mapper(BClass).map(unmappedB);

const mappedC = new Mapper(CClass).map(unmappedC);
# Results:
AClass { prop1A: 'PROP 1B', prop2A: 10 }
BClass {
  prop1B: 'PROP 1B',
  prop2B: 10,
  unmappedProp1B: 20,
  unmappedProp2B: 'UNMAPPED 2B',
  propAClass: AClass { prop1A: 'PROP 1B in A', prop2A: 300 }
}
CClass {
  prop1C: 'PROP 1A',
  prop2C: 20,
  propBClass: BClass {
    prop1B: 'PROP 1A in C',
    prop2B: 20,
    unmappedProp1B: 20,
    unmappedProp2B: 'UNMAPPED 2B in C',
    propAClass: null
  }
}

Compiling the source code to JS files

  • To compile all the code in the src folder to the lib-esm folder, run this command at the root of the project with typescript installed:
tsc
  • All configuration for this command is already set in tsconfig.json