Skip to content
This repository has been archived by the owner on Jul 12, 2018. It is now read-only.

Commit

Permalink
Add 'plugin/'-prefix to namespace and make store not enumerable
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed Aug 6, 2014
1 parent c90fb66 commit d5c0b26
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
16 changes: 14 additions & 2 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ function plugin(namespace, fn) {
throw new TypeError("Cannot create plugin: namespace should be a string, instead saw " + truncate(namespace));
}

// Prefix the namespace with 'plugin/' for a better debugging experience
namespace = "plugin/" + namespace;

function pluginContext(target) {
pluginContext.target = target;

Expand All @@ -15,9 +18,18 @@ function plugin(namespace, fn) {

pluginContext.target = null;

// TODO Prefix namespace with 'plugin/'
pluginContext.store = function () {
return pluginContext.target[namespace] || (pluginContext.target[namespace] = {});
var store = pluginContext.target[namespace];

if (!store) {
store = {};
Object.defineProperty(pluginContext.target, namespace, {
enumerable: false,
value: store
});
}

return store;
};

pluginContext.define = function (key, value) {
Expand Down
13 changes: 11 additions & 2 deletions test/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,21 @@ describe("fn's context", function () {
applyPlugin();
});

it("should store the returned object under obj[pluginNamespace]", function () {
it("should store the returned object under obj['plugin/' + pluginNamespace]", function () {
createPlugin(function () {
expect(this(obj).store()).to.equal(obj.testPlugin);
expect(this(obj).store()).to.equal(obj["plugin/testPlugin"]);
});
applyPlugin();
});

it("should make the store not enumerable", function () {
createPlugin(function () {
this(obj).store();
});
applyPlugin();

expect(Object.keys(obj)).to.not.contain("plugin/testPlugin");
});

it("should return always the same object for obj", function () {
createPlugin(function () {
Expand Down

0 comments on commit d5c0b26

Please sign in to comment.