Skip to content
This repository was archived by the owner on Apr 6, 2024. It is now read-only.

Commit a3c0977

Browse files
committed
Bugfixes and pretty print for json
1 parent 5efc633 commit a3c0977

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

scripts/JSONhandler.js

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function initStorage() {
6868
}
6969
// File does not exist: Create it and write default values in it.
7070
else {
71-
fs.appendFileSync( mainStoragePath, JSON.stringify( defaultStorageObj ) );
71+
fs.appendFileSync( mainStoragePath, JSON.stringify( defaultStorageObj, null, 4 ) );
7272
}
7373
}
7474

@@ -117,7 +117,7 @@ function readPreference( name ) {
117117
}
118118
// File does not exist: Create it, write default values and return a default value.
119119
else {
120-
fs.appendFileSync( settingsPath, JSON.stringify( defaultObj ) );
120+
fs.appendFileSync( settingsPath, JSON.stringify( defaultObj, null, 4 ) );
121121
return defaultObj[name];
122122
}
123123
}
@@ -135,17 +135,17 @@ function storePreference( name, value ) {
135135
if ( fs.existsSync( settingsPath ) ) {
136136
var settingsObj = JSON.parse( fs.readFileSync( settingsPath ) );
137137
settingsObj[name] = value;
138-
fs.writeFileSync( settingsPath, JSON.stringify( settingsObj ) );
138+
fs.writeFileSync( settingsPath, JSON.stringify( settingsObj, null, 4 ) );
139139
}
140140
// File does not exist: Create it and write default values in it.
141141
// When done, we can set the value.
142142
else {
143143
// Create default file.
144-
fs.appendFileSync( settingsPath, JSON.stringify( defaultObj ) );
144+
fs.appendFileSync( settingsPath, JSON.stringify( defaultObj, null, 4 ) );
145145
// Change the value of the specified field.
146146
var settingsObj = JSON.parse( fs.readFileSync( settingsPath ) );
147147
settingsObj[name] = value;
148-
fs.writeFileSync( settingsPath, JSON.stringify( settingsObj ) );
148+
fs.writeFileSync( settingsPath, JSON.stringify( settingsObj, null, 4 ) );
149149
}
150150
}
151151

@@ -168,7 +168,7 @@ function readMainStorage( field ) {
168168
}
169169
// File does not exist: Create it, write default values and return a default value.
170170
else {
171-
fs.appendFileSync( mainStoragePath, JSON.stringify( defaultStorageObj ) );
171+
fs.appendFileSync( mainStoragePath, JSON.stringify( defaultStorageObj, null, 4 ) );
172172
return defaultStorageObj[field];
173173
}
174174
}
@@ -183,17 +183,17 @@ function writeMainStorage( field, value ) {
183183
if ( fs.existsSync( mainStoragePath ) ) {
184184
var mainStorageObj = JSON.parse( fs.readFileSync( mainStoragePath ) );
185185
mainStorageObj[field] = value;
186-
fs.writeFileSync( mainStoragePath, JSON.stringify( mainStorageObj ) );
186+
fs.writeFileSync( mainStoragePath, JSON.stringify( mainStorageObj, null, 4 ) );
187187
}
188188
// File does not exist: Create it and write default values in it.
189189
// When done, we can set the value.
190190
else {
191191
// Create default file.
192-
fs.appendFileSync( mainStoragePath, JSON.stringify( defaultStorageObj ) );
192+
fs.appendFileSync( mainStoragePath, JSON.stringify( defaultStorageObj, null, 4 ) );
193193
// Change the value of the specified field.
194194
var mainStorageObj = JSON.parse( fs.readFileSync( mainStoragePath ) );
195195
mainStorageObj[field] = value;
196-
fs.writeFileSync( mainStoragePath, JSON.stringify( mainStorageObj ) );
196+
fs.writeFileSync( mainStoragePath, JSON.stringify( mainStorageObj, null, 4 ) );
197197
}
198198
}
199199

@@ -237,17 +237,35 @@ function getData( file, quest ) {
237237
else {
238238
// At least one param matched? Return true (ret=true) because connector is "or".
239239
if ( quest.connector === "or" ) {
240-
if ( dat[qu[0]] === qu[1] ) {
241-
ret = true;
242-
return true;
243-
}
240+
if (qu[0] === "budget") { // Handle multiple budgets like "b1, b2, b3"
241+
let budgets = dat[qu[0]].split(",");
242+
ret = budgets.some((value, index, array) => {
243+
return value.trim() === qu[1];
244+
});
245+
246+
if (ret) return true;
247+
} else {
248+
if ( dat[qu[0]] === qu[1] ) {
249+
ret = true;
250+
return true;
251+
}
252+
}
244253
}
245254
// One param does not match => "and" connector can not be satisfied (ret=false).
246255
else {
247-
if ( dat[qu[0]] !== qu[1] ) {
248-
ret = false;
249-
return true;
250-
}
256+
if (qu[0] === "budget") { // Handle multiple budgets like "b1, b2, b3"
257+
let budgets = dat[qu[0]].split(",");
258+
ret = !budgets.every((value, index, array) => {
259+
return value.trim() !== qu[1];
260+
});
261+
262+
if (!ret) return true;
263+
} else {
264+
if ( dat[qu[0]] !== qu[1] ) {
265+
ret = false;
266+
return true;
267+
}
268+
}
251269
}
252270
}
253271
});
@@ -348,12 +366,12 @@ function storeData( data ) {
348366
content.push( data );
349367
// Sort the data (oldest data first).
350368
content = sortData( content );
351-
fs.writeFileSync( dataPath, JSON.stringify( content ) );
369+
fs.writeFileSync( dataPath, JSON.stringify( content, null, 4 ) );
352370
}
353371
// File does not exist: Create it and write the data in it.
354372
else {
355373
// The content is an array containing JSON objects.
356-
fs.appendFileSync( dataPath, "[" + JSON.stringify( data ) + "]" );
374+
fs.appendFileSync( dataPath, "[" + JSON.stringify( data, null, 4 ) + "]" );
357375
}
358376
}
359377

@@ -366,11 +384,11 @@ function replaceData( file, data ) {
366384
var dataPath = readPreference( "path" ) + path.sep + file;
367385
// File exists: Overwrite the data in it.
368386
if ( fs.existsSync( dataPath ) ) {
369-
fs.writeFileSync( dataPath, JSON.stringify( data ) );
387+
fs.writeFileSync( dataPath, JSON.stringify( data, null, 4 ) );
370388
}
371389
// File does not exist: Create it and write the data in it.
372390
else {
373-
fs.appendFileSync( dataPath, JSON.stringify( data ) );
391+
fs.appendFileSync( dataPath, JSON.stringify( data, null, 4 ) );
374392
}
375393
}
376394

@@ -415,7 +433,7 @@ function deleteData( file, data ) {
415433
"allTimeSpendings", allTimeTransactions );
416434
}
417435
}
418-
fs.writeFileSync( dataPath, JSON.stringify( newContent ) );
436+
fs.writeFileSync( dataPath, JSON.stringify( newContent, null, 4 ) );
419437
}
420438
}
421439

0 commit comments

Comments
 (0)