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

nectar.js

v0.1.7

Published

JavaScript dependency injection library.

Downloads

15

Readme

#nectar.js

nectar.js is a JavaScript dependency injection library intended to help build loosely coupled JavaScript applications.

Works with modern browsers and Node.Js.

##Install

npm install nectar.js

##Usage

###Initialize

var nectar = new Nectar();

The instantiated nectar object can be stored in global scope e.g.

window.nectar = new Nectar();

Alternatively, the nectar instance can be injected into functions to be resolved e.g.

function fred(nectar) {
    alert(nectar.name);
}

(function () {
    var nectar = new Nectar();

    nectar.inject(fred);
}());

By default, the nectar instance is automatically registered so it can be injected. Therefore, it is not a good idea to call any of your function parameter names "nectar". Alternatively, this can be changed to use another name e.g.

function fred(bob, nectar) {
    alert(bob.name +  " says nectar is " + typeof nectar);
}

(function () {
    var nectar = new Nectar("bob");

    nectar.inject(fred);
}());

###register

register - stores an object to be resolved using the given key. Put simply, what you put in is what you get back when resolve is called.

nectar.register("oh", {my : "object"});

nectar.resolve("oh");
{my : "object"}

###registerSingleton

Registers an object to be resolved later. When the object is resolved for the first time, a new instance will be instantiated. Subsequent resolutions against this key will return the first instantiated instance. The object must have prototype methods defined in order to be instantiated during the resolve process, else the object will be returned as is.

var WhatIsTheDate = function () {
    this.date = new Date();
};

WhatIsTheDate.prototype = {
    theDate : function () {
        return this.date;
    }
};

nectar.registerSingleton("whatIsTheDate", WhatIsTheDate);

var date1 = nectar.resolve("whatIsTheDate").theDate();
alert("The date is, and will always be " + date1);

var date2 = nectar.resolve("whatIsTheDate").theDate();
alert("The date is, and will always be " + date2);

jsfiddle - registerSingleton example

###registerFactory

Registers an object that will be resolved later. Every time the resolve method is called, a new instance (if applicable) of the object will be instantiated. The object must have prototype methods defined in order to be instantiated during the resolve process, else the object will be returned as is.

var WhatIsTheDate = function () {
    this.date = new Date();
};

WhatIsTheDate.prototype = {
    theDate : function () {
        return this.date;
    }
};

nectar.registerFactory("whatIsTheDate", WhatIsTheDate);

var date1 = nectar.resolve("whatIsTheDate").theDate();
alert("The date will keep changing " + date1);

var date2 = nectar.resolve("whatIsTheDate").theDate();
alert("The date will keep changing " + date2);

jsfiddle - registerFactory example

###inject Injects into the given function, any resolvable arguments based on their name. Any unresolved arguments will be set to undefined.

nectar.register("iexist", "hello");

nectar.inject(function (iexist, idontexist) {

    alert("iexist and say " + iexist);
    alert("idontexist an am " + typeof idontexist);

});

jsfiddle - inject example