@reldens/utils 0.23.0 → 0.25.0
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/index.js +1 -0
- package/lib/file-handler.js +48 -0
- package/lib/shortcuts.js +23 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -11,6 +11,7 @@ module.exports = {
|
|
|
11
11
|
EventsManagerSingleton: new EventsManager(),
|
|
12
12
|
ErrorManager: require('./lib/error-manager'),
|
|
13
13
|
InteractionArea: require('./lib/interaction-area'),
|
|
14
|
+
FileHandler: require('./lib/file-handler'),
|
|
14
15
|
Logger: require('./lib/logger'),
|
|
15
16
|
sc: require('./lib/shortcuts')
|
|
16
17
|
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* Reldens - FileHandler
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
const path = require('path');
|
|
9
|
+
const Logger = require('./logger');
|
|
10
|
+
|
|
11
|
+
class FileHandler
|
|
12
|
+
{
|
|
13
|
+
|
|
14
|
+
constructor()
|
|
15
|
+
{
|
|
16
|
+
this.encoding = 'utf8';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
joinPaths(...paths)
|
|
20
|
+
{
|
|
21
|
+
return path.join(...paths);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
copyFile(from, to, folder)
|
|
25
|
+
{
|
|
26
|
+
fs.copyFileSync(from, path.join(folder, to));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
createFolder(folderPath)
|
|
30
|
+
{
|
|
31
|
+
fs.mkdirSync(folderPath, { recursive: true });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async writeFile(fileName, content)
|
|
35
|
+
{
|
|
36
|
+
return fs.writeFile(fileName, content, this.encoding, (err) => {
|
|
37
|
+
if(err){
|
|
38
|
+
Logger.error('Error saving the file:', err);
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
Logger.info('The file has been saved! New file name: '+fileName);
|
|
42
|
+
return true;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
module.exports = FileHandler;
|
package/lib/shortcuts.js
CHANGED
|
@@ -128,6 +128,11 @@ class Shortcuts
|
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
+
deepJsonClone(obj)
|
|
132
|
+
{
|
|
133
|
+
return JSON.parse(JSON.stringify(obj));
|
|
134
|
+
}
|
|
135
|
+
|
|
131
136
|
get(obj, prop, defaultReturn)
|
|
132
137
|
{
|
|
133
138
|
return this.hasOwn(obj, prop) ? obj[prop] : defaultReturn;
|
|
@@ -259,11 +264,29 @@ class Shortcuts
|
|
|
259
264
|
return (new Date()).toISOString().slice(0, 19).replace('T', ' ');
|
|
260
265
|
}
|
|
261
266
|
|
|
267
|
+
getDateForFileName()
|
|
268
|
+
{
|
|
269
|
+
return (new Date()).toISOString().slice(0, 19).replace('T', '-').replace(/:/g, '-');
|
|
270
|
+
}
|
|
271
|
+
|
|
262
272
|
getTime()
|
|
263
273
|
{
|
|
264
274
|
return (new Date()).getTime();
|
|
265
275
|
}
|
|
266
276
|
|
|
277
|
+
roundToPrecision(number, precision = 4)
|
|
278
|
+
{
|
|
279
|
+
return Number(number.toFixed(precision));
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
randomValueFromArray(array)
|
|
283
|
+
{
|
|
284
|
+
if(!this.isArray(array) || 0 === array.length){
|
|
285
|
+
return null;
|
|
286
|
+
}
|
|
287
|
+
return array[Math.floor(Math.random() * array.length)];
|
|
288
|
+
}
|
|
289
|
+
|
|
267
290
|
randomInteger(min, max)
|
|
268
291
|
{
|
|
269
292
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|