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

Commit

Permalink
Replace user-defined namespace with random namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed Aug 6, 2014
1 parent ad0b91d commit 513b80a
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 33 deletions.
2 changes: 1 addition & 1 deletion examples/after.js
Expand Up @@ -3,7 +3,7 @@
var plugin = require("../lib/plugin.js");
var afterPlugin;

afterPlugin = plugin("peerigon/after", function (obj) {
afterPlugin = plugin(function (obj) {
var self = this;

this(obj).after("someMethod", function (result) {
Expand Down
2 changes: 1 addition & 1 deletion examples/before.js
Expand Up @@ -3,7 +3,7 @@
var plugin = require("../lib/plugin.js");
var beforePlugin;

beforePlugin = plugin("peerigon/before", function (obj) {
beforePlugin = plugin(function (obj) {
var self = this;

this(obj).before("someMethod", function (number) {
Expand Down
2 changes: 1 addition & 1 deletion examples/define.js
Expand Up @@ -3,7 +3,7 @@
var plugin = require("../lib/plugin.js");
var definePlugin;

definePlugin = plugin("peerigon/define", function (obj) {
definePlugin = plugin(function (obj) {
this(obj).define("someValue", 2);
this(obj).define("someMethod", function () {
return this.someValue;
Expand Down
2 changes: 1 addition & 1 deletion examples/hook.js
Expand Up @@ -3,7 +3,7 @@
var plugin = require("../lib/plugin.js");
var hookPlugin;

hookPlugin = plugin("peerigon/hook", function (obj) {
hookPlugin = plugin(function (obj) {
this(obj).hook("someMethod", function () {
this.message = "hi";
});
Expand Down
2 changes: 1 addition & 1 deletion examples/override.js
Expand Up @@ -3,7 +3,7 @@
var plugin = require("../lib/plugin.js");
var overridePlugin;

overridePlugin = plugin("peerigon/override", function (obj) {
overridePlugin = plugin(function (obj) {
var pluginContext = this;

this(obj).override("someMethod", function (number) {
Expand Down
2 changes: 1 addition & 1 deletion examples/simple.js
Expand Up @@ -3,7 +3,7 @@
var plugin = require("../lib/plugin.js");
var simplePlugin;

simplePlugin = plugin("peerigon/simple", function (obj) {
simplePlugin = plugin(function (obj) {
obj.newNumber = 2;
});

Expand Down
2 changes: 1 addition & 1 deletion examples/store.js
Expand Up @@ -3,7 +3,7 @@
var plugin = require("../lib/plugin.js");
var storePlugin;

storePlugin = plugin("peerigon/store", function (obj) {
storePlugin = plugin(function (obj) {
var self = this;

this(obj).store().newNumber = 2;
Expand Down
24 changes: 17 additions & 7 deletions lib/plugin.js
@@ -1,12 +1,8 @@
"use strict";

function plugin(namespace, fn) {
if (typeof namespace !== "string") {
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 plugin(fn) {
// Generating random namespace. Should be enough randomness for our use-case.
var namespace = Math.random().toString() + Math.random().toString() + Math.random().toString();

function pluginContext(target) {
pluginContext.target = target;
Expand Down Expand Up @@ -157,4 +153,18 @@ function createStore(obj, key) {
return value;
}

/**
* @see http://stackoverflow.com/a/8809472
* @returns {string}
*/
function generateUUID(){
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x7|0x8)).toString(16);
});
return uuid;
}

module.exports = plugin;
24 changes: 5 additions & 19 deletions test/plugin.test.js
Expand Up @@ -18,25 +18,18 @@ describe("plugin", function () {

});

describe("plugin(namespace, fn)", function () {
describe("plugin(fn)", function () {

it("should return a function", function () {
expect(plugin("testPlugin", function () {})).to.be.a("function");
});

it("should throw an error if namespace is not a string", function () {
expect(function () {
// that's probably the most common error: we've forgotten to pass a namespace
plugin(function () {});
}).to.throw("Cannot create plugin: namespace should be a string, instead saw function");
expect(plugin(function () {})).to.be.a("function");
});

describe("calling the returned function with (obj, config?)", function () {
var spy, newPlugin, obj;

beforeEach(function () {
spy = sinon.spy();
newPlugin = plugin("testPlugin", spy);
newPlugin = plugin(spy);
obj = {};
});

Expand Down Expand Up @@ -73,7 +66,7 @@ describe("fn's context", function () {

function createPlugin(fn) {
spy = sinon.spy(fn);
newPlugin = plugin("testPlugin", spy);
newPlugin = plugin(spy);
}

function applyPlugin() {
Expand Down Expand Up @@ -101,20 +94,13 @@ describe("fn's context", function () {
applyPlugin();
});

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

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

expect(Object.keys(target)).to.not.contain("plugin/testPlugin");
expect(Object.keys(target)).to.eql([]);
});

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

0 comments on commit 513b80a

Please sign in to comment.