-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.ts
405 lines (347 loc) · 9.38 KB
/
helpers.ts
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/**
* This file is part of the node-share-folder distribution.
* Copyright (c) Marcel Joachim Kloubert.
*
* node-share-folder is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, version 3.
*
* node-share-folder is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import * as _ from 'lodash';
import * as Moment from 'moment';
import * as OS from 'os';
import * as Path from 'path';
import * as Stream from 'stream';
/**
* Describes a simple 'completed' action.
*
* @param {any} err The occurred error.
* @param {TResult} [result] The result.
*/
export type SimpleCompletedAction<TResult> = (err: any, result?: TResult) => void;
/**
* Returns a value as array.
*
* @param {T|T[]} val The value.
* @param {boolean} [removeEmpty] Remove items that are (null) / (undefined) or not.
*
* @return {T[]} The value as (new) array.
*/
export function asArray<T>(val: T | T[], removeEmpty = true): T[] {
removeEmpty = toBooleanSafe(removeEmpty, true);
return (_.isArray(val) ? val : [ val ]).filter(i => {
if (removeEmpty) {
return !_.isNil(i);
}
return true;
});
}
/**
* Returns a value as UTC Moment instance.
*
* @param {any} val The input value.
*
* @return {Moment.Moment} The output value.
*/
export function asUTC(val: any): Moment.Moment {
let utcTime: Moment.Moment;
if (!_.isNil(val)) {
if (Moment.isMoment(val)) {
utcTime = val;
} else if (Moment.isDate(val)) {
utcTime = Moment( val );
} else {
utcTime = Moment( toStringSafe(val) );
}
}
if (utcTime) {
if (!utcTime.isUTC()) {
utcTime = utcTime.utc();
}
}
return utcTime;
}
/**
* Clones an object / value deep.
*
* @param {T} val The value / object to clone.
*
* @return {T} The cloned value / object.
*/
export function cloneObject<T>(val: T): T {
if (!val) {
return val;
}
return JSON.parse(
JSON.stringify(val)
);
}
/**
* Creates a simple 'completed' callback for a promise.
*
* @param {Function} resolve The 'succeeded' callback.
* @param {Function} reject The 'error' callback.
*
* @return {SimpleCompletedAction<TResult>} The created action.
*/
export function createCompletedAction<TResult = any>(resolve: (value?: TResult | PromiseLike<TResult>) => void,
reject?: (reason: any) => void): SimpleCompletedAction<TResult> {
let completedInvoked = false;
return (err, result?) => {
if (completedInvoked) {
return;
}
completedInvoked = true;
if (err) {
if (reject) {
reject(err);
}
} else {
if (resolve) {
resolve(result);
}
}
};
}
/**
* Checks if the string representation of a value is empty
* or contains whitespaces only.
*
* @param {any} val The value to check.
*
* @return {boolean} Is empty or not.
*/
export function isEmptyString(val: any): boolean {
return '' === toStringSafe(val).trim();
}
/**
* Normalizes a path.
*
* @param {string} p The path to normalize.
*
* @return {string} The normalized path.
*/
export function normalizePath(p: string) {
p = toStringSafe(p);
if ('.' === p.trim()) {
p = '';
}
p = p.split( Path.sep )
.join('/');
while (p.trim().startsWith('/')) {
p = p.substr(p.indexOf('/') + 1);
}
while (p.trim().endsWith('/')) {
p = p.substr(0, p.lastIndexOf('/'));
}
if (!p.trim().startsWith('/')) {
p = '/' + p;
}
return p;
}
/**
* Normalizes a value as string so that is comparable.
*
* @param {any} val The value to convert.
*
* @return {string} The normalized value.
*/
export function normalizeString(val: any) {
return toStringSafe(val).toLowerCase().trim();
}
/**
* Reads the content of a stream.
*
* @param {Stream.Readable} stream The stream.
* @param {string} [enc] The custom (string) encoding to use.
*
* @returns {Promise<Buffer>} The promise with the content.
*/
export function readAll(stream: Stream.Readable, enc?: string): Promise<Buffer> {
enc = normalizeString(enc);
if ('' === enc) {
enc = undefined;
}
return new Promise<Buffer>((resolve, reject) => {
let buff: Buffer;
let dataListener: (chunk: Buffer | string) => void;
let endListener: () => void;
let errorListener: (err: any) => void;
let completedInvoked = false;
const COMPLETED = (err: any) => {
if (completedInvoked) {
return;
}
completedInvoked = true;
tryRemoveListener(stream, 'data', dataListener);
tryRemoveListener(stream, 'end', endListener);
tryRemoveListener(stream, 'error', errorListener);
if (err) {
reject(err);
} else {
resolve(buff);
}
};
if (_.isNil(stream)) {
buff = <any>stream;
COMPLETED(null);
return;
}
errorListener = (err: any) => {
if (err) {
COMPLETED(err);
}
};
dataListener = (chunk: Buffer | string) => {
try {
if (!chunk || chunk.length < 1) {
return;
}
if (_.isString(chunk)) {
chunk = new Buffer(chunk, enc);
}
buff = Buffer.concat([ buff, chunk ]);
} catch (e) {
COMPLETED(e);
}
};
endListener = () => {
COMPLETED(null);
};
try {
stream.on('error', errorListener);
buff = Buffer.alloc(0);
stream.once('end', endListener);
stream.on('data', dataListener);
} catch (e) {
COMPLETED(e);
}
});
}
/**
* Returns a value as boolean, which is not (null) and (undefined).
*
* @param {any} val The value to convert.
* @param {boolean} [defaultVal] The custom default value if 'val' is (null) or (undefined).
*
* @return {boolean} 'val' as boolean.
*/
export function toBooleanSafe(val: any, defaultVal = false): boolean {
if (_.isBoolean(val)) {
return val;
}
if (_.isNil(val)) {
return !!defaultVal;
}
return !!val;
}
/**
* Returns a value as string, which is not (null) and (undefined).
*
* @param {any} val The value to convert.
* @param {string} [defaultVal] The custom default value if 'val' is (null) or (undefined).
*
* @return {string} 'val' as string.
*/
export function toStringSafe(val: any, defaultVal = '') {
if (_.isString(val)) {
return val;
}
if (_.isNil(val)) {
return '' + defaultVal;
}
try {
if (val instanceof Error) {
return '' + val.message;
}
if (_.isFunction(val['toString'])) {
return '' + val.toString();
}
if (_.isObject(val)) {
return JSON.stringify(val);
}
} catch { }
return '' + val;
}
/**
* Tries to remove a listener from an event emitter.
*
* @param {NodeJS.EventEmitter} obj The emitter.
* @param {string|symbol} ev The event.
* @param {Function} listener The listener.
*
* @return {boolean} Operation was successfull or not.
*/
export function tryRemoveListener(
obj: NodeJS.EventEmitter,
ev: string | symbol, listener: Function,
) {
try {
if (obj && obj.removeListener) {
obj.removeListener(ev, listener);
}
return true;
} catch {
return false;
}
}
/**
* Writes a message to a stream.
*
* @param {any} msg The message to write.
* @param {NodeJS.WritableStream} [stream] The custom output stream. Default: stdout
*/
export function write(msg: any, stream?: NodeJS.WritableStream) {
if (arguments.length < 2) {
stream = process.stdout;
}
if (!Buffer.isBuffer(msg)) {
msg = toStringSafe(msg);
}
if (msg.length > 0) {
stream.write(msg);
}
}
/**
* Writes a message to stderr.
*
* @param {any} msg The message to write.
*/
export function write_err(msg: any) {
write(msg, process.stderr);
}
/**
* Writes an optional message to stderr and appends a new line.
*
* @param {any} [msg] The message to write.
*/
export function write_err_ln(msg?: any) {
write_ln(msg, process.stderr);
}
/**
* Writes an optional message to a stream and appends a new line.
*
* @param {any} [msg] The message to write.
* @param {NodeJS.WritableStream} [stream] The custom output stream. Default: stdout
*/
export function write_ln(msg?: any, stream?: NodeJS.WritableStream) {
if (arguments.length < 2) {
stream = process.stdout;
}
if (Buffer.isBuffer(msg)) {
msg = Buffer.concat([
msg,
new Buffer(OS.EOL, 'binary')
]);
} else {
msg = toStringSafe(msg) + OS.EOL;
}
write(msg, stream);
}