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

merg

v2.0.0

Published

Validate, resolve and merge options

Downloads

34

Readme

merg Build Status

Define object schema, validate and merge objects in different context

Installation

$ npm install merg -S

Usage


var Merg = require('merg');
var V    = require('node-veee');
var v    = V.scaffold(); // using [node-veee](https://github.com/node-veee/veee) for validations

// options is optional, below are the default handlers being used internally
var options = {
  // handler for formatting target object
  formatObject: function(value, context, cb) {
    return cb(null, value);
  },
  // handler for validating value-to-merge with schema field type
  validateField: function(value, type, context, cb) {
    return cb(null, value)
  },
  // handler for resolving field(s) not defined in schema
  onMissingField: function(fieldName, oldVal, newVal, context, cb) {
    return cb(null, newValue);
  }
};

var mergeOpts = new Merg({
  opt1: { type: v.string() },
  opt2: { type: v.number() }
});

var merge = new Merg({
  limit: { 
    type: v.number().positive()
  },
  offset: {
    type: v.number().nonNegative().default(10),
    // optional resolve function to decide what to merge
    resolve: function(oldVal, newVal, context, cb) {
      if (context.trusted) {
        return cb(null, newVal);
      } else {
        return cb(null, oldVal);
      }
    }
  },
  includeDeleted: {
    type: v.boolean().default(false),
    resolve: function(oldVal, newVal, context, cb) {
      if (context.user.role === 'admin') {
        return cb(null, newVal);
      } else {
        return cb(null, oldVal);
      }
    }
  },
  // nested merg
  opts: mergeOpts
}, options);

merge(old, { limit: 10, offset: 0 }, function(err, merged) {
  
});

merge(old, [{ limit: 1000, offset: -5 }, ...], { trusted: false }, function(err, merged) {
  
});

API

Merg(schema[, options])

create a new merger with a given schema (the resulting merger is a pure function, target won't not be mutated)

possible option(s):

  • formatObject transformation to be done, it's being executed before everything
  • validateField function to validate value-to-merge with the type in schema, plug your favourite validation module in, or use node-veee
  • onMissingField resolve function for fields that are not defined in schema

var Merg = require('merg');

var schema = {
  name: { type: v.string().required() } 
}

var options = {
  onMissingField: function(fieldName, oldVal, newVal, context, callback) {
    if (fieldName !== 'password') {
      return callback(null, newVal);
    }
  }
}

var merge = new Merg(schema, options);
merge(target[, sources][, context], callback)

merge objects


var schema = {
  name: { type: v.string() }
};

var options = {
  onMissingField: function(fieldName, oldVal, newVal, context, callback) {
    return callback(); // dont assign any missing fields
  }
}

var merge = Merg(schema, options);

var target = { name: 'John' };

merge(target, [{ name: 'Peter', age: 25 }, { name: 'Derek' }], function(err, result) {
  console.log(target); // { name: 'John' }
  console.log(err);    // null;
  console.log(result); // { name: 'Derek' }
});

Schema

// valid schema
var schema = { name: { type: v.string().required() } };

// also a valid schema
var mergeName = new Merg({ ... });
var schema = { name: mergeName };

Schema fields:

  • type: anything, whatever could be used to validate the value-to-merge, being used in validateField function
  • resolve: function that will be used for merging (optional)
  • format: function to transform the value-to-merge before validateField & resolve
{
  name: {
    type: v.object().keys({
      first: v.string().required(),
      last: v.string().required()
    }),
    resolve: function(oldValue, newValue, context, cb) {
      cb(null, {
        first: newValue.first,
        last: newValue.last
      });
    },
    format: function(value, context, cb) {
      cb(null, value);
    }
  }
}
Nesting

Same as above

var nameMerg = new Merg({
  first: { type: v.string().required() },
  last: { type: v.string().required() }
});

var personMerg = new Merg({
  name: nameMerg
});

Author

Marvin Lam [email protected]

Vladimir Popov [email protected]

License

MIT