Skip to content

Commit

Permalink
- tag 0.3.4
Browse files Browse the repository at this point in the history
- namespace is now optional calling directly native storages `.keys()` and `.reset()` functions
  • Loading branch information
Mathieu Ghaleb committed Oct 16, 2014
1 parent 1af5fac commit 5357a93
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 31 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ basil = new window.Basil(options);
basil.set('hello', 'world');

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

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

// remove data under a given namespace
basil.remove('hello', { 'namespace': 'alt' });
basil.remove('hello', { 'namespace': 'alt' });
basil.get('hello'); // return 'world'
basil.get('hello', { 'namespace': 'alt' }); // return null

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

## Configuration
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "basil.js",
"version": "0.3.3",
"version": "0.3.4",
"homepage": "https://github.com/Wisembly/basil.js",
"authors": [
"Mathieu Ghaleb <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion build/basil.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@
storageKeys = _storages[storage].keys(namespace);
for (var j = 0, key; j < storageKeys.length; j++) {
key = storageKeys[j];
map[key] = map[key] instanceof Array ? map[key] : [];
map[key] = map[key] || [];
map[key].push(storage);
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "basil.js",
"version": "0.3.3",
"version": "0.3.4",
"scripts": {
"test": "node node_modules/karma/bin/karma start test/karma.config.js",
"test-plugins": "node node_modules/karma/bin/karma start test/karma.plugins.config.js",
Expand Down
42 changes: 24 additions & 18 deletions src/basil.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
};

// Version
Basil.version = '0.3.3';
Basil.version = '0.3.4';

// Utils
Basil.utils = {
Expand All @@ -18,6 +18,9 @@
}
return destination;
},
isArray: function (obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
},
registerPlugin: function (methods) {
Basil.plugins = this.extend(methods, Basil.plugins);
}
Expand All @@ -42,13 +45,13 @@
_toStoragesArray = function (storages) {
if (!storages)
return null;
return Object.prototype.toString.call(storages) === '[object Array]' ? storages : [storages];
return Basil.utils.isArray(storages) ? storages : [storages];
},
_toStoredKey = function (namespace, name) {
var key = '';
if (typeof name === 'string')
key = namespace + ':' + name;
else if (name instanceof Array) {
else if (Basil.utils.isArray(name)) {
key = namespace;
for (var i = 0; i < name.length; i++)
if (name[i])
Expand All @@ -57,7 +60,9 @@
return key;
},
_toKeyName = function (namespace, name) {
return name.replace(namespace + ':', '');
if (!namespace)
return name;
return name.replace(new RegExp('^' + namespace + ':'), '');
},
_toStoredValue = function (value) {
return JSON.stringify(value);
Expand Down Expand Up @@ -92,7 +97,7 @@
reset: function (namespace) {
for (var i = 0, key; i < this.engine.length; i++) {
key = this.engine.key(i);
if (key.indexOf(namespace) === 0) {
if (!namespace || key.indexOf(namespace) === 0) {
this.remove(key);
i--;
}
Expand All @@ -102,7 +107,7 @@
var keys = [];
for (var i = 0, key; i < this.engine.length; i++) {
key = this.engine.key(i);
if (key.indexOf(namespace) === 0)
if (!namespace || key.indexOf(namespace) === 0)
keys.push(_toKeyName(namespace, key));
}
return keys;
Expand Down Expand Up @@ -133,14 +138,14 @@
},
reset: function (namespace) {
for (var key in this._hash) {
if (key.indexOf(namespace) === 0)
if (!namespace || key.indexOf(namespace) === 0)
this.remove(key);
}
},
keys: function (namespace) {
var keys = [];
for (var key in this._hash)
if (key.indexOf(namespace) === 0)
if (!namespace || key.indexOf(namespace) === 0)
keys.push(_toKeyName(namespace, key));
return keys;
}
Expand All @@ -167,8 +172,8 @@
},
get: function (name) {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].replace(/^\s*/, '');
for (var i = 0, cookie; i < cookies.length; i++) {
cookie = cookies[i].replace(/^\s*/, '');
if (cookie.indexOf(name + '=') === 0)
return cookie.substring(name.length + 1, cookie.length);
}
Expand All @@ -187,20 +192,20 @@
},
reset: function (namespace) {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].replace(/^\s*/, ''),
key = cookie.substr(0, cookie.indexOf('='));
if (key.indexOf(namespace) === 0)
for (var i = 0, cookie, key; i < cookies.length; i++) {
cookie = cookies[i].replace(/^\s*/, '');
key = cookie.substr(0, cookie.indexOf('='));
if (!namespace || key.indexOf(namespace) === 0)
this.remove(key);
}
},
keys: function (namespace) {
var keys = [],
cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].replace(/^\s*/, ''),
key = cookie.substr(0, cookie.indexOf('='));
if (key.indexOf(namespace) === 0)
for (var i = 0, cookie, key; i < cookies.length; i++) {
cookie = cookies[i].replace(/^\s*/, '');
key = cookie.substr(0, cookie.indexOf('='));
if (!namespace || key.indexOf(namespace) === 0)
keys.push(_toKeyName(namespace, key));
}
return keys;
Expand Down Expand Up @@ -310,6 +315,7 @@
return map;
},
// Access to native storages, without namespace or basil value decoration
memory: _storages.memory,
cookie: _storages.cookie,
localStorage: _storages.local,
sessionStorage: _storages.session
Expand Down
12 changes: 6 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@
var data = {
str: 'hello world',
nb: 42,
obj: { foo: 'bar', baz: 'quux' },
obj: { foo: 'bar', baz: 'quux' },
arr: ['foo', 42, 'bar']
},
alt = {
str: 'foobar',
nb: -1,
obj: { hello: 'world', foo: 'bar' },
obj: { hello: 'world', foo: 'bar' },
arr: ['quux', -1, 'baz']
};

Expand All @@ -61,7 +61,7 @@
var basil = new window.Basil();
for (var key in alt) {
basil.set(key, alt[key], { namespace: 'alt' });
expect(basil.get(key, { namespace: 'alt' })).to.eql(alt[key]);
expect(basil.get(key, { namespace: 'alt' })).to.eql(alt[key]);
expect(basil.get(key)).to.eql(data[key]);
}
});
Expand Down Expand Up @@ -135,9 +135,9 @@
});
expect(basil.keys({ namespace: 'third' })).to.eql([]);
expect(basil.keysMap({ namespace: 'third' })).to.eql({});
basil.reset({ namespace: 'first' });
basil.reset({ namespace: 'second' });
basil.reset({ namespace: 'third' });
basil.reset({ namespace: 'first' });
basil.reset({ namespace: 'second' });
basil.reset({ namespace: 'third' });
});
});

Expand Down

0 comments on commit 5357a93

Please sign in to comment.