Skip to content

Commit ce0f48f

Browse files
author
Mathieu Ghaleb
committed
Merge branch 'madglory-get-keys'
2 parents ad25267 + 00dda29 commit ce0f48f

7 files changed

Lines changed: 301 additions & 74 deletions

File tree

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ basil = new window.Basil(options);
1111

1212
// basic methods
1313
basil.set('foo', 'bar'); // store 'bar' value under 'foo' key
14+
basil.set('abc', 'xyz'); // store 'xyz' value under 'abc' key
1415
basil.get('foo'); // returns 'bar'
16+
basil.keys(); // returns ['abc', 'foo']
17+
basil.keysMap(); // returns { 'abc': ['local'], 'foo': ['local'] }
1518
basil.remove('foo'); // remove 'foo' value
1619

1720
// advanced methods
@@ -22,6 +25,8 @@ basil.reset(); // reset all stored values under namespace for current storage
2225

2326
## Advanced Usage
2427

28+
### Storages
29+
2530
```javascript
2631
basil = new window.Basil(options);
2732

@@ -32,6 +37,17 @@ basil.set('foo', 'bar', { 'storages': ['local'] });
3237
// set 'bar' value under 'foo' key in localStorage AND cookie
3338
basil.set('foo', 'bar', { 'storages': ['local', 'cookie'] });
3439

40+
// set 'xyz' value under 'abc' key in memory
41+
basil.set('abc', 'xyz', { 'storages': ['memory'] });
42+
43+
// retrieve keys
44+
basil.keys(); // returns ['foo', 'abc']
45+
basic.keys({ 'storages': ['memory'] }); // returns ['abc']
46+
47+
// retrive keys map
48+
basil.keysMap(); // returns { 'foo': ['local', 'cookie'], 'abc': ['memory'] }
49+
basic.keysMap({ 'storages': ['memory'] }); // returns { 'abc': ['memory'] }
50+
3551
// Access native storages
3652
// With basil API, but without namespace nor JSON parsing for values
3753

@@ -48,6 +64,39 @@ basil.sessionStorage.get(key);
4864
basil.sessionStorage.set(key, value);
4965
```
5066

67+
### Namespaces
68+
69+
```javascript
70+
basil = new window.Basil(options);
71+
72+
// store data under default namespace
73+
basil.set('hello', 'world');
74+
75+
// store data under a given namespace
76+
basil.set('hello', 42, { 'namespace': 'alt' });
77+
basil.set('abc', 'def', { 'namespace': 'alt', 'storages': ['memory'] });
78+
79+
// retrieve data
80+
basil.get('hello'); // return 'world'
81+
basil.get('hello', { 'namespace': 'alt' }); // return 42
82+
83+
// retrieves keys
84+
basil.keys(); // returns ['hello']
85+
basil.keys({ 'namespace': 'alt' }); // returns ['hello', 'abc']
86+
87+
// retrieves keys map
88+
basil.keysMap(); // returns { 'hello': ['local'] }
89+
basil.keysMap({ 'namespace': 'alt' }); // returns { 'hello': ['local'], 'abc': ['memory'] }
90+
91+
// remove data under a given namespace
92+
basil.remove('hello', { 'namespace': 'alt' });
93+
basil.get('hello'); // return 'world'
94+
basil.get('hello', { 'namespace': 'alt' }); // return null
95+
96+
// reset data under a given namespace
97+
basil.reset({ 'namespace': 'alt', 'storages': ['local', 'memory']});
98+
```
99+
51100
## Configuration
52101

