Comparing version 0.8.4 to 0.8.5
65
lockr.js
@@ -70,18 +70,14 @@ (function(root, factory) { | ||
} catch (e) { | ||
try { | ||
if(localStorage[query_key]) { | ||
value = JSON.parse('{"data":"' + localStorage.getItem(query_key) + '"}'); | ||
value = {data: localStorage.getItem(query_key)}; | ||
} else{ | ||
value = null; | ||
} | ||
} catch (e) { | ||
if (console) console.warn("Lockr could not load the item with key " + key); | ||
} | ||
} | ||
if(value === null) { | ||
if(!value) { | ||
return missing; | ||
} else if (typeof value.data !== 'undefined') { | ||
} | ||
else if (typeof value === 'object' && typeof value.data !== 'undefined') { | ||
return value.data; | ||
} else { | ||
return missing; | ||
} | ||
@@ -119,18 +115,39 @@ }; | ||
} | ||
if (value === null) | ||
return []; | ||
else | ||
return (value.data || []); | ||
return (value && value.data) ? 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 () { | ||
var keys = Object.keys(localStorage); | ||
Lockr.keys = function() { | ||
var keys = []; | ||
var allKeys = Object.keys(localStorage); | ||
if (Lockr.prefix.length === 0) { | ||
return allKeys; | ||
} | ||
allKeys.forEach(function (key) { | ||
if (key.indexOf(Lockr.prefix) !== -1) { | ||
keys.push(key.replace(Lockr.prefix, '')); | ||
} | ||
}); | ||
return keys; | ||
}; | ||
Lockr.getAll = function (includeKeys) { | ||
var keys = Lockr.keys(); | ||
if (includeKeys) { | ||
return keys.reduce(function (accum, key) { | ||
var tempObj = {}; | ||
tempObj[key] = Lockr.get(key); | ||
accum.push(tempObj); | ||
return accum; | ||
}, []); | ||
} | ||
return keys.map(function (key) { | ||
@@ -163,7 +180,15 @@ return Lockr.get(key); | ||
Lockr.rm = function (key) { | ||
localStorage.removeItem(key); | ||
var queryKey = this._getPrefixedKey(key); | ||
localStorage.removeItem(queryKey); | ||
}; | ||
Lockr.flush = function () { | ||
localStorage.clear(); | ||
if (Lockr.prefix.length) { | ||
Lockr.keys().forEach(function(key) { | ||
localStorage.removeItem(Lockr._getPrefixedKey(key)); | ||
}); | ||
} else { | ||
localStorage.clear(); | ||
} | ||
}; | ||
@@ -170,0 +195,0 @@ return Lockr; |
@@ -1,1 +0,1 @@ | ||
!function(root,factory){"undefined"!=typeof exports?"undefined"!=typeof module&&module.exports&&(exports=module.exports=factory(root,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 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){try{value=localStorage[query_key]?JSON.parse('{"data":"'+localStorage.getItem(query_key)+'"}'):null}catch(e){console&&console.warn("Lockr could not load the item with key "+key)}}return null===value?missing:"undefined"!=typeof value.data?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}); | ||
!function(root,factory){"undefined"!=typeof exports?"undefined"!=typeof module&&module.exports&&(exports=module.exports=factory(root,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 Array.prototype.indexOf||(Array.prototype.indexOf=function(elt){var len=this.length>>>0,from=Number(arguments[1])||0;for(from=from<0?Math.ceil(from):Math.floor(from),from<0&&(from+=len);from<len;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=localStorage[query_key]?{data:localStorage.getItem(query_key)}:null}return value?"object"==typeof value&&"undefined"!=typeof value.data?value.data:void 0: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 value&&value.data?value.data:[]},Lockr.sismember=function(key,value,options){return Lockr.smembers(key).indexOf(value)>-1},Lockr.keys=function(){var keys=[],allKeys=Object.keys(localStorage);return 0===Lockr.prefix.length?allKeys:(allKeys.forEach(function(key){key.indexOf(Lockr.prefix)!==-1&&keys.push(key.replace(Lockr.prefix,""))}),keys)},Lockr.getAll=function(includeKeys){var keys=Lockr.keys();return includeKeys?keys.reduce(function(accum,key){var tempObj={};return tempObj[key]=Lockr.get(key),accum.push(tempObj),accum},[]):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){var queryKey=this._getPrefixedKey(key);localStorage.removeItem(queryKey)},Lockr.flush=function(){Lockr.prefix.length?Lockr.keys().forEach(function(key){localStorage.removeItem(Lockr._getPrefixedKey(key))}):localStorage.clear()},Lockr}); |
{ | ||
"name": "lockr", | ||
"version": "0.8.4", | ||
"description": "A minimal API wrapper for localStorage", | ||
"version": "0.8.5", | ||
"author": { | ||
@@ -22,11 +23,14 @@ "name": "Dimitris Tsironis", | ||
"main": "lockr.js", | ||
"scripts": { | ||
"build": "grunt" | ||
}, | ||
"devDependencies": { | ||
"grunt-contrib-jshint": "~0.1.1", | ||
"grunt-contrib-jasmine": "~0.6.1", | ||
"grunt-contrib-watch": "~0.2.0", | ||
"grunt-contrib-clean": "~0.4.0", | ||
"grunt-contrib-uglify": "~0.2.7", | ||
"grunt": "~0.4.0", | ||
"grunt-cli": "~0.1.6" | ||
"grunt-contrib-jshint": "~0.11.0", | ||
"grunt-contrib-jasmine": "~1.0.0", | ||
"grunt-contrib-watch": "~0.6.1", | ||
"grunt-contrib-clean": "~0.7.0", | ||
"grunt-contrib-uglify": "~0.11.1", | ||
"grunt": "~0.4.5", | ||
"grunt-cli": "~0.1.13" | ||
} | ||
} |
![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. | ||
[![Build Status](https://travis-ci.org/tsironis/lockr.svg?branch=master)](https://travis-ci.org/tsironis/lockr) | ||
[![npm version](https://badge.fury.io/js/lockr.svg)](http://badge.fury.io/js/lockr) | ||
[![CodeClimate](https://codeclimate.com/github/tsironis/lockr/badges/gpa.svg)](https://codeclimate.com/github/tsironis/lockr) | ||
[![Dependencies](https://david-dm.org/tsironis/lockr.svg?theme=shields.io)](https://david-dm.org/tsironis/lockr) | ||
[![devDependency Status](https://david-dm.org/tsironis/lockr/dev-status.svg)](https://david-dm.org/tsironis/lockr#info=devDependencies) | ||
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/). | ||
@@ -18,4 +21,10 @@ | ||
or download it manually from [here](https://raw2.github.com/tsironis/lockr/master/lockr.js) and hook it in your HTML. | ||
or you use npm to install | ||
```js | ||
npm i lockr --save | ||
``` | ||
or maybe download it manually from [here](https://raw.github.com/tsironis/lockr/master/lockr.js) and hook it in your HTML. | ||
```html | ||
@@ -27,2 +36,21 @@ <script src="/path/to/lockr.js" type="text/javascript"></script> | ||
```Lockr.prefix``` - String | ||
> Set a prefix to a string value that is going to be prepended to each key saved by Lockr. | ||
*Example* | ||
```js | ||
Lockr.prefix = 'lockr_'; | ||
Lockr.set('username', 'Coyote'); // Saved as string | ||
localStorage.getItem('username'); | ||
> null | ||
localStorage.getItem('lockr_username'); | ||
> {"data":123} | ||
``` | ||
*Please note that* when prefix is set, ```flush``` method deletes only keys that are prefixed, and ignores the rest. | ||
--- | ||
```Lockr.set``` - arguments: *[ key, value ]* {String, Number, Array or Object} | ||
@@ -151,2 +179,13 @@ | ||
``` | ||
```Lockr.getAll``` - arguments: *[includeKeys] {Boolean}* | ||
> Returns contents of `localStorage` as an Array of dictionaries that contain key and value of the saved item. | ||
*Example* | ||
```js | ||
Lockr.getAll(true); | ||
> [{"username": "Coyote"}, {"user_id": 12345}, {"users": [{name: 'John Doe', age: 18}, {name: 'Jane Doe', age: 19}]}] | ||
``` | ||
--- | ||
@@ -153,0 +192,0 @@ |
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
14053
155
203