Skip to content

Commit

Permalink
Merge pull request #8 from eavgerinos/add_sets
Browse files Browse the repository at this point in the history
Add sets support
  • Loading branch information
tsironis committed Aug 7, 2014
2 parents cd631e1 + d18ebd6 commit 7ef22e4
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 3 deletions.
Binary file removed .DS_Store
Binary file not shown.
59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (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

Expand Down Expand Up @@ -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```
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": "lockr",
"version": "0.5.0",
"version": "0.7.0",
"ignore": [
"**/.*",
"**/_*",
Expand Down
77 changes: 77 additions & 0 deletions lockr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -40,6 +62,43 @@
return (value.data || missing);
};

Lockr.sadd = function(key, value) {
var salted_key = this.salt + key, json;

var 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) {
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 {
value = JSON.parse(localStorage.getItem(salted_key));
} catch (e) {
value = null;
}

if (value === null)
return [];
else
return (value.data || []);
};

Lockr.sismember = function(key, value) {
return Lockr.smembers(key).indexOf(value) > -1;
};

Lockr.getAll = function () {
var keys = Object.keys(localStorage);

Expand All @@ -48,6 +107,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);
};
Expand Down
2 changes: 1 addition & 1 deletion lockr.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions specs/lockrSpecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('Lockr::Saving data', function () {
expect(localStorage.getItem('my_hash')).toContain('whatsup');
});
});

describe('Lockr::Retrieving data', function () {
it('should get a hash object from the localStorage', function () {
var integer = Lockr.get('test');
Expand Down Expand Up @@ -67,6 +68,40 @@ 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('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";
Expand Down

0 comments on commit 7ef22e4

Please sign in to comment.