forked from simplify-framework/simplify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.js
197 lines (174 loc) · 5.88 KB
/
utilities.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env node
'use strict';
const { printTable, Table } = require('console-table-printer')
const http = require('http')
const crypto = require('crypto')
const fs = require('fs')
const { promisify } = require('util');
const { resolve } = require('path');
const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);
String.prototype.truncate = function (num, chars) {
if (this.length <= num) { return this }
return (typeof chars === 'undefined' ? '...' : chars) + this.slice(this.length - num)
}
String.prototype.toCamelCase = function () {
return this.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
return index === 0 ? word.toLowerCase() : word.toUpperCase();
}).replace(/\s+/g, '').split(' ').join('').split('-').join('');
}
String.prototype.truncateLeft = function (num, chars) {
if (this.length <= num) { return this }
return (typeof chars === 'undefined' ? '...' : chars) + this.slice(this.length - num)
}
String.prototype.truncateRight = function (num, chars) {
if (this.length <= num) { return this }
return this.slice(0, this.length - num) + (typeof chars === 'undefined' ? '...' : chars)
}
const formatBytesToKBMB = function (bytes, decimals = 2) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
const formatTimeSinceAgo = function (date) {
if (typeof date !== 'object') {
date = new Date(date);
}
var seconds = Math.floor((new Date() - date) / 1000);
var intervalType;
var interval = Math.floor(seconds / 31536000);
if (interval >= 1) {
intervalType = 'year';
} else {
interval = Math.floor(seconds / 2592000);
if (interval >= 1) {
intervalType = 'month';
} else {
interval = Math.floor(seconds / 86400);
if (interval >= 1) {
intervalType = 'day';
} else {
interval = Math.floor(seconds / 3600);
if (interval >= 1) {
intervalType = "hour";
} else {
interval = Math.floor(seconds / 60);
if (interval >= 1) {
intervalType = "minute";
} else {
interval = seconds;
intervalType = "second";
}
}
}
}
}
if (interval > 1 || interval === 0) {
intervalType += 's';
}
return interval + ' ' + intervalType + ' ago';
}
const toDateStringFile = function () {
var m = new Date();
return m.getFullYear() +
("0" + (m.getMonth() + 1)).slice(-2) +
("0" + m.getDate()).slice(-2) + "T" +
("0" + m.getHours()).slice(-2) +
("0" + m.getMinutes()).slice(-2) +
("0" + m.getSeconds()).slice(-2)
}
const getDateToday = function () {
var m = new Date();
return m.getFullYear() + '-' +
("0" + (m.getMonth() + 1)).slice(-2) + '-' +
("0" + m.getDate()).slice(-2)
}
const getTimeMoment = function () {
var m = new Date();
return ("0" + m.getHours()).slice(-2) + ':' +
("0" + m.getMinutes()).slice(-2) + ':' +
("0" + m.getSeconds()).slice(-2)
}
const getOutputKeyValue = function (outputs, propKey) {
var result = null
outputs.some(function (output) {
if (output.OutputKey === propKey) {
result = output.OutputValue
return true
}
return false
})
return result
}
const getSha256FileInHex = function (filePath) {
const data = fs.readFileSync(filePath)
return crypto.createHash('sha256').update(data).digest('hex')
}
const getSha256FileInBase64 = function (filePath) {
const data = fs.readFileSync(filePath)
return crypto.createHash('sha256').update(data).digest('base64')
}
const getSha256InHex = function (data) {
return crypto.createHash('sha256').update(data).digest('hex')
}
const getSha256InBase64 = function (data) {
return crypto.createHash('sha256').update(data).digest('base64')
}
const downloadFileFromUrl = function (url, dest) {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(dest, { flags: "wx" })
const request = http.get(url, response => {
if (response.statusCode === 200) {
response.pipe(file)
} else {
file.close()
fs.unlink(dest, () => { })
reject({ message: `Server responded with ${response.statusCode}: ${response.statusMessage}` });
}
})
request.on("error", err => {
file.close()
fs.unlink(dest, () => { })
reject(err)
})
file.on("finish", () => {
resolve()
})
file.on("error", err => {
file.close()
if (err.code === "EEXIST") {
reject({ message: "File already exists" });
} else {
fs.unlink(dest, () => { })
reject(err)
}
})
})
}
async function getFilesInDirectory(dir) {
const subdirs = await readdir(dir + "/");
const files = await Promise.all(subdirs.map(async (subdir) => {
const res = resolve(dir, subdir);
return (await stat(res)).isDirectory() ? getFilesInDirectory(res) : res;
}));
return files.reduce((a, f) => a.concat(f), []);
}
module.exports = {
getOutputKeyValue,
getSha256InHex,
getSha256FileInHex,
getSha256InBase64,
getSha256FileInBase64,
downloadFileFromUrl,
printTableWithJSON: printTable,
PrintTable: Table,
toDateStringFile,
getDateToday,
getTimeMoment,
formatBytesToKBMB,
formatTimeSinceAgo,
getFilesInDirectory
}