Skip to content

Commit

Permalink
Further updating how context works. Improving the api a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds committed Dec 19, 2013
1 parent e8eea73 commit dfd773f
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 44 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,17 @@ genie.mergeWishes(wishes);
// Also sets an internal variable: _previousContext for the revertContext function
genie.context(newContext [string || array | optional]);

// Sets and returns the current context to the default context (universe)
// Adds the context(s) to genie's current context
genie.addContext(newContext [string || array | optional]);

// Removes the context(s) to genie's current context
genie.removeContext(newContext [string || array | optional]);

// Sets and returns the current context to the default context: ['universe']
genie.restoreContext();

// Sets and returns the current context to the previous context
// The previous context is updated when context, addContext, removeContext, restoreContext, and revertContext are called.
genie.revertContext();

// Sets and returns the enabled state
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "genie",
"main": "genie.js",
"version": "0.2.0",
"version": "0.2.1",
"homepage": "https://github.com/kentcdodds/genie",
"authors": [
"Kent C. Dodds <[email protected]>"
Expand Down
61 changes: 45 additions & 16 deletions genie.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
var _wishes = {},
_previousId = 0,
_enteredMagicWords = {},
_defaultContext = 'universe',
_defaultContext = ['universe'],
_context = _defaultContext,
_previousContext = _defaultContext,
_enabled = true,
Expand Down Expand Up @@ -119,7 +119,7 @@
noWishMerge: true,
previousId: 0,
enteredMagicWords: [],
contexts: _defaultContext,
context: _defaultContext,
previousContext: _defaultContext,
enabled: true
});
Expand Down Expand Up @@ -306,33 +306,28 @@
}

