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

json-reform

v1.0.4

Published

A mini NodeJS lib that helps you map JSON objects easier and faster.

Downloads

9

Readme

JSON-Reform

A mini NodeJS library that helps you transform JSON objects faster & easier.

Installation

Using NPM:

npm install json-reform

In browser:

<script src="reform.js"></script>

Usage

const re = new Reformer(rules [,opts])

Transformation Rules

rules is an object whose keys are names of attributes that you want to transform, values define how these attribute will be transformed.

There are multiple ways to define them:

Rename an attribute

{
  old_name: "new_name"
}

Do something with the value

{
  version: function(v){
      return v+1;
  }
}

Full definition

{
  version: {
      name: 'ver',
      handler: function(v){
          return v+1;
      }
  }
}

One to many relationship

Attribute a will be transformed to a, b and c in the new object.

{
  a: [
      // this produces a
      function(val){
        return val-1;
      },
      
      // this produces b
      "b",
      
      // this produces c
      {
        name: "c",
        handler: function(old_value){
            return old_value + 1;
        }
      }]
}

Many to one relationship

You can access other attribute with the second parameter in the handler function.

{
  x: {
      name: 'z',
      handler: function(x, obj){
          const y = obj.y;
          return x+y;
      }
  }
}

Options

Name|Type|Default|Description :-----------|:------|:----|:----------- keepUnlisted|Boolean|false|Attributes of origin object that are not listed in schema will be kept. async |Boolean|false|transform() function will return a Promise. This option must be set to true when you are using any asynchronous function sequential |Boolean|false|When an array of objects is passed to transform() function, those will be transformed one after another. This may help to avoid overhead when you are sending network requests.

Example

const Reformer = require('json-reform');

const re = new Reformer({
    a: "b",
    c: {
        name: "d",
        handler(val, obj) {
            return val + 1;
        }
    },
    d: [{
        name: "e",
        handler(val) {
            return val + 1;
        }
    }, {
        name: "f",
        handler(val) {
            return val + 2;
        }
    }]
}, {keepUnlisted: true});

const result = re.transform({a: 1, b: 2, c: 3, d: 5, g: 0});
console.log(result);

Output:

{ f: 7, e: 6, d: 4, b: 2, g: 0 }

You can also transform an array of objects

const result = re.transform([{a: 1, b: 2, c: 3, d: 5, g: 0}]);

Output:

[
    { f: 7, e: 6, d: 4, b: 2, g: 0 }
]

When async option is true:

re.transform([{a: 1, b: 2, c: 3, d: 5, g: 0}])
    .then(res => {
        // Do something
    })

License

MIT © Thien Phuc Tran