bigbiff | d006b30 | 2015-03-06 18:36:03 -0500 | [diff] [blame] | 1 | module.exports = function Store(_store){ |
| 2 | var self = this; |
| 3 | |
| 4 | var store = []; |
| 5 | |
| 6 | if( isArray(_store) ){ |
| 7 | addArray(_store); |
| 8 | } |
| 9 | |
| 10 | function isObject(obj){ return !!obj && Object.prototype.toString.call(obj) == '[object Object]'; } |
| 11 | function isArray(obj){ return !!obj && Object.prototype.toString.call(obj) == '[object Array]'; } |
| 12 | |
| 13 | function addObject(data){ |
| 14 | store.push(data); |
| 15 | return data; |
| 16 | } |
| 17 | |
| 18 | function addArray(data){ |
| 19 | var added = []; |
| 20 | for (var i = 0; i < data.length; i++) |
| 21 | if( isObject(data[i]) ) |
| 22 | added.push(addObject(data[i])); |
| 23 | return added; |
| 24 | } |
| 25 | |
| 26 | self.clear = function(){ |
| 27 | store.length = 0; |
| 28 | return store; |
| 29 | }; |
| 30 | |
| 31 | self.get = function(){ |
| 32 | return store; |
| 33 | }; |
| 34 | |
| 35 | self.put = function(data){ |
| 36 | if( isObject(data) ) return addObject(data); |
| 37 | if( isArray(data) ) return addArray(data); |
| 38 | return undefined; |
| 39 | }; |
| 40 | }; |