function _wishInContext(wish) {
var currentContextIsDefault = _context.indexOf(_defaultContext) > -1;
var currentContextIsDefault = _context === _defaultContext;
var wishContextIsDefault = wish.context === _defaultContext;
var wishContextIsCurrentContext = wish.context === _context;
if (currentContextIsDefault || wishContextIsDefault || wishContextIsCurrentContext) {
return true;
}

var wishContextInContext = false;
var contextInWishContext = false;

var contextIsArray = _context instanceof Array;
var wishContextIsArray = wish.context instanceof Array;
var contextIsString = typeof _context === 'string';
var wishContextIsString = typeof wish.context === 'string';

if (wishContextIsString && contextIsArray) {
if (typeof wish.context === 'string') {
wishContextInContext = _context.indexOf(wish.context) > -1;
} else if (contextIsString && wishContextIsArray) {
contextInWishContext = wish.context.indexOf(_context) > -1;
} else if (contextIsArray && wishContextIsArray) {
} else if (wish.context instanceof Array) {
for (var i = 0; i < _context.length; i++) {
if (wish.context.indexOf(_context[i])) {
if (wish.context.indexOf(_context[i]) > -1) {
wishContextInContext = true;
break;
}
}
}

return currentContextIsDefault || wishContextIsDefault || wishContextIsCurrentContext ||
wishContextInContext || contextInWishContext;
return wishContextInContext || contextInWishContext;
}

// Begin API functions. //
Expand All @@ -357,7 +352,7 @@
wishes: _wishes,
previousId: _previousId,
enteredMagicWords: _enteredMagicWords,
contexts: _context,
context: _context,
previousContext: _previousContext,
enabled: _enabled
};
Expand All @@ -382,11 +377,43 @@
function context(newContext) {
if (newContext !== undefined) {
_previousContext = _context;
if (typeof newContext === 'string') {
newContext = [newContext];
}
_context = newContext;
}
return _context;
}

function addContext(newContext) {
_previousContext = _context;
if (newContext instanceof Array) {
_context = _context.concat(newContext);
} else {
_context.push(newContext);
}
}

function removeContext(contextToRemove) {
_previousContext = _context;
var isArray = contextToRemove instanceof Array;
var i = 0;
var ctx;

while(i < _context.length) {
ctx = _context[i];
if ((isArray && contextToRemove.indexOf(ctx) > -1) || ctx === contextToRemove) {
_context.splice(i, 1);
} else {
i++;
}
}

if (!_context.length) {
_context = _defaultContext;
}
}

function revertContext() {
return context(_previousContext);
}
Expand Down Expand Up @@ -429,6 +456,8 @@
genie.deregisterWish = _passThrough(deregisterWish, {});
genie.reset = _passThrough(reset, {});
genie.context = _passThrough(context, '');
genie.addContext = _passThrough(addContext, '');
genie.removeContext = _passThrough(removeContext, '');
genie.revertContext = _passThrough(revertContext, '');
genie.restoreContext = _passThrough(restoreContext, '');
genie.enabled = _passThrough(enabled, false);
Expand Down
4 changes: 2 additions & 2 deletions genie.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 60 additions & 24 deletions test/tests.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// tests.js

function getBlankWish(magicWords) {
function fillInWish(defaults) {
defaults = defaults || {};
return {
magicWords: magicWords,
action: function() {}
id: defaults.id,
context: defaults.context,
data: defaults.data,
magicWords: defaults.magicWords || 'magicWords',
action: defaults.action || function() {}
};
}
if (typeof require !== 'undefined') {
Expand Down Expand Up @@ -136,30 +140,28 @@ describe('genie', function(){

});

describe('#context', function() {
var defaultContextWish, newContextWish1, newContextWish2, multiContextWish;
describe('#context #addContext #removeContext', function() {
var defaultContextWish;
var allWishCount = 5;
beforeEach(function(done) {
defaultContextWish = genie(getBlankWish('wish0'));
newContextWish1 = genie({
magicWords: 'wish1',
context: 'new-context1',
action: function(){}
});
newContextWish2 = genie({
magicWords: 'wish2',
context: 'new-context2',
action: function(){}
});
multiContextWish = genie({
magicWords: 'wish2',
context: ['new-context1', 'new-context2', 'unique-context'],
action: function(){}
});
defaultContextWish = genie(fillInWish());
newContextWish1 = genie(fillInWish({
context: 'context1'
}));
newContextWish2 = genie(fillInWish({
context: 'context2'
}));
newContextWish3 = genie(fillInWish({
context: ['context3'],
}));
multiContextWish = genie(fillInWish({
context: ['context1', 'context2', 'context3'],
}));
done();
});
it('should have all wishes when genie.context is default', function() {
var allWishes = genie.getMatchingWishes();
expect(allWishes).to.have.length(4);
expect(allWishes).to.have.length(allWishCount);
});

it('should have only wishes with default context when genie.context is not default', function() {
Expand All @@ -170,15 +172,49 @@ describe('genie', function(){
});

it('should have only in context wishes (including default context wishes) when genie.context is not default', function() {
genie.context('new-context1');
genie.context('context1');
var allWishes = genie.getMatchingWishes();
expect(allWishes).to.have.length(3);
});

it('should be able to have multiple contexts', function() {
genie.context(['new-context1', 'new-context2']);
genie.context(['context1', 'context2']);
var allWishes = genie.getMatchingWishes();
expect(allWishes).to.have.length(4);
});

it('should be able to add a string context', function() {
genie.context('context1');
genie.addContext('context2');
var allWishes = genie.getMatchingWishes();
expect(allWishes).to.have.length(4);
});

it('should be able to add an array of contexts', function() {
genie.context('context1');
genie.addContext(['context2', 'context3']);
var allWishes = genie.getMatchingWishes();
expect(allWishes).to.have.length(allWishCount);
});

it('should be able to remove string context', function() {
genie.context(['context1', 'context2']);
var allWishes = genie.getMatchingWishes();
expect(allWishes).to.have.length(4);

genie.removeContext('context1');
var allWishes = genie.getMatchingWishes();
expect(allWishes).to.have.length(3);
});

it('should be able to remove an array of contexts', function() {
genie.context(['context1', 'context2', 'context3']);
var allWishes = genie.getMatchingWishes();
expect(allWishes).to.have.length(allWishCount);

genie.removeContext(['context1', 'context2']);
var allWishes = genie.getMatchingWishes();
expect(allWishes).to.have.length(3);
});
});
});

0 comments on commit dfd773f

Please sign in to comment.