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

opex

v0.1.10

Published

A tiny, fast extend/mixin function with n-args and functions-as-objects support.

Downloads

15

Readme

opex Build Status

opex (Options Extender) is a tiny, fast extend/mixin function with n-args and functions-as-objects support.

var opex = require('opex'),
  defaults = {
    // your defaults
  };

function example(options) {
  options = opex(defaults, options);
  // your code
}
  • opex always returns a new object -- no more of those extra curly braces just to keep your shared defaults from changing: extend({}, defaults, options)

  • params are collapsed from left to right into the new object; right-most values override any to the left

  • non-object and non-function parameters are ignored, which allows for simple input sanitizing

    consider:

var defaults = {};
function example(a, b, c) {
  var options = opex(defaults, a, b, c);
}

options will always be an object and opex will extend a new object with defaults and b even if a is null and c is an integer. It will also work if any parameter is a function which has had additional properties assigned to it, i.e. when a module wants to export a function but appends some helper data or functions to the exported function.

what about deep copy?

Only where it makes sense. opex is primarily indended to be used with JSON stuctures or a 'flat' object. Rather than use a flag (or always-on/off) like some implementations, opex decides whether to recurse on each property individually. Why? Deep copying can be dangerous. Best case, it's a little slower. Worst case? Non-enumerable properties get left behind while enumerable, this-dependent functions no longer behave as expected. To avoid these issues while still supporting deep copy for JSON-like structures (e.g. collapsing multiple config files based on application and environment), opex will only deep-copy simple objects and object literals. An object is deemed 'simple' when it's .__proto__ property is a direct reference to Object.prototype:

// which will recurse?
{}                // yes
new Object()      // yes
function x() { }  // no
[]                // no
5                 // no
{ y: [ 'foo' ] }  // yes, but y will not

You get the idea.. sound complicated? You'll find in most cases it's exactly what you would expect:

var opex = require('opex'),
  globalDefaults = {
    env: 'dev',
    log: {
      level: 'debug'
    }
  },
  appDefaults = {
    app: 'bar.com',
    log: {
      level: 'error'
    },
    key: 'bar'
  };

function App(options) {
  // options = {
  //   env: 'prod',
  //   signer: <Crypto.Signer instance>,
  //   customModule: <some custom utility>
  // }
  options = opex(globalDefaults, appDefaults, options);
  options.end;          // 'prod'
  options.log.level;    // 'error'
  options.key;          // 'bar'
  options.app;          // 'bar.com'
  options.singer;       // <Crypto.Signer instance> (non-deep copy)
  options.customModule; // <some custom utility> (non-deep copy)
}

install

npm install opex

test

npm install -g grunt-cli
npm test