-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
183 lines (183 loc) · 6.79 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
"use strict";
/**
* A library for creating, updating, reading, and deleting key-value pairs for NodeJs
*
* Author: Kishan Nareshpal Jadav | https://kishannareshpal.github.io
*/
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs");
var pathjs = require("path");
var Pairs = /** @class */ (function () {
/**
* Initializes setttings.
* Optional configurations on where to save and what the PairsJS file should be called, are all available.
*
* @param dir_path the directory where the settings file should be stored. (@default ./ root dir)
* @param name the name of the settings file. (@default pairs.json)
*/
function Pairs(dir_path, name) {
if (dir_path === void 0) { dir_path = './'; }
if (name === void 0) { name = 'pairs.json'; }
// place it in the root of the project
if (!fs.existsSync(dir_path)) {
// create the folder if not already.
fs.mkdirSync(dir_path);
}
this.pairsFilePath = pathjs.join(dir_path, name);
try {
if (!fs.existsSync(this.pairsFilePath)) {
// settings file does not exist, create a new one.
fs.writeFile(this.pairsFilePath, '', function (err) {
if (err)
throw err;
});
}
// settings file exists.
}
catch (err) {
// error occured while checking the file, or by creating a new one. Inspect the log for details.
throw err;
}
}
/**
* Returns the settings as a json.
*/
Pairs.prototype.toJSON = function () {
try {
var data = fs.readFileSync(this.pairsFilePath, 'utf8');
var json = JSON.parse(data);
return json;
}
catch (error) {
// error reading the settings file.
return JSON.parse("{ }");
}
};
/**
* Add a new pair.
*
* @param key
* @param value
* @param allowsOverwrite @default true overwrites the value of an existing pair with the same key, if found, ofc.
* @throws if found a pair with the same key, and, allowsOverwrite is set to false.
*
*/
Pairs.prototype.add = function (key, value, allowsOverwrite) {
if (allowsOverwrite === void 0) { allowsOverwrite = true; }
var pairs = this.toJSON();
if (pairs[key]) {
// a key with the same name is found.
if (allowsOverwrite) {
// overwrite the value of the key with the new one.
this.updateValue(key, value);
}
else {
// should not overwrite the value, throw an error.
throw "Cannot add a pair with this key as it is already defined. [If you want to overwrite the value of this key, either set the allowsOverwrite option to true, or use pairjs.updateValue()]";
}
}
else {
pairs[key] = value;
fs.writeFileSync(this.pairsFilePath, JSON.stringify(pairs, null, 2));
}
};
/**
* Removes a pair, by the key.
*
* @param key the key of the pair you want to remove
* @returns true if succesfull, false if it could not be deleted.
* @throws if no pair matching the the provided key was found.
*/
Pairs.prototype.remove = function (key) {
var pairs = this.toJSON();
if (pairs[key]) {
// found a pair with the passed key.
// procceed on to removing.
var isDeleted = delete pairs[key];
fs.writeFileSync(this.pairsFilePath, JSON.stringify(pairs, null, 2));
return isDeleted; // success
}
else {
// did not find a pair with the provided key.
throw "No pair found with the key `" + key + "`. Could not remove.";
}
};
/**
* Updates the value of a pair, by the key.
*
* @param key the key of the pair you want the value to be updated.
* @param newValue the new value for that pair
* @param allowsAdding allows the creation of a new pair if no pair found with the provided key.
* @throws if no pair with the provided key is found and the allowsAdding option is set to false.
*/
Pairs.prototype.updateValue = function (key, newValue, allowsAdding) {
if (allowsAdding === void 0) { allowsAdding = true; }
var pairs = this.toJSON();
if (pairs[key]) {
// found a pair matching the provided key.
// update the value
pairs[key] = newValue;
fs.writeFileSync(this.pairsFilePath, JSON.stringify(pairs, null, 2));
return true;
}
else {
// no pair found with the provided key.
if (allowsAdding) {
// create a new pair.
this.add(key, newValue);
return false;
}
else {
// should not create a new pair if no pair found.
// just throw an error.
throw "No pair found with the key `" + key + "`. Could not update.";
}
}
};
/**
* Renames a key of an existing pair without modifying its value.
*
* @param key the key you want to rename
* @throws if no pair is found with the provided key.
*/
Pairs.prototype.renameKey = function (key, renamedKey) {
if (!this.has(key)) {
// the key to be updated, does not exist in your pairs.json file
throw "No pair found with the key `" + key + "`. Cannot rename.";
}
// found a pair matching the key. rename its key.
var settings = this.toJSON();
var updatedKeyJson = JSON.parse(JSON.stringify(settings).split('"' + key + '":').join('"' + renamedKey + '":'));
fs.writeFileSync(this.pairsFilePath, JSON.stringify(updatedKeyJson, null, 2));
};
/**
* Checks if a pair with the key exists.
*
* @param key the key of the pair.
* @returns true if found, otherwise false.
*/
Pairs.prototype.has = function (key) {
var settings = this.toJSON();
return settings[key] ? true : false;
};
/**
* Get the value of a pair by it's key.
*
* @param key the key of the pair of which you want the value.
* @returns the value of the pair found.
* the defaultValue (if provided), when no pair is found.
* undefined if no pair is found and no defaultValue is provided.
*/
Pairs.prototype.get = function (key, callbackFn, defaultValue) {
var settings = this.toJSON();
var value = settings[key];
if (callbackFn) {
return callbackFn(value);
}
else {
return value ? value : defaultValue;
}
};
return Pairs;
}());
exports.default = Pairs;