Skip to content

Commit 7ef22e4

Browse files
committed
Merge pull request #8 from eavgerinos/add_sets
Add sets support
2 parents cd631e1 + d18ebd6 commit 7ef22e4

File tree

6 files changed

+172
-3
lines changed

6 files changed

+172
-3
lines changed

.DS_Store

-6 KB
Binary file not shown.

README.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> A minimal API wrapper for localStorage. Simple as your high-school locker.
44
5-
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/).
5+
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/).
66

77
## How to use lockr
88

@@ -72,6 +72,63 @@ Lockr.get('score', 0):
7272

7373
---
7474

75+
```Lockr.sadd``` - arguments *[ key, value ]*{String, Number, Array or Object}
76+
77+
> Adds a unique value to a particular set under a hash key.
78+
79+
*Example*
80+
81+
```js
82+
Lockr.sadd("wat", 1); // [1]
83+
Lockr.sadd("wat", 2); // [1, 2]
84+
Lockr.sadd("wat", 1); // [1, 2]
85+
```
86+
87+
---
88+
89+
```Lockr.smembers``` - arguments *[ key ]*
90+
91+
> Returns the values of a particular set under a hash key.
92+
93+
*Example*
94+
95+
```js
96+
Lockr.sadd("wat", 42);
97+
Lockr.sadd("wat", 1337);
98+
Lockr.smembers("wat"); // [42, 1337]
99+
```
100+
101+
---
102+
103+
```Lockr.sismember``` - arguments *[ key, value ]*
104+
105+
> Returns whether the value exists in a particular set under a hash key.
106+
107+
*Example*
108+
109+
```js
110+
Lockr.sadd("wat", 1);
111+
Lockr.sismember("wat", 1); // true
112+
Lockr.sismember("wat', 2); // false
113+
```
114+
115+
---
116+
117+
```Lockr.srem``` - arguments *[ key, value ]*
118+
119+
> Removes a value from a particular set under a hash key.
120+
121+
*Example*
122+
123+
```js
124+
Lockr.sadd("wat", 1);
125+
Lockr.sadd("wat", 2);
126+
Lockr.srem("wat", 1);
127+
Lockr.smembers("wat"); // [2]
128+
```
129+
130+
---
131+
75132
```Lockr.getAll``` - arguments: *null*
76133
77134
> Returns all saved values & objects, in an ```Array```

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lockr",
3-
"version": "0.5.0",
3+
"version": "0.7.0",
44
"ignore": [
55
"**/.*",
66
"**/_*",

lockr.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,28 @@
1313

1414
root.Lockr = Lockr;
1515

16+
if (!Array.prototype.indexOf) {
17+
Array.prototype.indexOf = function(elt /*, from*/)
18+
{
19+
var len = this.length >>> 0;
20+
21+
var from = Number(arguments[1]) || 0;
22+
from = (from < 0)
23+
? Math.ceil(from)
24+
: Math.floor(from);
25+
if (from < 0)
26+
from += len;
27+
28+
for (; from < len; from++)
29+
{
30+
if (from in this &&
31+
this[from] === elt)
32+
return from;
33+
}
34+
return -1;
35+
};
36+
}
37+
1638
Lockr.salt = "";
1739

1840
Lockr.set = function (key, value) {
@@ -40,6 +62,43 @@
4062
return (value.data || missing);
4163
};
4264

65+
Lockr.sadd = function(key, value) {
66+
var salted_key = this.salt + key, json;
67+
68+
var values = Lockr.smembers(key);
69+
70+
if (values.indexOf(value) > -1) {
71+
return null;
72+
}
73+
74+
try {
75+
values.push(value);
76+
json = JSON.stringify({"data": values});
77+
localStorage.setItem(salted_key, json);
78+
} catch (e) {
79+
if (console) console.warn("Lockr didn't successfully add the "+ value +" to "+ key +" set, because the localStorage is full.");
80+
}
81+
};
82+
83+
Lockr.smembers = function(key) {
84+
var salted_key = this.salt + key, value;
85+
86+
try {
87+
value = JSON.parse(localStorage.getItem(salted_key));
88+
} catch (e) {
89+
value = null;
90+
}
91+
92+
if (value === null)
93+
return [];
94+
else
95+
return (value.data || []);
96+
};
97+
98+
Lockr.sismember = function(key, value) {
99+
return Lockr.smembers(key).indexOf(value) > -1;
100+
};
101+
43102
Lockr.getAll = function () {
44103
var keys = Object.keys(localStorage);
45104

@@ -48,6 +107,24 @@
48107
});
49108
};
50109

110+
Lockr.srem = function(key, value) {
111+
var salted_key = this.salt + key, json, index;
112+
var values = Lockr.smembers(key, value);
113+
114+
index = values.indexOf(value);
115+
116+
if (index > -1)
117+
values.splice(index, 1);
118+
119+
json = JSON.stringify({"data": values});
120+
121+
try {
122+
localStorage.setItem(salted_key, json);
123+
} catch (e) {
124+
if (console) console.warn("Lockr couldn't remove the "+ value +" from the set "+ key);
125+
}
126+
};
127+
51128
Lockr.rm = function (key) {
52129
localStorage.removeItem(key);
53130
};

lockr.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.

specs/lockrSpecs.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('Lockr::Saving data', function () {
1515
expect(localStorage.getItem('my_hash')).toContain('whatsup');
1616
});
1717
});
18+
1819
describe('Lockr::Retrieving data', function () {
1920
it('should get a hash object from the localStorage', function () {
2021
var integer = Lockr.get('test');
@@ -67,6 +68,40 @@ describe('Lockr::Flushing data', function () {
6768
expect(contents.length).toBe(0);
6869
});
6970
});
71+
72+
describe('Lockr.sadd', function () {
73+
it('saves a set under the given key in the localStorage', function () {
74+
Lockr.sadd('test_set', 1)
75+
Lockr.sadd('test_set', 2)
76+
expect(localStorage.getItem('test_set')).toEqual('{"data":[1,2]}');
77+
});
78+
79+
it('does not add the same value again', function() {
80+
Lockr.sadd('test_set, 1');
81+
expect(Lockr.smembers('test_set')).toEqual([1, 2]);
82+
});
83+
});
84+
85+
describe('Lockr.smembers', function() {
86+
it('returns all the values for given key', function() {
87+
expect(Lockr.smembers('test_set')).toEqual([1, 2]);
88+
});
89+
});
90+
91+
describe('Lock.sismember', function() {
92+
it('returns true if the value exists in the given set(key)', function () {
93+
expect(Lockr.sismember('test_set', 1)).toEqual(true);
94+
expect(Lockr.sismember('test_set', 34)).toEqual(false);
95+
});
96+
});
97+
98+
describe('Lock.srem', function() {
99+
it('removes value from collection if exists', function() {
100+
Lockr.srem('test_set', 1);
101+
expect(Lockr.sismember('test_set', 1)).toEqual(false);
102+
});
103+
});
104+
70105
describe('Lockr::Salted', function() {
71106
it('should set a salt key', function() {
72107
Lockr.salt = "imasalt";

0 commit comments

Comments
 (0)