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 🙏

© 2026 – Pkg Stats / Ryan Hefner

object-model-transform

v1.2.1

Published

Transforms objects and its object property into model.

Readme

API Reference

transform(data: any, schema: ITransformSchema)

Recursively transforms data into a model by the given schema and returns the value.

  • data - [required] Any object or array data to transform.
  • schema - [required] The schema for transforming objects to Model
    • field - [required for nested schema] The property field you want to transform. Supports dotnotation as field value.
    • Model - [optional] The Model class of the object to transform.
    • include - [optional] A Schema to transform nested objects. Value can be array for multiple fields to transform.
    • singleParam - [optional] A boolean value to provide a single parameter with any data type for the constructor of any custom model.
    • multiParam - [optional] A boolean value to provide multiple parameters for the constructor of any custom model. Provided data type must be array that contains all the parameters.
    • onTransform - [optional] Provide a function with a highest precedence that returns a transformed data.

Defining a Model

  var transformer = require('object-model-transform');

  // using a built-in Model
  class Person extends transformer.Model { }
  // create your custom Model
  class Item {
    constructor(obj) {
      Object.assign(this, obj);
    }
  }
  class Animal {
    constructor(obj) {
      Object.assign(this, obj);
    }

    makeSound() {
      // code ...
    }
  }

Guide

Basic Transform

  var obj = {
    name: 'li',
    age: 32,
  };

  var schema = {
    Model: Person,
  };

  transformer.transform(obj, schema);
    // => Person { name: 'li', age: 32, };

Transforming an array

  var arr = [
    { name: 'Gab' },
    { name: 'Pauline' },
  ];
  
  transformer.transform(arr, schema);
    // => [Person { name: 'Gab', }, Person { name: 'Pauline' }]

Transforming a nested objects

  var nestedObjs = [
    {
      name: 'Pat',
      items: [
        { name: '0', color: 'red' },
        { name: '1', color: 'black' },
      ],
      pet: {
        name: 'Brench',
        type: 'dog',
        collar: { color: 'yello' },
      },
    },
  ],

  var nestedSchema = {
    Model: Person,
    include: [
      { field: 'items', Model: Item },
      {
        field: 'pet',
        Model: Animal,
        include: { field: 'collar', Model: Item }
      },
    ],
  };

  transformer.transform(nestedObjs, nestedSchema); /* => 
    [
      Person {
        ....
        items: [
          Item { .... },
          Item { .... },
        ],
        pet: Animal {
          ....
          collar: Item { .... },
          makeSount(),
        },
      },
    ]
  */

Basic use of dot notation as a field

  var dnObj = {
    name: 'jans',
    item: {
      tool: {
        type: 'programming',
        laptop: { color: 'black' },
      },
    },
  };
  var dnSchema = {
    include: {
      field: 'item.tool.laptop',
      Model: Item,
    },
  }

  transformer.transform(dnObj, dnSchema); /* => 
  {
    ....
    item: {
      tool: {
        ....
        laptop: Item { .... },
      },
    },
  }
  */

For dot notation with multiple fields

  var dnObj2 = {
    name: 'Brent',
    computer: {
      favorite: {
        laptops: [
          { brand: 'dell' },
          { brand: 'asus' }
        ],
        computer: {
          brand: 'mansanas',
          casing: { material: 'titanium' }
        },
      },
    },
  };
  var dnSchema2 = {
    Model: Person,
    include: {
      field: 'computer.favorite',
      include: [
        { field: 'laptops', Model: Laptop },
        {
          field: 'computer',
          Model: Computer,
          include: { field: 'casing', Model: Item },
        },
      ],
    },
  };

  transformer.transform(dnObj2, dnSchema2); /* => 
    Person {
      ....
      computer: {
        favorite: {
          laptops: [
            Laptop { .... },
            Laptop { .... }
          ],
          computer: Computer {
            ....
            casing: Item { .... }
          },
        },
      },
    };
  */

Controling the data to transform

onTrnasform(Model, data, parent, root)

Customize a model for the provided field.

  • Model - The model provided in schema.
  • data - The data object to transform from the specified field of schema. For nested transformation child object of data are the first to transform.
  • parent - The parent object or array of the data.
  • root - The root object from nested transformation
 class Person {
   // constructor that receives custom parameters
   constructor(person, petName) {
     Object.assign(this, person);
     this.petName = petName;
   }
 }

var data = {
  person: { name: 'Karl', age: 33 },
  pet: { name: 'Light' },
};
var schema = {
  include: {
    field: 'person',
    Model: Person,
    onTransform: (Model, data, parent, root) => {
      return new Model(data, root.pet.name);
    },
  },
};

transformer.transform(data, schema) /*=>
  {
    person: Person {
      name: 'Karl',
      age: 33,
      petName: 'Light'
    },
    pet: { name: 'Light' },
  }
*/

Models with different parameters

Single parameter


var timestamp = Date.now;
// 'singleParam' property to pass any type of single parameter
var schema = { Model: Date, singleParam: true }

transformer.transform(name, schema); /* =>
  Date { ... }
*/

Multiple Parameter

class Laptop {
  constructor(name, brand, weight) {
    this.name = name;
    this.brand = brand;
    this.weight = weight;
  }
}

var data = ['pc32', 'brand-y', 2];
// 'multiParam' property to pass any type of multiple parameters
var schema = { Model: Laptop, multiParam: true };

transformer.transform(data, schema); /* =>
  Laptop { name: 'pc32', brand: 'brand-y', weight: 2 }
*/