react-native-data-storage
Advanced tools
Comparing version 0.0.1 to 0.1.0
24
index.js
@@ -60,5 +60,4 @@ import { AsyncStorage } from 'react-native'; | ||
var output = {}; | ||
for(var k in key){ | ||
if(k in data) output[k] = data[k]; | ||
else throw new Error(`Key ${k} not found`); | ||
for(var k of key){ | ||
if(k in data) output[k] = data[k]; // else simply don't add the key | ||
} | ||
@@ -69,3 +68,3 @@ | ||
if(key in data) return data[key]; | ||
else throw new Error(`Key ${key} not found`); | ||
else throw new Error(`Key '${key}' not found`); | ||
} | ||
@@ -78,3 +77,7 @@ }); | ||
.then((data)=>{ | ||
return key in data; | ||
if(Array.isArray(key)){ | ||
var output = {}; | ||
for(var k of key) output[k] = k in data; | ||
return output; | ||
}else return key in data; | ||
}); | ||
@@ -86,3 +89,12 @@ } | ||
.then((data)=>{ | ||
delete data[key]; | ||
if(Array.isArray(key)){ | ||
var output = {}; | ||
for(var k of key){ | ||
if(k in data) delete data[k]; | ||
} | ||
}else{ | ||
if(key in data) delete data[key]; | ||
else throw new Error(`Key '${key}' not found`); | ||
} | ||
return this.setAll(data); | ||
@@ -89,0 +101,0 @@ }); |
{ | ||
"name": "react-native-data-storage", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "A data storage tool for React Native written on top of AsyncStorage", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,16 +5,18 @@ #DataStorage | ||
With `react-native-data-storage` you can set, get, remove and check for data. | ||
You can save integers, booleans, strings, primitive objects and arrays (anything that can be represented as in the JSON format). | ||
The methods always return a promise. | ||
With `react-native-data-storage` you can set, get, remove and check for data. You can save integers, booleans, strings, primitive objects and arrays (anything that can be represented as in the JSON format). The methods always return a promise. | ||
##Installation | ||
npm install react-native-data-storage --save | ||
`npm install react-native-data-storage --save` | ||
##Reference | ||
`set(key, value)`: sets a new value to the storage. | ||
`get(key)`: gets a value in the storage. Promise is rejected if key is not found. An array of keys can be passed and an object with the values will be returned. | ||
`has(key)`: checks whether a value has been set to the storage. Promise resolves with `true` of `false`. | ||
`remove(key)`: removes a value from the storage. | ||
`set(key, [value])`: sets a new value to the storage. An object can be passed and it will be merged and overwrite the current key-values. | ||
`get(key)`: gets a value in the storage. Promise is rejected if key is not found. An array of keys can be passed and an object with the same keys provided and their values will be returned. | ||
`has(key)`: checks whether a value has been set to the storage. Promise resolves with `true` of `false`. An array of keys can be passed and an object with the same keys provided will be returned. | ||
`remove(key)`: removes a value from the storage. An array of keys can be passed. | ||
`getAll()`: gets all the values in the storage as an object. | ||
@@ -42,2 +44,26 @@ | ||
}); | ||
Storage.set('name','John Doe'); | ||
//When an array is passed and the key is not in the store, it is not present in the returned object | ||
Storage.get(['name', 'email']) | ||
.then((data)=>{ | ||
if('name' in data) console.log('There is name in the storage', data.name); | ||
else console.log('If the key was not returned, then it doesn\'t exist'); | ||
}); | ||
//When a keyname is passed, it throws an error if the key is not found in the storage | ||
Storage.get('email') | ||
.then((data)=>{ | ||
//do something | ||
}) | ||
.catch((err)=>{ | ||
console.log(err); //Key 'email' not found | ||
}); | ||
//checking for multiple keys | ||
Storage.has(['name','email']) | ||
.then((data)=>{ | ||
console.log(data); //{name: true, email: false}; | ||
}); | ||
``` |
5076
90
68