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

morpheusjs

v0.0.6

Published

A simple little library to transform data structures

Downloads

7

Readme

Morpheus

A simple little library to validate and transform objects. It is inspired by the awesome .NET library AutoMapper but built for JavaScript.

Note: This is an experimental package. Please file an issue if you find one.

Build Status Dependency Status Coverage Status

Installation

Using Bower

bower install morpheus --save

In a browser:

<script src="bower_components/morpheus/dist/morpheus.js"></script>

Using npm

npm install morpheusjs --save

In Node.js/io.js

let Morpheus = require('morpheus')
let morpheus = new Morpheus()

Overview

Morpheus uses JSONSchema to work with objects in a "typed" way. There is a two step process.

  1. Register: register all mappings (typically done when your app is bootstrapping). fromSchema and toSchema are JavaScript objects that comply with the JSONSchema spec.
  2. Use one of the registered mappings to transform.

Register example:

let fromSchema = {
	type: 'object',
  properties: {
    name: { type: 'string' },
    address: { type: 'string' }
  }
}

let toSchema = {
	type: 'object',
	properties: {
		address: { type: 'string' }
	}
}
let = morpheus = new Morpheus()

morpheus.register({
	id: 'neo',
	fromSchema: fromSchema,
	toSchema: toSchema
})

Map example:

let fromObj = { name: 'Mr. Anderson', address: 'Capital City, USA' }

let actual = morpheus.map('neo', fromObj)
expect(actual)
	.to.have.property('name').equal('Mr. Anderson')
let isValid = morpheus.validate(actual, toSchema)
expect(isValid.errors).to.have.length(0)

Features

Note: Examples are taken from unit tests located in ./test

Validation with JSONSchema

Validation is enforced on both fromObj and toObj using the fromSchema and toSchema. If you are calling an external service and if the service changes the data model, you can get a validation error early. It also makes writing unit tests easier for the mapping logic.

let schema = {
	type: 'number'
}
let instance = 4

let actual = morpheus.validate(instance, schema)
expect(actual.errors).to.have.length(0)

Map Arrays

Mapping arrays and transforming them

let fromSchema = {
	type: 'array',
	items: {
		type: 'number'
	}
}
let toSchema = {
	type: 'array',
	items: {
		type: 'number'
	},
	morph: x => x.map(y => y * 2)
}
let fromArray = [1, 2, 3]
let actual = morpheus.map('array', fromArray)
//actual is now [2,4,6]

Map objects

Mapping objects with desired properties

let fromSchema = {
	type: 'object',
	properties: {
		name: { type: 'string' },
		address: { type: 'string' },
		zip: { type: 'string' }
	}
}
let toSchema = {
	type: 'object',
	properties: {
		name: { type: 'string' },
		zip: { type: 'string' }
	}
}

Default values

Default value for the zip property

let fromSchema = {
	type: 'object',
	properties: {
		name: { type: 'string' },
		zip: {
			type: ['string', 'null']
		}
	}
}
let toSchema = {
	type: 'object',
	properties: {
		name: { type: 'string' },
		zip: {
			type: 'string',
			'default': '60075'
		}
	}
}

Flattening

Flatten an object using the camelCase property naming convention for the addressZip property.

let fromSchema = {
	type: 'object',
	properties: {
		address: {
			type: 'object',
			properties: {
				zip: {
					type: ['string']
				}
			}
		}
	}
}
let toSchema = {
	type: 'object',
	properties: {
		addressZip: {
			type: 'string'
		}
	}
}

Projection

Project over properties of an object and create new properties

let fromSchema = {
	type: 'object',
	properties: {
		firstName: {
			type: 'string'
		},
		lastName: {
			type: 'string'
		},
		zip: {
			type: 'number'
		}
	}
}
let toSchema = {
	type: 'object',
	properties: {
		name: {
			type: 'string',
			morph: x => `${x.firstName} ${x.lastName}`
		},
		zip: {
			type: 'string',
			morph: x => x.zip.toString()
		}
	}
}