@bemoje/fs 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/lib/JsonFile.js +32 -22
- package/src/lib/JsonFile.js.map +1 -1
package/package.json
CHANGED
package/src/lib/JsonFile.js
CHANGED
|
@@ -8,6 +8,16 @@ const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
|
8
8
|
const path_1 = tslib_1.__importDefault(require("path"));
|
|
9
9
|
const readJsonFileSync_1 = require("./readJsonFileSync");
|
|
10
10
|
const writeJsonFileSync_1 = require("./writeJsonFileSync");
|
|
11
|
+
function deepClone(obj) {
|
|
12
|
+
if (typeof obj !== 'object' || obj === null)
|
|
13
|
+
return obj;
|
|
14
|
+
try {
|
|
15
|
+
return JSON.parse(JSON.stringify(obj));
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
return '';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
11
21
|
/**
|
|
12
22
|
* Represents a JSON file manager that ensures immutable data since everything is serialized and deserialized before receiving or returning any data.
|
|
13
23
|
*/
|
|
@@ -17,7 +27,7 @@ class JsonFile extends events_1.default {
|
|
|
17
27
|
* @returns A deep copy of the data.
|
|
18
28
|
*/
|
|
19
29
|
get data() {
|
|
20
|
-
return
|
|
30
|
+
return deepClone(tslib_1.__classPrivateFieldGet(this, _JsonFile_data, "f"));
|
|
21
31
|
}
|
|
22
32
|
/**
|
|
23
33
|
* Constructs a new instance of the JsonFile class.
|
|
@@ -47,7 +57,7 @@ class JsonFile extends events_1.default {
|
|
|
47
57
|
* @returns The value associated with the key.
|
|
48
58
|
*/
|
|
49
59
|
get(key) {
|
|
50
|
-
return
|
|
60
|
+
return deepClone(tslib_1.__classPrivateFieldGet(this, _JsonFile_data, "f")[key]);
|
|
51
61
|
}
|
|
52
62
|
/**
|
|
53
63
|
* Sets the value associated with the specified key.
|
|
@@ -57,13 +67,22 @@ class JsonFile extends events_1.default {
|
|
|
57
67
|
set(key, value) {
|
|
58
68
|
if (tslib_1.__classPrivateFieldGet(this, _JsonFile_data, "f")[key] === value)
|
|
59
69
|
return;
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
70
|
+
if (value === undefined || value === null) {
|
|
71
|
+
if (this.has(key))
|
|
72
|
+
return this.delete(key);
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
const json = JSON.stringify(value);
|
|
76
|
+
if (json === JSON.stringify(tslib_1.__classPrivateFieldGet(this, _JsonFile_data, "f")[key]))
|
|
77
|
+
return;
|
|
78
|
+
value = JSON.parse(json);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
value = '';
|
|
82
|
+
}
|
|
83
|
+
this.emit('set', Date.now(), key, value);
|
|
64
84
|
tslib_1.__classPrivateFieldGet(this, _JsonFile_data, "f")[key] = value;
|
|
65
85
|
this.saveFile();
|
|
66
|
-
this.emit('set', Date.now(), key, value);
|
|
67
86
|
}
|
|
68
87
|
/**
|
|
69
88
|
* Checks if the specified key exists in the JSON file.
|
|
@@ -79,8 +98,8 @@ class JsonFile extends events_1.default {
|
|
|
79
98
|
*/
|
|
80
99
|
delete(key) {
|
|
81
100
|
delete tslib_1.__classPrivateFieldGet(this, _JsonFile_data, "f")[key];
|
|
82
|
-
this.saveFile();
|
|
83
101
|
this.emit('set', Date.now(), key, void 0);
|
|
102
|
+
this.saveFile();
|
|
84
103
|
}
|
|
85
104
|
/**
|
|
86
105
|
* Gets the value associated with the specified key, or sets it to the provided default value if it doesn't exist.
|
|
@@ -90,9 +109,9 @@ class JsonFile extends events_1.default {
|
|
|
90
109
|
*/
|
|
91
110
|
getOrElse(key, getValue) {
|
|
92
111
|
if (this.has(key)) {
|
|
93
|
-
return
|
|
112
|
+
return deepClone(this.get(key));
|
|
94
113
|
}
|
|
95
|
-
const value =
|
|
114
|
+
const value = deepClone(getValue());
|
|
96
115
|
this.set(key, value);
|
|
97
116
|
return value;
|
|
98
117
|
}
|
|
@@ -100,27 +119,18 @@ class JsonFile extends events_1.default {
|
|
|
100
119
|
* Clears all the data in the JSON file.
|
|
101
120
|
*/
|
|
102
121
|
clear() {
|
|
103
|
-
const time = Date.now();
|
|
104
122
|
for (const key of Object.keys(tslib_1.__classPrivateFieldGet(this, _JsonFile_data, "f"))) {
|
|
105
|
-
this.
|
|
123
|
+
this.set(key, undefined);
|
|
106
124
|
}
|
|
107
|
-
tslib_1.__classPrivateFieldSet(this, _JsonFile_data, {}, "f");
|
|
108
|
-
fs_1.default.writeFileSync(this.filepath, '{}', 'utf8');
|
|
109
125
|
}
|
|
110
126
|
/**
|
|
111
127
|
* Assigns new data to the JSON file.
|
|
112
128
|
* @param data - The data to assign.
|
|
113
129
|
*/
|
|
114
130
|
assign(data) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
for (const [key, value] of Object.entries(data)) {
|
|
118
|
-
if (tslib_1.__classPrivateFieldGet(this, _JsonFile_data, "f")[key] && JSON.stringify(tslib_1.__classPrivateFieldGet(this, _JsonFile_data, "f")[key]) !== JSON.stringify(value)) {
|
|
119
|
-
this.emit('set', time, key, value);
|
|
120
|
-
}
|
|
131
|
+
for (const [key, value] of Object.entries(deepClone(data))) {
|
|
132
|
+
this.set(key, value);
|
|
121
133
|
}
|
|
122
|
-
Object.assign(tslib_1.__classPrivateFieldGet(this, _JsonFile_data, "f"), data);
|
|
123
|
-
this.saveFile();
|
|
124
134
|
}
|
|
125
135
|
/**
|
|
126
136
|
* Ensures that the directory containing the JSON file exists.
|
package/src/lib/JsonFile.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JsonFile.js","sourceRoot":"","sources":["../../../../../packages/fs/src/lib/JsonFile.ts"],"names":[],"mappings":";;;;;AAAA,4DAAiC;AACjC,oDAAmB;AACnB,wDAAuB;AACvB,yDAAqD;AACrD,2DAAuD;AAEvD;;GAEG;AACH,MAAa,QAAwC,SAAQ,gB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