-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
205 lines (193 loc) · 7.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* @overview Utilizing React native AsyncStorage Api in easier way when it comes for storing objects as well as arrays
* @license MIT
*/
import AsyncStorage from '@react-native-async-storage/async-storage';
var _ = require('lodash');
class LocalStorage {
/**
* Get a one or more value for a key or array of keys from AsyncStorage
* @param {String|Array} key A key or array of keys
* @return {Promise}
*/
async get(key) {
try {
if (!Array.isArray(key)) {
const value = await AsyncStorage.getItem(key);
const item = JSON.parse(value);
return item;
} else {
const values = await AsyncStorage.multiGet(key)
return values.map(value => {
return JSON.parse(value[1]);
});
}
} catch (error) {
return Promise.reject(error)
}
}
/**
* Save a key value pair or a series of key value pairs to AsyncStorage.
* @param {String|Array} key The key or an array of key/value pairs
* @param {Any} value The value to save
* @return {Promise}
*/
async save(key, value) {
try {
if (!Array.isArray(key)) {
await AsyncStorage.setItem(key, JSON.stringify(value));
} else {
var pairs = key.map(function(pair) {
return [pair[0], JSON.stringify(pair[1])];
});
await AsyncStorage.multiSet(pairs);
}
} catch (error) {
return Promise.reject(error)
}
}
/**
* Updates the value in the store for a given key in AsyncStorage. If the value is a string it will be replaced. If the value is an object it will be deep merged.
* @param {String} key The key
* @param {Value} value The value to update with
* @return {Promise}
*/
async update(key, value) {
try {
const item = await this.get(key)
value = typeof value === 'string' ? value : _.merge({}, item, value);
await AsyncStorage.setItem(key, JSON.stringify(value));
return value;
} catch (error) {
return Promise.reject(error)
}
}
/**
* Delete the value for a given key in AsyncStorage.
* @param {String|Array} key The key or an array of keys to be deleted
* @return {Promise}
*/
async delete(key) {
try {
if (Array.isArray(key)) {
return await AsyncStorage.multiRemove(key);
} else {
return await AsyncStorage.removeItem(key);
}
} catch (error) {
return Promise.reject(error)
}
}
/**
* Get all keys in AsyncStorage.
* @return {Promise} A promise which when it resolves gets passed the saved keys in AsyncStorage.
*/
async keys() {
try {
return await AsyncStorage.getAllKeys();
} catch (error) {
return Promise.reject(error)
}
}
/**
* Push a value onto an array stored in AsyncStorage by key or create a new array in AsyncStorage for a key if it's not yet defined.
* @param {String} key They key
* @param {Any} value The value to push onto the array
* @param {Boolean} [isExist=false] check existence before pushing onto array, if its false it wont check for existence
* @param {Function} [predicate] The function invoked per element.
* @return {Boolean} returns wether the item pushed to the array or not
*/
async push(key, value, isExist = false, predicate) {
try {
const currentValue = await this.get(key)
if (currentValue === null) {
// if there is no current value populate it with the new value
await this.save(key, [value]);
return [value]
}
if (Array.isArray(currentValue)) {
let exist = false
if (isExist) {
if (predicate) {
exist = _.some(currentValue, (item) => predicate(item));
} else {
exist = _.some(currentValue, function(item) {
return item === value
});
}
}
if (!exist)
{
await this.save(key, [...currentValue, value]);
return [...currentValue, value]
}else{
return currentValue
}
}
return Promise.reject(new Error(`Existing value for key "${key}" must be of type null or Array, received ${typeof currentValue}.`));
} catch (error) {
return Promise.reject(error)
}
}
/**
* update an item from an array stored in AsyncStorage, If the new value is an object it will be deep merged.
* @param {String} key They key
* @param {Any} value The value to look for to update from the array of string, if array of objects is the value to match with
* @param {String|Array} [path] The path of the property to get.
* @param {Any} newValue The new Value to be updated with
* @return {Promise}
*/
async set(key, value, path, newValue) {
try {
const currentValue = await this.get(key)
if (currentValue === null) {
return Promise.reject(new Error(`There is no Array with key "${key}" stored, received ${typeof currentValue}.`))
}
if (Array.isArray(currentValue)) {
if (path) {
_.remove(currentValue, [path, value])
} else {
_.remove(currentValue, function(v) {
return v === value;
})
}
currentValue.push(newValue)
await this.save(key, currentValue);
return currentValue
}
return Promise.reject(new Error(`Existing value for key "${key}" must be of type null or Array, received ${typeof currentValue}.`));
} catch (error) {
return Promise.reject(error)
}
}
/**
* delete an item from an array stored in AsyncStorage by its value
* @param {String} key They key
* @param {Any} value The value to delete from the array of string, if array of objects is the value to match with
* @param {String|Array} [path] The path of the property to get.
* @return {Promise}
*/
async pop(key, value, path) {
try {
const currentValue = await this.get(key)
if (currentValue === null) {
return Promise.reject(new Error(`There is no Array with key "${key}" stored, received ${typeof currentValue}.`));
}
if (Array.isArray(currentValue)) {
if (path) {
_.remove(currentValue, [path, value])
} else {
_.remove(currentValue, function(v) {
return v === value;
})
}
await this.save(key, currentValue);
return currentValue
}
return Promise.reject(Error(`Existing value for key "${key}" must be of type null or Array, received ${typeof currentValue}.`));
} catch (error) {
return Promise.reject(error)
}
}
}
export default new LocalStorage()