Skip to content
This repository has been archived by the owner on Mar 16, 2022. It is now read-only.

Commit

Permalink
converted Store to Singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-fei committed Jun 7, 2015
1 parent eab4c09 commit 81309d9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 41 deletions.
2 changes: 1 addition & 1 deletion dest/jekyll-search.js

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

60 changes: 30 additions & 30 deletions src/Store.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
module.exports = function Store(_store){
var store = []
module.exports = {
put:put,
clear: clear,
get: get
}

if( isArray(_store) ){
addArray(_store)
}
var store = []

function isObject(obj){ return !!obj && Object.prototype.toString.call(obj) == '[object Object]' }
function isArray(obj){ return !!obj && Object.prototype.toString.call(obj) == '[object Array]' }
function put(data){
if( isObject(data) ) return addObject(data)
if( isArray(data) ) return addArray(data)
return undefined
}
function clear(){
store.length = 0
return store
}

function addObject(data){
store.push(data)
return data
}
function get(){
return store
}

function addArray(data){
var added = []
for (var i = 0; i < data.length; i++)
if( isObject(data[i]) )
added.push(addObject(data[i]))
return added
}

this.clear = function(){
store.length = 0
return store
}
function isObject(obj){ return !!obj && Object.prototype.toString.call(obj) == '[object Object]' }
function isArray(obj){ return !!obj && Object.prototype.toString.call(obj) == '[object Array]' }

this.get = function(){
return store
}
function addObject(data){
store.push(data)
return data
}

this.put = function(data){
if( isObject(data) ) return addObject(data)
if( isArray(data) ) return addArray(data)
return undefined
}
function addArray(data){
var added = []
for (var i = 0; i < data.length; i++)
if( isObject(data[i]) )
added.push(addObject(data[i]))
return added
}
4 changes: 1 addition & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

var searcher = require('./Searcher')
var templater = require('./Templater')
var Store = require('./Store')
var store = require('./Store')
var JSONLoader = require('./JSONLoader')

var store
var jsonLoader

var requiredOptions = [
Expand All @@ -29,7 +28,6 @@
window.SimpleJekyllSearch = function SimpleJekyllSearch(_opt){
opt = validateOptions(_opt)
searcher.setOptions(_opt)
store = new Store()
jsonLoader = new JSONLoader()

isJSON(opt.json) ?
Expand Down
12 changes: 5 additions & 7 deletions test/unit/StoreTest.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
describe("Store", function() {
var Store = require('../../src/Store.js');
var store;
var store = require('../../src/Store.js');

var foo = {foo:'bar'};
var foo1 = {foo1:'bar1'};

beforeEach(function() {
store = new Store;
store.clear();
});

it("should instanciate a store with data provided via constructor", function() {
store = new Store([foo]);
store.put([foo]);
expect(
store.get()
).toEqual(
Expand All @@ -25,7 +23,7 @@ describe("Store", function() {
store.clear()
).toEqual(
[]
);
);
});

it("should store single object", function() {
Expand All @@ -43,7 +41,7 @@ describe("Store", function() {
store.get()
).toEqual(
[{foo:'bar'},{foo1:'bar1'}]
);
);
});

it("should not add other things than objects", function() {
Expand All @@ -59,4 +57,4 @@ describe("Store", function() {
[foo,foo1]
);
});
});
});

0 comments on commit 81309d9

Please sign in to comment.