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

easymapper

v1.0.2

Published

Data mapping utility

Downloads

9

Readme

EasyMapper

Simple data mapping utility for easier hexagonal architecture implementation.

Main purpose of the library is to reduce the amount of code that you need to write to map your objects.

Map

A method that allows you to map objects synchronously or asynchronously using specification. To map an object you need to pass two parameters to the Map function:

  1. specification object
  2. object that you want to map

As a result the function will return the object that is the result of mapping.

Specification object

Is the object where you specify how to acquire properties of the destination object from the source object.

Each property has a name that corresponds to the specific property P of the destination object. Every property is a function that takes a deep copy of the source object and returns a value that should be put in the destination object at P key.

Map specification looks like this

export type ObjectMapSpec<O1 extends DataObject, O2 extends DataObject> = {
    [P in keyof O2]: (o: O1) => O2[P]
} & {[MapperSpecOptionsSym]?: {async?: false}};

// or async version
export type AsyncObjectMapSpec<O1 extends DataObject, O2 extends DataObject> = {
    [P in keyof O2]: (o: O1) => O2[P] | Promise<O2[P]>
} & {[MapperSpecOptionsSym]: {async: true}};      // you need to set async option to true

Examples

Simple object

type UserInfo = {
    userId: string,
    colorOfEyes: string,
    role: 'user' | 'admin',
    city: string,
    location: [number, number],
}
type UserInfoView = {
    colorOfEyes: [number, number, number],
    role: 'user' | 'admin',
    city: string,
    location: string,
}
const UserInfoUserInfoViewMapSpec: ObjectMapSpec<UserInfo, UserInfoView> = {
    role: o => o.role,
    city: o => o.city.toUpperCase(),
    location: o => o.location[0] + '.' + o.location[1],
    colorOfEyes: o => MapColor(o.colorOfEyes)
}
const userInfo = {
    userId: 'dsf79f8s98f7',
    role: 'user',
    colorOfEyes: '2FA8FF',
    city: 'London',
    location: [2141241, 509520523],
}
const userInfoView = Map(UserInfoUserInfoViewMapSpec, userInfo);

Result will be:

{
  "role": "user",
  "city": "LONDON",
  "location": "2141241.509520523",
  "colorOfEyes": [47, 168, 255]
}

Mapping nested objects

  1. Using specification to map nested objects For bigger nested objects with a lot of mapping logic it's recommended to put mapping logic into another specification and use Map function inside the specification like so:
const userMapSpec = {
    userInfo: o => Map(UserInfo_UserInfoViewMapSpec, o.userInfo)
    // ...
}
  1. Mapping by hand Smaller objects can be mapped directly without creating another specification.
userInfo: o => ({
    city: o.userInfo.city,
    role: o.userInfo.role,
    location: o.userInfo.location,
    colorOfEyes: MapColor(o.userInfo.colorOfEyes)
})