-
Notifications
You must be signed in to change notification settings - Fork 0
/
carbon.misc.js
86 lines (86 loc) · 2.39 KB
/
carbon.misc.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
'use strict';
const hasOwn = Object.prototype.hasOwnProperty,
fs = require('./carbon.fs'),
path = require('path'),
fibers = require('./carbon.fibers');
//JSON:
function copyJSON(json) {
return(JSON.parse(JSON.stringify(json)));
}
function mergeJSON(json, overrides, /*optional*/concatArrays) {
if (Array.isArray(overrides)) {
if (concatArrays && Array.isArray(json)) {
return(copyJSON(json).concat(copyJSON(overrides)));
}
} else if (json && overrides && (json instanceof Object) && (overrides instanceof Object)) {
let result = {};
for (let key in json) {
if (hasOwn.call(json, key) && !hasOwn.call(overrides, key)) {
result[key] = json[key];
}
}
for (let key in overrides) {
if (hasOwn.call(overrides, key)) {
result[key] = mergeJSON(json[key], overrides[key], concatArrays);
}
}
return(result);
}
return(copyJSON(overrides));
}
function loadJSON(fullpath, displayWarnings, evil, callback) {
fullpath = path.resolve(fullpath);
fs.readFile(fullpath)
.on('done', function (error, content) {
if (!content) {
error = new Error(' No Such JSON File.');
}
(error && displayWarnings && console.warn('\nERROR: Unable to load file: ', fullpath, '\n' + error.stack + '\n'));
if (!error) {
try {
return(callback(null, JSON.parse(content)));
} catch (error) {
(displayWarnings && console.warn('WARNING: JSON' + error + ' ', fullpath));
if (evil) {
try {
return(callback(null, eval('(' + ('' + (content||'')).trim() + ')')));
} catch (error) {
(displayWarnings && console.warn('\nERROR: Unable to load file: ', fullpath, '\n' + error.stack + '\n'));
}
}
}
}
return(callback(null, null));
})
.run();
}
//RegExp:
function escapeRegExp(string) {
return('' + (string || '')).replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
}
//Format:
function formatSeconds(ms) {
let sec = ('' + (ms * 0.001)).split('.');
sec[1] = ((sec[1]||'')+'000').substring(0, 3);
return(sec.join('.'));
}
function formatPercent(progress, total) {
return(('' + ((progress / total) * 100)).split('.')[0] + '%');
}
//exports:
module.exports = {
JSON: {
load: function (fullpath, displayWarnings, evil) {
return(new fibers(null, loadJSON, [fullpath, displayWarnings, evil]));
},
copy: copyJSON,
merge: mergeJSON,
},
RegExp: {
escape: escapeRegExp,
},
format: {
seconds: formatSeconds,
percent: formatPercent,
},
};