Skip to content

Commit 5357a93

Browse files
author
Mathieu Ghaleb
committed
- tag 0.3.4
- namespace is now optional calling directly native storages `.keys()` and `.reset()` functions
1 parent 1af5fac commit 5357a93

File tree

6 files changed

+37
-31
lines changed

6 files changed

+37
-31
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ basil = new window.Basil(options);
7373
basil.set('hello', 'world');
7474

7575
// store data under a given namespace
76-
basil.set('hello', 42, { 'namespace': 'alt' });
77-
basil.set('abc', 'def', { 'namespace': 'alt', 'storages': ['memory'] });
76+
basil.set('hello', 42, { 'namespace': 'alt' });
77+
basil.set('abc', 'def', { 'namespace': 'alt', 'storages': ['memory'] });
7878

7979
// retrieve data
8080
basil.get('hello'); // return 'world'
@@ -89,12 +89,12 @@ basil.keysMap(); // returns { 'hello': ['local'] }
8989
basil.keysMap({ 'namespace': 'alt' }); // returns { 'hello': ['local'], 'abc': ['memory'] }
9090

9191
// remove data under a given namespace
92-
basil.remove('hello', { 'namespace': 'alt' });
92+
basil.remove('hello', { 'namespace': 'alt' });
9393
basil.get('hello'); // return 'world'
9494
basil.get('hello', { 'namespace': 'alt' }); // return null
9595

