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

super-object-mapper

v0.6.0

Published

Sup-ed up object-level schema control - whitelist fields and convert between types and keys.

Downloads

17

Readme

#super-object-mapper

Gitter

Flexibly enforce any schema, anytime. SuperOM is sup-ed up schema control over any object - whitelist fields and convert between types and keys at your leisure.

Many thanks and credit are due to wankdanker's node-object-mapper. This module is essentially an API and Type-enforcement layer on top of it, an abstraction of a pattern that evolved at Moveline.

#Install

npm i --save super-object-mapper

#Philosophy

##Use-Cases

It is often desirable to enforce an object mapping or schema at an application's interfaces, typically between app logic and a Database or a Client.

An object mapper can deal with issues where your app's consumers:

  • Need to limit an object to a subset of fields
  • Prefer different data types (Ex: MongoId vs String)
  • Use legacy field names that are no-longer ideal

##Intent

Notably, the above use-cases can all be accomplished with wankdanker's node-object-mapper. After building a handful of apps with this module, a few patterns popped up - Super Object Mapper intends to present those patterns with a simple API.

#Usage

You can require and create an instance of the Super Object Mapper anywhere in your app:

var SuperOM = require('super-object-mapper');
var superOM = new SuperOM();

###superOM.addMapper(mapper, mapperName)

Define and add a mapper to any instance of superOM like so:

userMapper = {
  "database": {
    "id": "_id",
    "name": "name",
    "email": "emailAddress"
  },
  "domain": {
    "_id": "id",
    "name": "name",
    "emailAddress": "email"
  }
}

superOM.addMapper(userMapper, "users");

###superOM.mapObject(object, options)

You can then run any object across the mapper and map of your choosing.

var object = {
  id: "123456abcdef654321fedcba",
  name: "Mario",
  email: "[email protected]",
  extraneousProperty: "whatever data"
}
var databaseObject = superOM.mapObject(object, {mapper: "users", map: "database"});

console.log(databaseObject);
//{
//  _id: "123456abcdef654321fedcba",
//  name: "Mario",
//  emailAddress: "[email protected]"
//}

Only the fields specified by the mapper will survive the mapping. Fields explicity set to falsy values will be carried through the mappers as null.

If the specifed map or mapper do not exist, an error will be thrown.

If the object passed is falsy, null will be returned.

Instead of an object, you may hand an array of objects to the mapObject function. The mapper will be run over every object in the array, and the fully mapped Array will be returned.

var array = [
  {
    id: "123456abcdef654321fedcba",
    name: "Mario",
    email: "[email protected]",
    extraneousProperty: "whatever data"
  }, {
    id: "123456abcdef654321fedcba",
    name: "Luigi",
    email: "[email protected]",
    extraneousProperty: "whatever data"
  }
]
var databaseArray = superOM.mapObject(array, {mapper: "users", map: "database"});

console.log(databaseArray);
//[
//  {
//    _id: "123456abcdef654321fedcba",
//    name: "Mario",
//    emailAddress: "[email protected]"
//  },{
//    _id: "123456abcdef654321fedcba",
//    name: "Luigi",
//    emailAddress: "[email protected]"
//  }
//]

###options

You can specify clean on the options object to remove falsey values from the mapped object.

var mappedObject = superOM.mapObject(object, {mapper: "users", map: "database", clean: true});

##SuperOMType

super-object-mapper provides a set of built-in datatype handlers. These types can be implemented in your mappers as follows:

superOMType = require('super-object-mapper').Types;

userMapper = {
  "database": {
    "id": superOMType.objectId("_id")
  },
  "domain": {
    "_id": superOMType.string("id")
  }
}

These wrappers will cast your data type as the specified type, with the exception of falsy values, which will always be passed across the mappers as null.

###.string(key)

Converts any value passed to a String.

###.objectId(key)

Currently this depends on the ObjectID provided by mongodb-core.BSON (TODO: add links).

#Development

Feel free to contribute! Open to any PRs.

Run tests with npm test.