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

Commit 81309d9

Browse files
committed
converted Store to Singleton
1 parent eab4c09 commit 81309d9

File tree

4 files changed

+37
-41
lines changed

4 files changed

+37
-41
lines changed

dest/jekyll-search.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Store.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
module.exports = function Store(_store){
2-
var store = []
1+
module.exports = {
2+
put:put,
3+
clear: clear,
4+
get: get
5+
}
36

4-
if( isArray(_store) ){
5-
addArray(_store)
6-
}
7+
var store = []
78

8-
function isObject(obj){ return !!obj && Object.prototype.toString.call(obj) == '[object Object]' }
9-
function isArray(obj){ return !!obj && Object.prototype.toString.call(obj) == '[object Array]' }
9+
function put(data){
10+
if( isObject(data) ) return addObject(data)
11+
if( isArray(data) ) return addArray(data)
12+
return undefined
13+
}
14+
function clear(){
15+
store.length = 0
16+
return store
17+
}
1018

11-
function addObject(data){
12-
store.push(data)
13-
return data
14-
}
19+
function get(){
20+
return store
21+
}
1522

16-
function addArray(data){
17-
var added = []
18-
for (var i = 0; i < data.length; i++)
19-
if( isObject(data[i]) )
20-
added.push(addObject(data[i]))
21-
return added
22-
}
2323

24-
this.clear = function(){
25-
store.length = 0
26-
return store
27-
}
24+
function isObject(obj){ return !!obj && Object.prototype.toString.call(obj) == '[object Object]' }
25+
function isArray(obj){ return !!obj && Object.prototype.toString.call(obj) == '[object Array]' }
2826

29-
this.get = function(){
30-
return store
31-
}
27+
function addObject(data){
28+
store.push(data)
29+
return data
30+
}
3231

33-
this.put = function(data){
34-
if( isObject(data) ) return addObject(data)
35-
if( isArray(data) ) return addArray(data)
36-
return undefined
37-
}
32+
function addArray(data){
33+
var added = []
34+
for (var i = 0; i < data.length; i++)
35+
if( isObject(data[i]) )
36+
added.push(addObject(data[i]))
37+
return added
3838
}

src/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
var searcher = require('./Searcher')
55
var templater = require('./Templater')
6-
var Store = require('./Store')
6+
var store = require('./Store')
77
var JSONLoader = require('./JSONLoader')
88

9-
var store
109
var jsonLoader
1110

1211
var requiredOptions = [
@@ -29,7 +28,6 @@
2928
window.SimpleJekyllSearch = function SimpleJekyllSearch(_opt){
3029
opt = validateOptions(_opt)
3130
searcher.setOptions(_opt)
32-
store = new Store()
3331
jsonLoader = new JSONLoader()
3432

3533
isJSON(opt.json) ?

test/unit/StoreTest.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
describe("Store", function() {
2-
var Store = require('../../src/Store.js');
3-
var store;
2+
var store = require('../../src/Store.js');
43

54
var foo = {foo:'bar'};
65
var foo1 = {foo1:'bar1'};
76

87
beforeEach(function() {
9-
store = new Store;
108
store.clear();
119
});
1210

1311
it("should instanciate a store with data provided via constructor", function() {
14-
store = new Store([foo]);
12+
store.put([foo]);
1513
expect(
1614
store.get()
1715
).toEqual(
@@ -25,7 +23,7 @@ describe("Store", function() {
2523
store.clear()
2624
).toEqual(
2725
[]
28-
);
26+
);
2927
});
3028

3129
it("should store single object", function() {
@@ -43,7 +41,7 @@ describe("Store", function() {
4341
store.get()
4442
).toEqual(
4543
[{foo:'bar'},{foo1:'bar1'}]
46-
);
44+
);
4745
});
4846

4947
it("should not add other things than objects", function() {
@@ -59,4 +57,4 @@ describe("Store", function() {
5957
[foo,foo1]
6058
);
6159
});
62-
});
60+
});

0 commit comments

Comments
 (0)