-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
239 lines (200 loc) · 6.01 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* module name: electron-data
* author: Dominik Winter
* version: 2.1.2
* release date: 14.03.2020
*/
(() => {
'use strict';
const fs = require('fs-extra');
const home = require('user-home');
const path = require('path');
const has_json_ext = require('./lib/has-json-ext');
const def_options = {
filename: 'data',
path: home +'/.electron-app',
autosave: false,
prettysave: false,
lastUpdate: false
};
const self = {
data: {},
options: {}
};
/**
* Configuration for electron-data
*
* @param {Object} options - An object containing the options to override
*/
const config = options => {
// Set the options
self.options = Object.assign(def_options, options);
// Build the filepath
let filename = (has_json_ext(self.options.filename)) ? self.options.filename : self.options.filename +'.json';
self.filepath = path.join(self.options.path, filename);
};
/**
* Get the current settings
*
* @return {Promise<Object>} The options object
*/
const getOptions = () => {
return Promise.resolve(self.options);
};
/**
* Checks if the stored data has a value with given key.
*
* @param {String} key - A String to search for in the data object properties
* @return {Promise<Boolean>} A boolean value, either true when key exists or false if it doesn´t
*/
const has = key => {
return Promise.resolve(self.data.hasOwnProperty(key));
};
/**
* Get the value for given key.
*
* @param {String} key - A String to search for in the data object properties.
* @return {Promise<?>} Any type of valid JSON value, if the key exists.
*/
const get = key => {
return has(key)
.then(has => {
if (!has) throw new Error(`No value for: ${key}`);
return self.data[key];
});
};
/**
* Get an object containing key => value pairs for given keys.
* If one of the keys isn´t presented in the data object, it will be skipped.
*
* @param {Array<String>} keys - An array of keys.
* @return {Promise<Object>} The object containing the data.
*/
const getMany = keys => {
if (!keys || !Array.isArray(keys)) return Promise.reject(new Error('keys must be an array.'));
let i = 0,
len = keys.length,
promises = [],
data = {};
for (; i < len; i++) promises.push(has(keys[i]));
return Promise.all(promises)
.then(has_keys => {
i = 0;
promises = [];
for (; i < len; i++) {
if (has_keys[i] === true) promises.push(get(keys[i]));
}
return Promise.all(promises)
.then(values => {
i = 0;
for (; i < len; i++) data[keys[i]] = values[i];
return data;
});
});
};
/**
* Resolves the whole data object, even it´s empty.
*
* @return {Promise<Object>} The whole data object.
*/
const getAll = () => {
return Promise.resolve(self.data);
};
/**
* Set a key => value pair.
*
* @param {String} key - A String to add to the data object properties.
* @param {?} value - Any valid JSON value to add to "key"
* @return {Promise<Object>} The whole data object
*/
const set = (key, value) => {
if (!key) return Promise.reject(new Error('key must be given.'));
if (typeof key !== 'string') return Promise.reject(new Error('key must be a string.'));
if (value === undefined || (typeof value === 'number' && isNaN(value))) return Promise.reject(new Error('value must be a valid JSON value. See http://www.json.org/'));
try {
self.data[key] = value;
if (self.options.autosave) {
return save().then(() => self.data);
} else {
return Promise.resolve(self.data);
}
} catch (e) {
return Promise.reject(e);
}
};
/**
* Set many key => value pairs at once.
*
* @param {Object} obj - The data to store.
* @return {Promise<Object>} The whole data object.
*/
const setMany = obj => {
try {
for (let p in obj) {
if (obj[p] === undefined || (typeof obj[p] === 'number' && isNaN(obj[p]))) delete obj[p];
}
self.data = Object.assign({}, self.data, obj);
if (self.options.autosave) {
return save().then(() => self.data);
} else {
return Promise.resolve(self.data);
}
} catch (e) {
return Promise.reject(e);
}
};
/**
* Get all keys from the data object.
*
* @return {Promise<Array>} An array containing all keys.
*/
const keys = () => {
let keys = [];
for (let p in self.data) keys.push(p);
return Promise.resolve(keys);
}
/**
* Remove a key => value at given key.
*
* @param {String} key - A String to remove from the data object.
* @return {Promise<boolean>|Promise<Error>}
*/
const unset = key => {
if (!key) return Promise.reject(new Error('key must be given.'));
try {
let deleted = delete self.data[key];
if (self.options.autosave) {
return save().then(() => deleted);
} else {
return Promise.resolve(deleted);
}
} catch (e) {
return Promise.reject(e);
}
};
/**
* Clear the whole data object. Caution: The data can´t be recoverd.
*
* @return {Promise<Object>} An empty object
*/
const clear = () => {
self.data = {};
if (self.options.autosave) {
return save().then(() => self.data);
} else {
return Promise.resolve(self.data);
}
};
/**
* Save the data to a JSON file. Change the config to move the location and the filename.
*
* @return {Promise<undefined>|Promise<Error>}
*/
const save = () => {
let pretty = (self.options.prettysave) ? 2 : null,
data = Object.assign({}, self.data);
if (self.options.lastUpdate) data.lastUpdate = new Date(Date.now()).toISOString();
return fs.outputFile(self.filepath, JSON.stringify(data, null, pretty), 'utf-8');
};
module.exports = {config, getOptions, has, get, getMany, getAll, set, setMany, keys, unset, clear, save};
})();