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

myriad.js

v0.3.2

Published

JavaScript adaptation of Ruby's method_missing and PHP's __call without using Harmony's Proxy/Firefox's __noSuchMethod.

Downloads

19

Readme

Myriad.js

With Myriad it is possible to mimic the behaviour of Ruby's method_missing method, and PHP's __call. Since JavaScript doesn't support this type of behaviour natively – apart from Firefox's __noSuchMethod, Myriad.js was created to fill the gap!

Examples

// Single property.
myriad.getByName();

// Multiple properties.
myriad.getByNameAndLocationAndSex();

// Arguments.
myriad.getByNameAndLocationAndSex('male');

All of which can be created without any manual method creating at all. In fact, all possible combinations are created so you can construct your method names however you like.

Getting Started

In order to get started, Myriad only requires a couple of parameters from you to begin – the model to create the methods from, and the callback property for the function to invoke.

new Myriad(model, function callback(properties, args) {

});

Myriad has a third argument which accepts a hash for setting additional options.

Since Myriad has no idea on the desired scope for the callback method, you should use bind (or Underscore's bind) to enforce the context.

_.bind(function callbackWithStringScope() {

    // "String as a scope!?"
    console.log(this);

}, 'String as a scope!?');

By default Myriad will only delve 3 levels deep into your model, which means that getByNameAndSexAndLocation would be created, but getByNameAndSexAndLocationAndEmployed would not because that's 4 properties. To change the default behaviour to include the latter, you can specify the depth option to set it to 4 or above (or less). For no limit at all you can set the property to Infinity – but be careful.

Options

Sometimes wild beasts need to be tamed! Myriad has a set of options for modifying and restricting the behaviour.

Implementation

Since JavaScript does not natively support any method_missing, __call, or even Python's __getattr__, Myriad uses recursion to generate all possible combinations from your model's properties. Each created method has attached the properties that were used to create it in the first place – which are then passed through to your eventual callback.

Myriad is comprised of essentially two methods – firstly _addIteration is invoked passing in the first property that's discovered on your model. Since Myriad knows of all the properties on your model a difference can be calculated to determine which other properties are required to be attached to it – _addIteration is then called iteratively, adding a new property onto it each and every time until difference eventually yields an empty array.

  • _addIteration(['name']);
  • _addIteration(['name', 'age']);
  • _addIteration(['name', 'age', 'location']);
  • _addIteration(['name']);
  • _addIteration(['name', 'location']);
  • _addIteration(['name', 'location', 'age']);

Once the combinations have been generated, the _createMethod method takes over to generate those methods with the corresponding properties used to generate it.

Contributing

All contributions are welcome provided they come with unit tests. Myriad's tests are written in Mocha in conjunction with Should.js – please refer to the tests/spec.js.

Once you've added your contribution, please open a pull request for it to be merged into master.