Comparing version 0.1.0 to 0.8.0
110
lockr.js
@@ -21,9 +21,42 @@ (function(root, factory) { | ||
Lockr.salt = ""; | ||
if (!Array.prototype.indexOf) { | ||
Array.prototype.indexOf = function(elt /*, from*/) | ||
{ | ||
var len = this.length >>> 0; | ||
Lockr.set = function (key, value) { | ||
var salted_key = this.salt + key; | ||
var from = Number(arguments[1]) || 0; | ||
from = (from < 0) | ||
? Math.ceil(from) | ||
: Math.floor(from); | ||
if (from < 0) | ||
from += len; | ||
for (; from < len; from++) | ||
{ | ||
if (from in this && | ||
this[from] === elt) | ||
return from; | ||
} | ||
return -1; | ||
}; | ||
} | ||
Lockr.prefix = ""; | ||
Lockr._getPrefixedKey = function(key, options) { | ||
options = options || {}; | ||
if (options.noPrefix) { | ||
return key; | ||
} else { | ||
return this.prefix + key; | ||
} | ||
}; | ||
Lockr.set = function (key, value, options) { | ||
var query_key = this._getPrefixedKey(key, options); | ||
try { | ||
localStorage.setItem(salted_key, JSON.stringify({"data": value})); | ||
localStorage.setItem(query_key, JSON.stringify({"data": value})); | ||
} catch (e) { | ||
@@ -34,8 +67,8 @@ if (console) console.warn("Lockr didn't successfully save the '{"+ key +": "+ value +"}' pair, because the localStorage is full."); | ||
Lockr.get = function (key, missing) { | ||
var salted_key = this.salt + key, | ||
Lockr.get = function (key, missing, options) { | ||
var query_key = this._getPrefixedKey(key, options), | ||
value; | ||
try { | ||
value = JSON.parse(localStorage.getItem(salted_key)); | ||
value = JSON.parse(localStorage.getItem(query_key)); | ||
} catch (e) { | ||
@@ -50,2 +83,44 @@ value = null; | ||
Lockr.sadd = function(key, value, options) { | ||
var query_key = this._getPrefixedKey(key, options), | ||
json; | ||
var values = Lockr.smembers(key); | ||
if (values.indexOf(value) > -1) { | ||
return null; | ||
} | ||
try { | ||
values.push(value); | ||
json = JSON.stringify({"data": values}); | ||
localStorage.setItem(query_key, json); | ||
} catch (e) { | ||
console.log(e); | ||
if (console) console.warn("Lockr didn't successfully add the "+ value +" to "+ key +" set, because the localStorage is full."); | ||
} | ||
}; | ||
Lockr.smembers = function(key, options) { | ||
var query_key = this._getPrefixedKey(key, options), | ||
value; | ||
try { | ||
value = JSON.parse(localStorage.getItem(query_key)); | ||
} catch (e) { | ||
value = null; | ||
} | ||
if (value === null) | ||
return []; | ||
else | ||
return (value.data || []); | ||
}; | ||
Lockr.sismember = function(key, value, options) { | ||
var query_key = this._getPrefixedKey(key, options); | ||
return Lockr.smembers(key).indexOf(value) > -1; | ||
}; | ||
Lockr.getAll = function () { | ||
@@ -59,2 +134,23 @@ var keys = Object.keys(localStorage); | ||
Lockr.srem = function(key, value, options) { | ||
var query_key = this._getPrefixedKey(key, options), | ||
json, | ||
index; | ||
var values = Lockr.smembers(key, value); | ||
index = values.indexOf(value); | ||
if (index > -1) | ||
values.splice(index, 1); | ||
json = JSON.stringify({"data": values}); | ||
try { | ||
localStorage.setItem(query_key, json); | ||
} catch (e) { | ||
if (console) console.warn("Lockr couldn't remove the "+ value +" from the set "+ key); | ||
} | ||
}; | ||
Lockr.rm = function (key) { | ||
@@ -61,0 +157,0 @@ localStorage.removeItem(key); |
@@ -1,1 +0,1 @@ | ||
!function(root,factory){"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=_),exports._=_):"function"==typeof define&&define.amd?define(["exports"],function(exports){root.Lockr=factory(root,exports)}):root.Lockr=factory(root,{})}(this,function(root,Lockr){"use strict";return root.Lockr=Lockr,Lockr.salt="",Lockr.set=function(key,value){var salted_key=this.salt+key;try{localStorage.setItem(salted_key,JSON.stringify({data:value}))}catch(e){console&&console.warn("Lockr didn't successfully save the '{"+key+": "+value+"}' pair, because the localStorage is full.")}},Lockr.get=function(key,missing){var value,salted_key=this.salt+key;try{value=JSON.parse(localStorage.getItem(salted_key))}catch(e){value=null}return null===value?missing:value.data||missing},Lockr.getAll=function(){var keys=Object.keys(localStorage);return keys.map(function(key){return Lockr.get(key)})},Lockr.rm=function(key){localStorage.removeItem(key)},Lockr.flush=function(){localStorage.clear()},Lockr}); | ||
!function(root,factory){"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=_),exports._=_):"function"==typeof define&&define.amd?define(["exports"],function(exports){root.Lockr=factory(root,exports)}):root.Lockr=factory(root,{})}(this,function(root,Lockr){"use strict";return root.Lockr=Lockr,Array.prototype.indexOf||(Array.prototype.indexOf=function(elt){var len=this.length>>>0,from=Number(arguments[1])||0;for(from=0>from?Math.ceil(from):Math.floor(from),0>from&&(from+=len);len>from;from++)if(from in this&&this[from]===elt)return from;return-1}),Lockr.prefix="",Lockr._getPrefixedKey=function(key,options){return options=options||{},options.noPrefix?key:this.prefix+key},Lockr.set=function(key,value,options){var query_key=this._getPrefixedKey(key,options);try{localStorage.setItem(query_key,JSON.stringify({data:value}))}catch(e){console&&console.warn("Lockr didn't successfully save the '{"+key+": "+value+"}' pair, because the localStorage is full.")}},Lockr.get=function(key,missing,options){var value,query_key=this._getPrefixedKey(key,options);try{value=JSON.parse(localStorage.getItem(query_key))}catch(e){value=null}return null===value?missing:value.data||missing},Lockr.sadd=function(key,value,options){var json,query_key=this._getPrefixedKey(key,options),values=Lockr.smembers(key);if(values.indexOf(value)>-1)return null;try{values.push(value),json=JSON.stringify({data:values}),localStorage.setItem(query_key,json)}catch(e){console.log(e),console&&console.warn("Lockr didn't successfully add the "+value+" to "+key+" set, because the localStorage is full.")}},Lockr.smembers=function(key,options){var value,query_key=this._getPrefixedKey(key,options);try{value=JSON.parse(localStorage.getItem(query_key))}catch(e){value=null}return null===value?[]:value.data||[]},Lockr.sismember=function(key,value,options){this._getPrefixedKey(key,options);return Lockr.smembers(key).indexOf(value)>-1},Lockr.getAll=function(){var keys=Object.keys(localStorage);return keys.map(function(key){return Lockr.get(key)})},Lockr.srem=function(key,value,options){var json,index,query_key=this._getPrefixedKey(key,options),values=Lockr.smembers(key,value);index=values.indexOf(value),index>-1&&values.splice(index,1),json=JSON.stringify({data:values});try{localStorage.setItem(query_key,json)}catch(e){console&&console.warn("Lockr couldn't remove the "+value+" from the set "+key)}},Lockr.rm=function(key){localStorage.removeItem(key)},Lockr.flush=function(){localStorage.clear()},Lockr}); |
{ | ||
"name": "lockr", | ||
"version": "0.1.0", | ||
"version": "0.8.0", | ||
"author": { | ||
@@ -11,2 +11,3 @@ "name": "Dimitris Tsironis", | ||
}, | ||
"main": "lockr.js", | ||
"devDependencies": { | ||
@@ -22,2 +23,1 @@ "grunt-contrib-jshint": "~0.1.1", | ||
} | ||
![Lockr logo](http://i.imgur.com/m5kPjkB.png) | ||
[![Code | ||
Climate](https://codeclimate.com/github/tsironis/lockr/badges/gpa.svg)](https://codeclimate.com/github/tsironis/lockr) | ||
> A minimal API wrapper for localStorage. Simple as your high-school locker. | ||
Lockr (it's pronounced /ˈlɒkəʳ/) is a extremely lightweight library (<1kb when minified), designed to facilitate how you interact with localStorage. Saving objects and arrays, numbers or other data types, accessible via a Redis-like API, heavily inspired by [node_redis](https://github.com/mranney/node_redis/). | ||
Lockr (pronounced /ˈlɒkəʳ/) is an extremely lightweight library (<2kb when minified), designed to facilitate how you interact with localStorage. Saving objects and arrays, numbers or other data types, accessible via a Redis-like API, heavily inspired by [node_redis](https://github.com/mranney/node_redis/). | ||
@@ -37,15 +40,2 @@ ## How to use lockr | ||
```Lockr.salt``` - arguments: *empty string* | ||
> Salts each key that's getting saved with a differentiator of your own taste. | ||
*Example* | ||
```js | ||
Lockr.salt = "userid123"; | ||
Lockr.set('account_type', 'paid'); // actually saves ```{ userid123account_type : '{"data":"paid"}' }``` | ||
Lockr.get('account_type'); | ||
> "paid" | ||
``` | ||
```Lockr.get``` - arguments: *[ key or hash_key, default value ]* | ||
@@ -76,2 +66,59 @@ | ||
```Lockr.sadd``` - arguments *[ key, value ]*{String, Number, Array or Object} | ||
> Adds a unique value to a particular set under a hash key. | ||
*Example* | ||
```js | ||
Lockr.sadd("wat", 1); // [1] | ||
Lockr.sadd("wat", 2); // [1, 2] | ||
Lockr.sadd("wat", 1); // [1, 2] | ||
``` | ||
--- | ||
```Lockr.smembers``` - arguments *[ key ]* | ||
> Returns the values of a particular set under a hash key. | ||
*Example* | ||
```js | ||
Lockr.sadd("wat", 42); | ||
Lockr.sadd("wat", 1337); | ||
Lockr.smembers("wat"); // [42, 1337] | ||
``` | ||
--- | ||
```Lockr.sismember``` - arguments *[ key, value ]* | ||
> Returns whether the value exists in a particular set under a hash key. | ||
*Example* | ||
```js | ||
Lockr.sadd("wat", 1); | ||
Lockr.sismember("wat", 1); // true | ||
Lockr.sismember("wat", 2); // false | ||
``` | ||
--- | ||
```Lockr.srem``` - arguments *[ key, value ]* | ||
> Removes a value from a particular set under a hash key. | ||
*Example* | ||
```js | ||
Lockr.sadd("wat", 1); | ||
Lockr.sadd("wat", 2); | ||
Lockr.srem("wat", 1); | ||
Lockr.smembers("wat"); // [2] | ||
``` | ||
--- | ||
```Lockr.getAll``` - arguments: *null* | ||
@@ -78,0 +125,0 @@ |
@@ -1,11 +0,15 @@ | ||
localStorage.clear(); | ||
describe('Lockr::Saving data', function () { | ||
it('should save a key-value pair in the localStorage', function () { | ||
afterEach(function() { | ||
localStorage.clear(); | ||
}); | ||
describe('Lockr.set', function () { | ||
it('saves a key-value pair in the localStorage', function () { | ||
Lockr.set('test', 123); | ||
expect(localStorage.getItem('test')).toEqual('{"data":123}'); | ||
Lockr.set('test_floating', 123.123); | ||
expect(localStorage.getItem('test_floating')).toEqual('{"data":123.123}'); | ||
}); | ||
it('should save a hash object in the localStorage', function () { | ||
Lockr.set('my_hash', {"test": 123, "hey": "whatsup"}); | ||
expect(localStorage.getItem('my_hash')).toContain('data'); | ||
@@ -18,50 +22,80 @@ expect(localStorage.getItem('my_hash')).toContain('test'); | ||
}); | ||
describe('Lockr::Retrieving data', function () { | ||
it('should get a hash object from the localStorage', function () { | ||
var integer = Lockr.get('test'); | ||
var floating = Lockr.get('test_floating'); | ||
var number = Lockr.get('test'); | ||
var hash = Lockr.get('my_hash'); | ||
var empty = Lockr.get('something_that_doesnt_exist'); | ||
expect(hash.hey).toBe('whatsup'); | ||
expect(hash.test).toEqual(123); | ||
expect(integer).toEqual(123); | ||
expect(floating).toEqual(123.123); | ||
expect(empty).not.toBeNull(); | ||
expect(empty).toBeUndefined(); | ||
describe('Lockr.get', function () { | ||
beforeEach(function() { | ||
Lockr.set('test', 123); | ||
Lockr.sadd('array', 2); | ||
Lockr.sadd('array', 3); | ||
Lockr.set('hash', {"test": 123, "hey": "whatsup"}); | ||
}); | ||
it('should get all contents of the localStorage', function () { | ||
it('returns the value for the given key from the localStorage', function () { | ||
var value = Lockr.get('test'); | ||
expect(value).toEqual(123); | ||
}); | ||
it('returns undefined for a non-existent key', function() { | ||
var value = Lockr.get('something'); | ||
expect(value).not.toBeNull(); | ||
expect(value).toBeUndefined(); | ||
}); | ||
it('gets all contents of the localStorage', function () { | ||
var contents = Lockr.getAll(); | ||
expect(contents.length).toBe(3); | ||
expect(contents).toContain(123.123); | ||
expect(contents).toContain({"test": 123, "hey": "whatsup"}); | ||
expect(contents).toContain(123); | ||
expect(contents).toContain([2, 3]); | ||
}); | ||
it('should return strings containing "{"" as strings', function () { | ||
var theString = 'This is a string containing the characters "{", "}", "[", "]", ":" which are used in objects'; | ||
describe('with wrong data', function() { | ||
beforeEach(function() { | ||
localStorage.setItem('wrong', ',fluffy,truffly,commas,hell'); | ||
}); | ||
Lockr.set('aString', theString); | ||
expect(Lockr.get('aString')).toEqual(theString); | ||
}); | ||
it('cleans wrong data', function () { | ||
var wrongData = Lockr.get("wrong"); | ||
it('should clean wrong data', function () { | ||
localStorage.setItem('wrong', ',fluffy,truffly,commas,hell'); | ||
expect(Lockr.get('wrong')).toBeUndefined(); | ||
expect(wrongData).toBeUndefined(); | ||
}); | ||
}); | ||
}); | ||
describe('Lockr::Deleting an element', function () { | ||
it('should remove succesfully a key-value pair', function() { | ||
Lockr.rm('test_floating'); | ||
expect(Lockr.get('test_floating')).toBeUndefined(); | ||
describe('Lockr.rm', function () { | ||
beforeEach(function() { | ||
Lockr.set('test', 123); | ||
Lockr.sadd('array', 2); | ||
Lockr.sadd('array', 3); | ||
Lockr.set('hash', {"test": 123, "hey": "whatsup"}); | ||
}); | ||
it('removes succesfully a key-value pair', function() { | ||
var oldContents = Lockr.getAll(); | ||
expect(oldContents.length).toBe(3); | ||
Lockr.rm('test'); | ||
expect(Lockr.get('test')).toBeUndefined(); | ||
var contents = Lockr.getAll(); | ||
expect(contents.length).toBe(4); | ||
expect(contents.length).toBe(2); | ||
}); | ||
}); | ||
describe('Lockr::Flushing data', function () { | ||
it('should clear all contents of the localStorage', function () { | ||
describe('Lockr.flush', function () { | ||
beforeEach(function() { | ||
Lockr.set('test', 123); | ||
Lockr.sadd('array', 2); | ||
Lockr.sadd('array', 3); | ||
Lockr.set('hash', {"test": 123, "hey": "whatsup"}); | ||
}); | ||
it('clears all contents of the localStorage', function () { | ||
var oldContents = Lockr.getAll(); | ||
expect(oldContents.length).not.toBe(0); | ||
Lockr.flush(); | ||
var contents = Lockr.getAll(); | ||
@@ -71,11 +105,62 @@ expect(contents.length).toBe(0); | ||
}); | ||
describe('Lockr::Salted', function() { | ||
it('should set a salt key', function() { | ||
Lockr.salt = "imasalt"; | ||
expect(Lockr.salt).toEqual("imasalt"); | ||
describe('Sets', function() { | ||
beforeEach(function() { | ||
Lockr.sadd('test_set', 1); | ||
Lockr.sadd('test_set', 2); | ||
}); | ||
it('should save a key-value pair, salted', function() { | ||
Lockr.set("andpeper", true); | ||
expect("imasaltandpeper" in localStorage).toEqual(true); | ||
describe('Lockr.sadd', function () { | ||
it('saves a set under the given key in the localStorage', function () { | ||
Lockr.sadd('a_set', 1); | ||
Lockr.sadd('a_set', 2); | ||
expect(localStorage.getItem('a_set')).toEqual('{"data":[1,2]}'); | ||
}); | ||
it('does not add the same value again', function() { | ||
Lockr.sadd('test_set', 1); | ||
Lockr.sadd('test_set', 2); | ||
Lockr.sadd('test_set, 1'); | ||
expect(Lockr.smembers('test_set')).toEqual([1, 2]); | ||
}); | ||
}); | ||
describe('Lockr.smembers', function() { | ||
it('returns all the values for given key', function() { | ||
expect(Lockr.smembers('test_set')).toEqual([1, 2]); | ||
}); | ||
}); | ||
describe('Lock.sismember', function() { | ||
it('returns true if the value exists in the given set(key)', function () { | ||
expect(Lockr.sismember('test_set', 1)).toEqual(true); | ||
expect(Lockr.sismember('test_set', 34)).toEqual(false); | ||
}); | ||
}); | ||
describe('Lock.srem', function() { | ||
it('removes value from collection if exists', function() { | ||
Lockr.srem('test_set', 1); | ||
expect(Lockr.sismember('test_set', 1)).toEqual(false); | ||
}); | ||
}); | ||
}); | ||
describe('Lockr::Prefixed', function() { | ||
it('should set a prefix', function() { | ||
Lockr.prefix = "imaprefix"; | ||
expect(Lockr.prefix).toEqual("imaprefix"); | ||
}); | ||
it('should return a correctly prefixed key', function() { | ||
expect(Lockr._getPrefixedKey('lalala')).toEqual('imaprefixlalala'); | ||
}); | ||
it('should return a non-prefixed key', function() { | ||
expect(Lockr._getPrefixedKey('lalala', {noPrefix: true})).toEqual("lalala"); | ||
}); | ||
it('should save a key-value pair, prefixed', function() { | ||
Lockr.set("justlikeyou", true); | ||
expect("imaprefixjustlikeyou" in localStorage).toEqual(true); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17726
359
147