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

mogrifier

v0.2.0

Published

Transmogrifying input based on a defined model

Downloads

14

Readme

Mogrifier Build Status

Mogrifier is a simple type transmogrification library. It allows you to define simple models/schemas and use them to transmogrify values to the types defined in your model.

It arose out of frustration with having to deal with Dates being sent via REST as .toJSON() values and then being stored in Mongo as strings instead of ISODate.

Example

'use strict';

import { mogrify } from 'mogrifier';

const model = {
  id: String,
  name: String,
  birthday: Date
};

const input = {
  id: 'abc123',
  name: 'Pat',
  birthday: '1981-01-01T00:00:00.000Z' // <- String, not a Date =(
};

let mogrified = mogrify(model, input);

console.log('before -', input);
console.log('after -', mogrified);

/*
before - { id: 'abc123',
  name: 'Pat',
  birthday: '1981-01-01T00:00:00.000Z' }
after - { id: 'abc123',
  name: 'Pat',
  birthday: Wed Dec 31 1980 16:00:00 GMT-0800 (PST) }
*/

mogrify(model, input[, options])

  • model - Function, Object, Array, required - The model is the definition for how to mogrify the input and which type you use depends on the input you are working with.
  • input - Any, required - The input is the value that you are trying to mogrify. Typically, this is a JSON Object or Array, but it can be a simple value as well.
  • options - Object, optional - Allows for customizing the behavior of the mogrification
    • strict - Boolean, default = false - In strict mode, mogrifier will strip out any properties in the input that are not in the model

Defining a Model

There are three main ways to define a model.

Model as a Type Constructor

This capability arose out of the recursive nature of the library, but it ends up being pretty useful. If you're not needing to mogrify objects or arrays, you can still mogrify values using constructors.

const model = String; // <- you want to mogrify TO a String

let mogrified = mogrify(model, true);

console.log(mogrified); // -> 'true'

Model as an Array

If your input is or has an Array, you can define that in the model.

const model = [String]; // <- your input should be mogrified to an Array of Strings

let mogrified = mogrify(model, [ true, 0, new Date() ]);

console.log(mogrified); // -> [ 'true', '0', 'Wed Dec 31 1980 16:00:00 GMT-0800 (PST)' ]

Model as an Object

Defining an object model is probably the most common and complex approach. The keys on your model object should match up with the keys on your input object that you want to mogrify into a specific type. Assigning the type constructor to the specific key will enforce the mogrification.

const model = {
  keyA: String,
  keyB: Number,
  keyC: Boolean,
  keyD: Date
};

const input = {
  keyA: 0,
  keyB: false,
  keyC: 'true',
  keyD: '1981-01-01T00:00:00.000Z'
};

let mogrified = mogrify(model, input);

console.log(mogrified);
/*
{ keyA: '0',
  keyB: 0,
  keyC: true,
  keyD: Wed Dec 31 1980 16:00:00 GMT-0800 (PST) }
 */

Nested Objects and Arrays

Mogrification of nested objects and arrays is still pretty simple. You just follow the same pattern as above when you define your model and mogrify() will navigate the nesting to mogrify the types you've specified.

const model = {
  id: String,
  name: { first: String, last: String }, // <- nested Object
  tags: [String], // <- nested Array containing Strings
  cats: [{ name: String, legs: Number }] // <- Object(s) within nested Array
};

const input = {
  id: 123456789, // <- oops, not a String
  name: { first: 'Pat', last: 'Jackson' },
  tags: [ 'foo', 'bar' ],
  cats: [
    { name: 'Tripod', legs: '3' }, // <- legs should be a Number
    { name: 'Monopod', legs: true }, // <- legs should be a Number
    { name: 'Hovercat', legs: false } // <- legs should be a Number
  ]
};

let mogrified = mogrify(model, input);

console.log(mogrified);
/*
{ id: '123456789',
  name: { first: 'Pat', last: 'Jackson' },
  tags: [ 'foo', 'bar' ],
  cats:
   [ { name: 'Tripod', legs: 3 },
     { name: 'Monopod', legs: 1 },
     { name: 'Hovercat', legs: 0 } ] }
 */

Custom Type Constructors

If none of the built-in type constructors meet your needs, you can assign custom functions for mogrification.

const model = {
  id: String,
  foo(value) { return value + '-bar'; }
};

const input = {
  id: 123456789,
  foo: 'foo' // <- should be "foo-bar"
};

let mogrified = mogrify(model, input);

console.log(mogrified); // <- { id: '123456789', foo: 'foo-bar' }

Version

  • 0.2.0 - added options to mogrify(model, input, options)
  • 0.1.2 - npm publish on master
  • 0.1.1 - Internal refactor to simplify mogrifyArray()
  • 0.1.0 - Initial release