53102
Here is the whole `options` object that you could give to Basil:

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "basil.js",
3-
"version": "0.3.2",
3+
"version": "0.3.3",
44
"homepage": "https://github.com/Wisembly/basil.js",
55
"authors": [
66
"Mathieu Ghaleb <mathieu@wisembly.com>"

build/basil.js

Lines changed: 88 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
(function () {
2-
32
// Basil
43
var Basil = function (options) {
5-
return new Basil.Storage().init(options);
4+
return Basil.utils.extend(Basil.plugins, new Basil.Storage().init(options));
65
};
76

87
// Version
9-
Basil.version = '0.3.2';
8+
Basil.version = '0.3.3';
109

1110
// Utils
1211
Basil.utils = {
@@ -18,9 +17,14 @@
1817
destination[property] = arguments[i][property];
1918
}
2019
return destination;
20+
},
21+
registerPlugin: function (methods) {
22+
Basil.plugins = this.extend(methods, Basil.plugins);
2123
}
2224
};
2325

26+
Basil.plugins = {};
27+
2428
// Options
2529
Basil.options = Basil.utils.extend({
2630
namespace: 'b45i1',
@@ -52,6 +56,9 @@
5256
}
5357
return key;
5458
},
59+
_toKeyName = function (namespace, name) {
60+
return name.replace(namespace + ':', '');
61+
},
5562
_toStoredValue = function (value) {
5663
return JSON.stringify(value);
5764
},
@@ -83,13 +90,22 @@
8390
this.engine.removeItem(name);
8491
},
8592
reset: function (namespace) {
86-
for (var key, i = 0; i < this.engine.length; i++) {
93+
for (var i = 0, key; i < this.engine.length; i++) {
8794
key = this.engine.key(i);
8895
if (key.indexOf(namespace) === 0) {
8996
this.remove(key);
9097
i--;
9198
}
9299
}
100+
},
101+
keys: function (namespace) {
102+
var keys = [];
103+
for (var i = 0, key; i < this.engine.length; i++) {
104+
key = this.engine.key(i);
105+
if (key.indexOf(namespace) === 0)
106+
keys.push(_toKeyName(namespace, key));
107+
}
108+
return keys;
93109
}
94110
};
95111

@@ -120,6 +136,13 @@
120136
if (key.indexOf(namespace) === 0)
121137
this.remove(key);
122138
}
139+
},
140+
keys: function (namespace) {
141+
var keys = [];
142+
for (var key in this._hash)
143+
if (key.indexOf(namespace) === 0)
144+
keys.push(_toKeyName(namespace, key));
145+
return keys;
123146
}
124147
};
125148

