From 3c354cc9f5bf8f1b4eba48d408f346df48d7c1b7 Mon Sep 17 00:00:00 2001 From: Aggelos Avgerinos Date: Wed, 30 Jul 2014 09:26:42 +0300 Subject: [PATCH 01/12] Add sadd --- lockr.js | 23 +++++++++++++++++++++++ specs/lockrSpecs.js | 9 +++++++++ 2 files changed, 32 insertions(+) diff --git a/lockr.js b/lockr.js index 8d9e358..eccf277 100644 --- a/lockr.js +++ b/lockr.js @@ -40,6 +40,29 @@ return (value.data || missing); }; + Lockr.sadd = function(key, value) { + var salted_key = this.salt + key, + val, json; + + if (values.indexOf(value) > -1) { + return null; + } + + try { + val = JSON.parse(localStorage.getItem(salted_key)); + } catch (e) { + val = []; + } + + try { + val.push(value); + json = JSON.stringify({"data": val}); + localStorage.setItem(salted_key, json); + } catch (e) { + if (console) console.warn("Lockr didn't successfully add the "+ value +" to "+ key +" set, because the localStorage is full."); + } + }; + Lockr.getAll = function () { var keys = Object.keys(localStorage); diff --git a/specs/lockrSpecs.js b/specs/lockrSpecs.js index e55bc8d..f01e8bf 100644 --- a/specs/lockrSpecs.js +++ b/specs/lockrSpecs.js @@ -15,6 +15,15 @@ describe('Lockr::Saving data', function () { expect(localStorage.getItem('my_hash')).toContain('whatsup'); }); }); + +describe('Lockr.sadd', function () { + it('saves a set under the given key in the localStorage', function () { + Lockr.sadd('test', 1) + Lockr.sadd('test', 2) + expect(localStorage.getItem('test')).toEqual('{"data":[1,2]}'); + }); +}); + describe('Lockr::Retrieving data', function () { it('should get a hash object from the localStorage', function () { var integer = Lockr.get('test'); From 08edebb3f380bbd6ab0f0323d07d26f046bd6122 Mon Sep 17 00:00:00 2001 From: Aggelos Avgerinos Date: Wed, 30 Jul 2014 19:39:38 +0300 Subject: [PATCH 02/12] Add smembers --- lockr.js | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lockr.js b/lockr.js index eccf277..bb83aee 100644 --- a/lockr.js +++ b/lockr.js @@ -41,26 +41,37 @@ }; Lockr.sadd = function(key, value) { - var salted_key = this.salt + key, - val, json; + var salted_key = this.salt + key, json; + + var values = Lockr.smembers(key); if (values.indexOf(value) > -1) { return null; } try { - val = JSON.parse(localStorage.getItem(salted_key)); + values.push(value); + json = JSON.stringify({"data": values}); + localStorage.setItem(salted_key, json); } catch (e) { - val = []; + 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) { + var salted_key = this.salt + key, value; try { - val.push(value); - json = JSON.stringify({"data": val}); - localStorage.setItem(salted_key, json); + value = JSON.parse(localStorage.getItem(salted_key)); } catch (e) { - if (console) console.warn("Lockr didn't successfully add the "+ value +" to "+ key +" set, because the localStorage is full."); + value = null; } + + if (value === null) + return []; + else + return (value.data || []); }; Lockr.getAll = function () { From 8e4983a4cebf828c21566419a358c30b348c868c Mon Sep 17 00:00:00 2001 From: Aggelos Avgerinos Date: Wed, 30 Jul 2014 19:39:54 +0300 Subject: [PATCH 03/12] Add sismember --- lockr.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lockr.js b/lockr.js index bb83aee..b8957aa 100644 --- a/lockr.js +++ b/lockr.js @@ -74,6 +74,10 @@ return (value.data || []); }; + Lockr.sismember = function(key, value) { + return Lockr.smembers(key).indexOf(value) > -1; + }; + Lockr.getAll = function () { var keys = Object.keys(localStorage); From 714407df58355c81429af643b6e245666e5d4f9e Mon Sep 17 00:00:00 2001 From: Aggelos Avgerinos Date: Wed, 30 Jul 2014 20:25:58 +0300 Subject: [PATCH 04/12] Add some specs for SETS functionality --- specs/lockrSpecs.js | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/specs/lockrSpecs.js b/specs/lockrSpecs.js index f01e8bf..9b4eedb 100644 --- a/specs/lockrSpecs.js +++ b/specs/lockrSpecs.js @@ -16,14 +16,6 @@ describe('Lockr::Saving data', function () { }); }); -describe('Lockr.sadd', function () { - it('saves a set under the given key in the localStorage', function () { - Lockr.sadd('test', 1) - Lockr.sadd('test', 2) - expect(localStorage.getItem('test')).toEqual('{"data":[1,2]}'); - }); -}); - describe('Lockr::Retrieving data', function () { it('should get a hash object from the localStorage', function () { var integer = Lockr.get('test'); @@ -76,6 +68,33 @@ describe('Lockr::Flushing data', function () { expect(contents.length).toBe(0); }); }); + +describe('Lockr.sadd', function () { + it('saves a set under the given key in the localStorage', function () { + Lockr.sadd('test_set', 1) + Lockr.sadd('test_set', 2) + expect(localStorage.getItem('test_set')).toEqual('{"data":[1,2]}'); + }); + + it('does not add the same value again', function() { + 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('Lockr::Salted', function() { it('should set a salt key', function() { Lockr.salt = "imasalt"; From 2bcbcc49292817ff3c676fc7810b566b908a80d5 Mon Sep 17 00:00:00 2001 From: Aggelos Avgerinos Date: Wed, 30 Jul 2014 20:35:15 +0300 Subject: [PATCH 05/12] Wipe console.log(e) from the face of earth --- lockr.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lockr.js b/lockr.js index b8957aa..9f3947b 100644 --- a/lockr.js +++ b/lockr.js @@ -54,7 +54,6 @@ json = JSON.stringify({"data": values}); localStorage.setItem(salted_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."); } }; From 71b27cd8337cd93353e2a9d640006ceb10e31171 Mon Sep 17 00:00:00 2001 From: Aggelos Avgerinos Date: Sat, 2 Aug 2014 23:54:45 +0300 Subject: [PATCH 06/12] Add srem --- lockr.js | 18 ++++++++++++++++++ specs/lockrSpecs.js | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/lockr.js b/lockr.js index 9f3947b..ea21fa7 100644 --- a/lockr.js +++ b/lockr.js @@ -85,6 +85,24 @@ }); }; + Lockr.srem = function(key, value) { + var salted_key = this.salt + key, 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(salted_key, json); + } catch (e) { + if (console) console.warn("Lockr couldn't remove the "+ value +" from the set "+ key); + } + }; + Lockr.rm = function (key) { localStorage.removeItem(key); }; diff --git a/specs/lockrSpecs.js b/specs/lockrSpecs.js index 9b4eedb..5293c7e 100644 --- a/specs/lockrSpecs.js +++ b/specs/lockrSpecs.js @@ -95,6 +95,13 @@ describe('Lock.sismember', function() { }); }); +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::Salted', function() { it('should set a salt key', function() { Lockr.salt = "imasalt"; From 23a17e0260091b9a12ef21ad4a627d0656e27c11 Mon Sep 17 00:00:00 2001 From: Aggelos Avgerinos Date: Sun, 3 Aug 2014 00:00:04 +0300 Subject: [PATCH 07/12] Change minified size in README to tell the truth --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2a2e591..bf32894 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ > 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 (it's pronounced /ˈlɒkəʳ/) is a 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/). ## How to use lockr From 0482795ad0c3909ab94da5099c3151a4fce4b058 Mon Sep 17 00:00:00 2001 From: Aggelos Avgerinos Date: Sun, 3 Aug 2014 02:58:44 +0300 Subject: [PATCH 08/12] Update README to cover sets functionality --- README.md | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/README.md b/README.md index bf32894..a0946d5 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,63 @@ Lockr.get('score', 0): --- +```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* > Returns all saved values & objects, in an ```Array``` From d7ec332c3ccdfc72ef5b4380ed50069932923a2b Mon Sep 17 00:00:00 2001 From: Aggelos Avgerinos Date: Sun, 3 Aug 2014 02:59:53 +0300 Subject: [PATCH 09/12] Fix grammar in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a0946d5..5be80ef 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ > A minimal API wrapper for localStorage. Simple as your high-school locker. -Lockr (it's pronounced /ˈlɒkəʳ/) is a 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/). +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/). ## How to use lockr From 56e4c049fae2f982fe2a74217158ba49e5d562c8 Mon Sep 17 00:00:00 2001 From: Aggelos Avgerinos Date: Tue, 5 Aug 2014 09:41:58 +0300 Subject: [PATCH 10/12] Add support for IE8 Polyfill indexOf --- lockr.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lockr.js b/lockr.js index ea21fa7..647ef6a 100644 --- a/lockr.js +++ b/lockr.js @@ -13,6 +13,28 @@ root.Lockr = Lockr; + if (!Array.prototype.indexOf) { + Array.prototype.indexOf = function(elt /*, from*/) + { + var len = this.length >>> 0; + + 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.salt = ""; Lockr.set = function (key, value) { From 18d6592f3d57b2d30f792deed57094135a02c332 Mon Sep 17 00:00:00 2001 From: Aggelos Avgerinos Date: Tue, 5 Aug 2014 10:20:12 +0300 Subject: [PATCH 11/12] Remove DS_Store --- .DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100755 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100755 index a81d0cd02d0225a68fc077886912d0f02d1fe0e0..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK!AiqG5PjQ5Db_>5OYvepK!oBCgcALMen1P2RG~4&HWyF9nYH49--P~v ziTkqW9dg{R#jG8V*Zr%w?;q!{OfU1(Y#DpAzZAbVVE!S+&5+;t|8*`f+VN z5WaT1s{qw(wno#U_Nss?pbC5|z~6_E8e`f{PQ7|^sBjro^?IibVIVPTOx zWX4hnE!DUyhOu;-58b-RVPVnIVcg}zxX8xcP>e*U{X}-OT{@Dht(C)ZtqrLSsIl?7!lF>9bLChDT#DP&#K;%&fG~1c SSfm*;{Sgouv{MECRDmy_PKAH~ From d18ebd68a890e1d72fc28ac7428dd1db3269b561 Mon Sep 17 00:00:00 2001 From: Aggelos Avgerinos Date: Tue, 5 Aug 2014 09:45:25 +0300 Subject: [PATCH 12/12] Bump Lockr to v0.7.0 --- bower.json | 2 +- lockr.min.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bower.json b/bower.json index f8fd378..e28c306 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "lockr", - "version": "0.5.0", + "version": "0.7.0", "ignore": [ "**/.*", "**/_*", diff --git a/lockr.min.js b/lockr.min.js index 2bce8bb..6b02d7a 100644 --- a/lockr.min.js +++ b/lockr.min.js @@ -1 +1 @@ -!function(root,factory){"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}); \ No newline at end of file +!function(root,factory){"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.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.sadd=function(key,value){var json,salted_key=this.salt+key,values=Lockr.smembers(key);if(values.indexOf(value)>-1)return null;try{values.push(value),json=JSON.stringify({data:values}),localStorage.setItem(salted_key,json)}catch(e){console&&console.warn("Lockr didn't successfully add the "+value+" to "+key+" set, because the localStorage is full.")}},Lockr.smembers=function(key){var value,salted_key=this.salt+key;try{value=JSON.parse(localStorage.getItem(salted_key))}catch(e){value=null}return null===value?[]:value.data||[]},Lockr.sismember=function(key,value){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){var json,index,salted_key=this.salt+key,values=Lockr.smembers(key,value);index=values.indexOf(value),index>-1&&values.splice(index,1),json=JSON.stringify({data:values});try{localStorage.setItem(salted_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}); \ No newline at end of file