@bemoje/fs 0.2.0 → 0.2.2
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 +33 -21
- 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.
|
|
@@ -55,15 +65,26 @@ class JsonFile extends events_1.default {
|
|
|
55
65
|
* @param value - The value to set.
|
|
56
66
|
*/
|
|
57
67
|
set(key, value) {
|
|
68
|
+
value = deepClone(value);
|
|
58
69
|
if (tslib_1.__classPrivateFieldGet(this, _JsonFile_data, "f")[key] === value)
|
|
59
70
|
return;
|
|
60
|
-
|
|
61
|
-
|
|
71
|
+
if (value === undefined || value === null) {
|
|
72
|
+
if (this.has(key))
|
|
73
|
+
this.delete(key);
|
|
62
74
|
return;
|
|
63
|
-
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
const json = JSON.stringify(value);
|
|
78
|
+
if (json === JSON.stringify(tslib_1.__classPrivateFieldGet(this, _JsonFile_data, "f")[key]))
|
|
79
|
+
return;
|
|
80
|
+
value = JSON.parse(json);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
value = '';
|
|
84
|
+
}
|
|
85
|
+
this.emit('set', Date.now(), key, value);
|
|
64
86
|
tslib_1.__classPrivateFieldGet(this, _JsonFile_data, "f")[key] = value;
|
|
65
87
|
this.saveFile();
|
|
66
|
-
this.emit('set', Date.now(), key, value);
|
|
67
88
|
}
|
|
68
89
|
/**
|
|
69
90
|
* Checks if the specified key exists in the JSON file.
|
|
@@ -79,8 +100,8 @@ class JsonFile extends events_1.default {
|
|
|
79
100
|
*/
|
|
80
101
|
delete(key) {
|
|
81
102
|
delete tslib_1.__classPrivateFieldGet(this, _JsonFile_data, "f")[key];
|
|
82
|
-
this.saveFile();
|
|
83
103
|
this.emit('set', Date.now(), key, void 0);
|
|
104
|
+
this.saveFile();
|
|
84
105
|
}
|
|
85
106
|
/**
|
|
86
107
|
* Gets the value associated with the specified key, or sets it to the provided default value if it doesn't exist.
|
|
@@ -90,9 +111,9 @@ class JsonFile extends events_1.default {
|
|
|
90
111
|
*/
|
|
91
112
|
getOrElse(key, getValue) {
|
|
92
113
|
if (this.has(key)) {
|
|
93
|
-
return
|
|
114
|
+
return deepClone(this.get(key));
|
|
94
115
|
}
|
|
95
|
-
const value =
|
|
116
|
+
const value = deepClone(getValue());
|
|
96
117
|
this.set(key, value);
|
|
97
118
|
return value;
|
|
98
119
|
}
|
|
@@ -100,27 +121,18 @@ class JsonFile extends events_1.default {
|
|
|
100
121
|
* Clears all the data in the JSON file.
|
|
101
122
|
*/
|
|
102
123
|
clear() {
|
|
103
|
-
const time = Date.now();
|
|
104
124
|
for (const key of Object.keys(tslib_1.__classPrivateFieldGet(this, _JsonFile_data, "f"))) {
|
|
105
|
-
this.
|
|
125
|
+
this.set(key, undefined);
|
|
106
126
|
}
|
|
107
|
-
tslib_1.__classPrivateFieldSet(this, _JsonFile_data, {}, "f");
|
|
108
|
-
fs_1.default.writeFileSync(this.filepath, '{}', 'utf8');
|
|
109
127
|
}
|
|
110
128
|
/**
|
|
111
129
|
* Assigns new data to the JSON file.
|
|
112
130
|
* @param data - The data to assign.
|
|
113
131
|
*/
|
|
114
132
|
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
|
-
}
|
|
133
|
+
for (const [key, value] of Object.entries(deepClone(data))) {
|
|
134
|
+
this.set(key, value);
|
|
121
135
|
}
|
|
122
|
-
Object.assign(tslib_1.__classPrivateFieldGet(this, _JsonFile_data, "f"), data);
|
|
123
|
-
this.saveFile();
|
|
124
136
|
}
|
|
125
137
|
/**
|
|
126
138
|
* 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;AACv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