@@ -144,7 +167,6 @@
144167
},
145168
get: function (name) {
146169
var cookies = document.cookie.split(';');
147-
148170
for (var i = 0; i < cookies.length; i++) {
149171
var cookie = cookies[i].replace(/^\s*/, '');
150172
if (cookie.indexOf(name + '=') === 0)
@@ -157,7 +179,6 @@
157179
return;
158180
// remove cookie from main domain
159181
this.set(name, '', { expireDays: -1 });
160-
161182
// remove cookie from upper domains
162183
var domainParts = document.domain.split('.');
163184
for (var i = domainParts.length - 1; i > 0; i--) {
@@ -166,35 +187,42 @@
166187
},
167188
reset: function (namespace) {
168189
var cookies = document.cookie.split(';');
169-
170190
for (var i = 0; i < cookies.length; i++) {
171191
var cookie = cookies[i].replace(/^\s*/, ''),
172192
key = cookie.substr(0, cookie.indexOf('='));
173193
if (key.indexOf(namespace) === 0)
174194
this.remove(key);
175195
}
196+
},
197+
keys: function (namespace) {
198+
var keys = [],
199+
cookies = document.cookie.split(';');
200+
for (var i = 0; i < cookies.length; i++) {
201+
var cookie = cookies[i].replace(/^\s*/, ''),
202+
key = cookie.substr(0, cookie.indexOf('='));
203+
if (key.indexOf(namespace) === 0)
204+
keys.push(_toKeyName(namespace, key));
205+
}
206+
return keys;
176207
}
177208
};
178209

179210
return {
180211
init: function (options) {
181212
this.options = Basil.utils.extend({}, Basil.options, options);
182-
183213
this.supportedStorages = {};
184214
for (var i = 0, storage; i < this.options.storages.length; i++) {
185215
storage = this.options.storages[i];
186216
if (_storages.hasOwnProperty(storage))
187217
this.supportedStorages[storage] = _storages[storage];
188218
}
189-
190219
this.defaultStorage = this.check(this.options.storage) ? this.options.storage : this.detect();
191220
return this;
192221
},
193222
detect: function () {
194-
for (var storage in this.supportedStorages) {
223+
for (var storage in this.supportedStorages)
195224
if (this.check(storage))
196225
return storage;
197-
}
198226
return null;
199227
},
200228
check: function (storage) {
@@ -204,56 +232,82 @@
204232
return false;
205233
},
206234
set: function (name, value, options) {
207-
if (!(name = _toStoredKey(this.options.namespace, name)))
235+
options = options || {};
236+
if (!(name = _toStoredKey(options.namespace || this.options.namespace, name)))
208237
return;
209238
value = _toStoredValue(value);
210239
options = Basil.utils.extend({
211240
expireDays: this.options.expireDays
212241
}, options);
213-
214242
var storages = _toStoragesArray(options.storages) || [this.defaultStorage];
215-
for (var i = 0; i < storages.length; i++) {
216-
if (!this.check(storages[i]))
243+
for (var i = 0, storage; i < storages.length; i++) {
244+
storage = storages[i];
245+
if (!this.check(storage))
217246
continue;
218-
_storages[storages[i]].set(name, value, options);
247+
_storages[storage].set(name, value, options);
219248
}
220249
},
221250
get: function (name, options) {
222-
if (!(name = _toStoredKey(this.options.namespace, name)))
223-
return null;
224251
options = options || {};
225-
252+
if (!(name = _toStoredKey(options.namespace || this.options.namespace, name)))
253+
return null;
226254
var value = null,
227255
storages = _toStoragesArray(options.storages) || [this.defaultStorage];
228-
229-
for (var i = 0; value === null && i < storages.length; i++) {
230-
if (!this.check(storages[i]))
256+
for (var i = 0, storage; value === null && i < storages.length; i++) {
257+
storage = storages[i];
258+
if (!this.check(storage))
231259
continue;
232-
value = _fromStoredValue(_storages[storages[i]].get(name));
260+
value = _fromStoredValue(_storages[storage].get(name));
233261
}
234262
return value;
235263
},
236264
remove: function (name, options) {
237-
if (!(name = _toStoredKey(this.options.namespace, name)))
238-
return null;
239265
options = options || {};
240-
266+
if (!(name = _toStoredKey(options.namespace || this.options.namespace, name)))
267+
return null;
241268
var storages = _toStoragesArray(options.storages) || [this.defaultStorage];
242-
for (var i = 0; i < storages.length; i++) {
243-
if (!this.check(storages[i]))
269+
for (var i = 0, storage; i < storages.length; i++) {
270+
storage = storages[i];
271+
if (!this.check(storage))
244272
continue;
245-
_storages[storages[i]].remove(name);
273+
_storages[storage].remove(name);
246274
}
247275
},
248276
reset: function (options) {
249277
options = options || {};
250-
251-
var storages = _toStoragesArray(options.storages) || [this.defaultStorage];
252-
for (var i = 0; i < storages.length; i++) {
253-
if (!this.check(storages[i]))
278+
var storages = _toStoragesArray(options.storages) || [this.defaultStorage],
279+
namespace = options.namespace || this.options.namespace;
280+
for (var i = 0, storage; i < storages.length; i++) {
281+
storage = storages[i];
282+
if (!this.check(storage))
254283
continue;
255-
_storages[storages[i]].reset(this.options.namespace);
284+
_storages[storage].reset(namespace);
285+
}
286+
},
287+
keys: function (options) {
288+
options = options || {};
289+
var keys = [];
290+
for (var key in this.keysMap(options))
291+
keys.push(key);
292+
return keys;
293+
},
294+
keysMap: function (options) {
295+
options = options || {};
296+
var map = {},
297+
storages = _toStoragesArray(options.storages) || this.options.storages,
298+
namespace = options.namespace || this.options.namespace;
299+
for (var i = 0, storage, storageKeys; i < storages.length; i++) {
300+
storage = storages[i];
301+
if (!this.check(storage))
302+
continue;
303+
storageKeys = _storages[storage].keys(namespace);
304+
for (var j = 0, key; j < storageKeys.length; j++) {
305+
key = storageKeys[j];
306+
map[key] = map[key] instanceof Array ? map[key] : [];
307+
map[key].push(storage);
308+
}
256309
}
310+
return map;
257311
},
258312
// Access to native storages, without namespace or basil value decoration
259313
cookie: _storages.cookie,

build/basil.min.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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "basil.js",
3-
"version": "0.3.2",
3+
"version": "0.3.3",
44
"scripts": {
55
"test": "node node_modules/karma/bin/karma start test/karma.config.js",
66
"test-plugins": "node node_modules/karma/bin/karma start test/karma.plugins.config.js",

0 commit comments

Comments
 (0)