-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.js
154 lines (154 loc) · 4.19 KB
/
helpers.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
/// <reference types="node" />
"use strict";
/**
* Returns data as buffer.
*
* @param {any} data The input data.
* @param {string} [encoding] The custom encoding to use if 'data' is NOT a buffer.
*
* @return {Buffer} The output data.
*/
function asBuffer(data, encoding) {
let result = data;
if (!isNullOrUndefined(result)) {
if ('object' !== typeof result) {
// handle as string
encoding = normalizeString(encoding);
if (!encoding) {
encoding = 'utf8';
}
result = new Buffer(toStringSafe(result), encoding);
}
}
return result;
}
exports.asBuffer = asBuffer;
/**
* 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.
*/
function createSimplePromiseCompletedAction(resolve, reject) {
return (err, result) => {
if (err) {
if (reject) {
reject(err);
}
}
else {
if (resolve) {
resolve(result);
}
}
};
}
exports.createSimplePromiseCompletedAction = createSimplePromiseCompletedAction;
/**
* 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.
*/
function isEmptyString(val) {
return '' === toStringSafe(val).trim();
}
exports.isEmptyString = isEmptyString;
/**
* Checks if a value is (null) or (undefined).
*
* @param {any} val The value to check.
*
* @return {boolean} Is (null)/(undefined) or not.
*/
function isNullOrUndefined(val) {
return null === val ||
'undefined' === typeof val;
}
exports.isNullOrUndefined = isNullOrUndefined;
/**
* Normalizes a value as string so that is comparable.
*
* @param {any} val The value to convert.
* @param {(str: string) => string} [normalizer] The custom normalizer.
*
* @return {string} The normalized value.
*/
function normalizeString(val, normalizer) {
if (!normalizer) {
normalizer = (str) => str.toLowerCase().trim();
}
return normalizer(toStringSafe(val));
}
exports.normalizeString = normalizeString;
/**
* Reads a number of bytes from a socket.
*
* @param {net.Socket} socket The socket.
* @param {Number} [numberOfBytes] The amount of bytes to read.
*
* @return {Promise<Buffer>} The promise.
*/
function readSocket(socket, numberOfBytes) {
return new Promise((resolve, reject) => {
let completed = createSimplePromiseCompletedAction(resolve, reject);
try {
let buff = socket.read(numberOfBytes);
if (null === buff) {
socket.once('readable', function () {
readSocket(socket, numberOfBytes).then((b) => {
completed(null, b);
}, (err) => {
completed(err);
});
});
}
else {
completed(null, buff);
}
}
catch (e) {
completed(e);
}
});
}
exports.readSocket = readSocket;
/**
* Converts a value to a boolean.
*
* @param {any} val The value to convert.
* @param {any} defaultValue The value to return if 'val' is (null) or (undefined).
*
* @return {boolean} The converted value.
*/
function toBooleanSafe(val, defaultValue = false) {
if (isNullOrUndefined(val)) {
return defaultValue;
}
return !!val;
}
exports.toBooleanSafe = toBooleanSafe;
/**
* Converts a value to a string that is NOT (null) or (undefined).
*
* @param {any} str The input value.
* @param {any} defValue The default value.
*
* @return {string} The output value.
*/
function toStringSafe(str, defValue = '') {
if (isNullOrUndefined(str)) {
str = '';
}
str = '' + str;
if ('' === str) {
str = defValue;
}
return str;
}
exports.toStringSafe = toStringSafe;
//# sourceMappingURL=helpers.js.map