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

amp-derivedify

v1.0.2

Published

Simple util for making derived property helpers

Downloads

2

Readme

amp-derivedify

Simple util for making derived property helpers. This is fairly meta, so we'll start simple. If you just want to dive into examples, the tests are a good place to start.

ampersand-state allows you to define derived properties like so:

var Person = AmpersandState.extend({
  props: {
      active: 'boolean',
  },
  derived: {
    inActive: {
      deps: ['active'],
      fn: function () {
        return !this.active;
      }
    }
  }
});

You probably wouldn't define a separate property just for !active, especially when it's so verbose, but just humour me for a moment. This is pretty nice and expressive, but imagine I'm feeling especially functional, so I define a not function, like this:

var not = function(bool){ return !bool; };
// ...
  derived: {
    inActive: {
      deps: ['active'],
      fn: function () {
        return not(this.active);
      }
    }
  }

I have attempted to find a similarly defined function on npm without success, which is strange. At any rate, this doesn't seem much better. However, what about if I write a new derivedNot function, one designed for derived properties instead of boolean literals?

var derivedNot = function(propName){
  return {
    deps: [propName],
    fn: function(){
        return not(this[propName]);
    }
  };
}
// ...
    derived: {
      inActive: derivedNot('active')
    }

Now my derived property section has started to look quite nice and we can save derivedNot in a helper file and use it anywhere we need it. In my opinion this is terse, but readable. As it happens the logic involved in creating derivedNot from not is totally abstractable, and it can work the same way for all pure functions. This is what derivedify does.

var not = function(bool){ return !bool; };
var derivedNot = derivedify(not);

var Person = AmpersandState.extend({
    props: {
        active: 'boolean',
    },
    derived: {
        inActive: derivedNot('active')
    }
});

Another example:

  derived: {
    activeUsers: {
      deps: ['users'],
      fn: function(){
        return _.filter(this.users, 'active');
      }
    }
  }
var derivedFilter = derivedify(_.filter);
  ...
  derived: {
    activeUsers: derivedFilter('users', derivedify.l('active'))
  }

The important thing to note is how the arguments to derivedFilter relate to the arguments to _.filter - derivedFilter accepts property names and other derived properties as arguments instead of array and string literals which _.filter accepts. You'll notice that when we want to pass a string literal (such as 'active'), instead of a property name, we need to wrap it in in derivedify.l (l stands for literal) so that derivedify doesn't search for a property named active, which doesn't exist.

derivedify(func)

  • func {function} A pure function - it should deal only with inputs and outputs and have no side effects.

Pass in any pure function which accepts some inputs. Returns a function derivedFunc which accepts property names or other derived property definitions and returns a derived property definition. This derived property definition will depend on any passed property names and it will invoke func with those properties as inputs.

derivedify.l(literal)

  • literal any javascript literal - strings, numbers, objects, arrays and booleans are all fine

When defining derived properties that should depend on literal strings, as opposed to strings as property names, use derivedify.l to wrap the literal in its own derived property.

Composition

Because functions created using derivedify can accept either property names or other derived property definitions as inputs, they are totally composable. If you find yourself making intermediate properties, just remember you do have the option to chain them:

  derived: {
    filteredUsers: derivedFilter('users', derivedify.l('active')),
    filteredUserNames: derivedMap('filteredUsers', derivedify.l('name')),
    firstFilteredUserName: derivedFirst('filteredUserNames')
  }
  derived: {
    firstFilteredUserName: derivedFirst(
            derivedMap(
              derivedFilter('users', derivedify.l('active')),
            derivedify.l('name')))
  }

Granted, this is ugly as hell. derivedify just gives you power, keeping things readable is your problem, one solution is to use lodash's function utilities:

  derived: {
    firstFilteredUserName: _.chain(derivedFilter('users', derivedify.l('active')))
                            .thru(_.partialRight(derivedMap, derivedify.l('name')))
                            .thru(derivedFirst)
                            .value()
  }

Of course, this is still quite awkward to read, at least initially. Caveat emptor, Your mileage may vary, etc.