9696
// reset data under a given namespace
97-
basil.reset({ 'namespace': 'alt', 'storages': ['local', 'memory']});
97+
basil.reset({ 'namespace': 'alt', 'storages': ['local', 'memory']});
9898
```
9999

100100
## Configuration

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.3",
3+
"version": "0.3.4",
44
"homepage": "https://github.com/Wisembly/basil.js",
55
"authors": [
66
"Mathieu Ghaleb <mathieu@wisembly.com>"

build/basil.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@
303303
storageKeys = _storages[storage].keys(namespace);
304304
for (var j = 0, key; j < storageKeys.length; j++) {
305305
key = storageKeys[j];
306-
map[key] = map[key] instanceof Array ? map[key] : [];
306+
map[key] = map[key] || [];
307307
map[key].push(storage);
308308
}
309309
}

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.3",
3+
"version": "0.3.4",
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",

src/basil.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
};
66

77
// Version
8-
Basil.version = '0.3.3';
8+
Basil.version = '0.3.4';
99

1010
// Utils
1111
Basil.utils = {
@@ -18,6 +18,9 @@
1818
}
1919
return destination;
2020
},
21+
isArray: function (obj) {
22+
return Object.prototype.toString.call(obj) === '[object Array]';
23+
},
2124
registerPlugin: function (methods) {
2225
Basil.plugins = this.extend(methods, Basil.plugins);
2326
}
@@ -42,13 +45,13 @@
4245
_toStoragesArray = function (storages) {
4346
if (!storages)
4447
return null;
45-
return Object.prototype.toString.call(storages) === '[object Array]' ? storages : [storages];
48+
return Basil.utils.isArray(storages) ? storages : [storages];
4649
},
4750
_toStoredKey = function (namespace, name) {
4851
var key = '';
4952
if (typeof name === 'string')
5053
key = namespace + ':' + name;
51-
else if (name instanceof Array) {
54+
else if (Basil.utils.isArray(name)) {
5255
key = namespace;
5356
for (var i = 0; i < name.length; i++)
5457
if (name[i])
@@ -57,7 +60,9 @@
5760
return key;
5861
},
5962
_toKeyName = function (namespace, name) {
60-
return name.replace(namespace + ':', '');
63+
if (!namespace)
64+
return name;
65+
return name.replace(new RegExp('^' + namespace + ':'), '');
6166
},
6267
_toStoredValue = function (value) {
6368
return JSON.stringify(value);
@@ -92,7 +97,7 @@
9297
reset: function (namespace) {
9398
for (var i = 0, key; i < this.engine.length; i++) {
9499
key = this.engine.key(i);
95-
if (key.indexOf(namespace) === 0) {
100+
if (!namespace || key.indexOf(namespace) === 0) {
96101
this.remove(key);
97102
i--;
98103
}
@@ -102,7 +107,7 @@
102107
var keys = [];
103108
for (var i = 0, key; i < this.engine.length; i++) {
104109
key = this.engine.key(i);
105-
if (key.indexOf(namespace) === 0)
110+
if (!namespace || key.indexOf(namespace) === 0)
106111
keys.push(_toKeyName(namespace, key));
107112
}
108113
return keys;
@@ -133,14 +138,14 @@
133138
},
134139
reset: function (namespace) {
135140
for (var key in this._hash) {
136-
if (key.indexOf(namespace) === 0)
141+
if (!namespace || key.indexOf(namespace) === 0)
137142
this.remove(key);
138143
}
139144
},
140145
keys: function (namespace) {
141146
var keys = [];
142147
for (var key in this._hash)
143-
if (key.indexOf(namespace) === 0)
148+
if (!namespace || key.indexOf(namespace) === 0)
144149
keys.push(_toKeyName(namespace, key));
145150
return keys;
146151
}
@@ -167,8 +172,8 @@
167172
},
168173
get: function (name) {
169174
var cookies = document.cookie.split(';');
170-
for (var i = 0; i < cookies.length; i++) {
171-
var cookie = cookies[i].replace(/^\s*/, '');
175+
for (var i = 0, cookie; i < cookies.length; i++) {
176+
cookie = cookies[i].replace(/^\s*/, '');
172177
if (cookie.indexOf(name + '=') === 0)
173178
return cookie.substring(name.length + 1, cookie.length);
174179
}
@@ -187,20 +192,20 @@
187192
},
188193
reset: function (namespace) {
189194
var cookies = document.cookie.split(';');
190-
for (var i = 0; i < cookies.length; i++) {
191-
var cookie = cookies[i].replace(/^\s*/, ''),
192-
key = cookie.substr(0, cookie.indexOf('='));
193-
if (key.indexOf(namespace) === 0)
195+
for (var i = 0, cookie, key; i < cookies.length; i++) {
196+
cookie = cookies[i].replace(/^\s*/, '');
197+
key = cookie.substr(0, cookie.indexOf('='));
198+
if (!namespace || key.indexOf(namespace) === 0)
194199
this.remove(key);
195200
}
196201
},
197202
keys: function (namespace) {
198203
var keys = [],
199204
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)
205+
for (var i = 0, cookie, key; i < cookies.length; i++) {
206+
cookie = cookies[i].replace(/^\s*/, '');
207+
key = cookie.substr(0, cookie.indexOf('='));
208+
if (!namespace || key.indexOf(namespace) === 0)
204209
keys.push(_toKeyName(namespace, key));
205210
}
206211
return keys;
@@ -310,6 +315,7 @@
310315
return map;
311316
},
312317
// Access to native storages, without namespace or basil value decoration
318+
memory: _storages.memory,
313319
cookie: _storages.cookie,
314320
localStorage: _storages.local,
315321
sessionStorage: _storages.session

test/test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@
4040
var data = {
4141
str: 'hello world',
4242
nb: 42,
43-
obj: { foo: 'bar', baz: 'quux' },
43+
obj: { foo: 'bar', baz: 'quux' },
4444
arr: ['foo', 42, 'bar']
4545
},
4646
alt = {
4747
str: 'foobar',
4848
nb: -1,
49-
obj: { hello: 'world', foo: 'bar' },
49+
obj: { hello: 'world', foo: 'bar' },
5050
arr: ['quux', -1, 'baz']
5151
};
5252

@@ -61,7 +61,7 @@
6161
var basil = new window.Basil();
6262
for (var key in alt) {
6363
basil.set(key, alt[key], { namespace: 'alt' });
64-
expect(basil.get(key, { namespace: 'alt' })).to.eql(alt[key]);
64+
expect(basil.get(key, { namespace: 'alt' })).to.eql(alt[key]);
6565
expect(basil.get(key)).to.eql(data[key]);
6666
}
6767
});
@@ -135,9 +135,9 @@
135135
});
136136
expect(basil.keys({ namespace: 'third' })).to.eql([]);
137137
expect(basil.keysMap({ namespace: 'third' })).to.eql({});
138-
basil.reset({ namespace: 'first' });
139-
basil.reset({ namespace: 'second' });
140-
basil.reset({ namespace: 'third' });
138+
basil.reset({ namespace: 'first' });
139+
basil.reset({ namespace: 'second' });
140+
basil.reset({ namespace: 'third' });
141141
});
142142
});
143143

0 commit comments

Comments
 (0)