-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.ts
195 lines (175 loc) · 5.75 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
/// <reference types="node" />
// The MIT License (MIT)
//
// node-simple-socket (https://github.com/mkloubert/node-simple-socket)
// Copyright (c) Marcel Joachim Kloubert <marcel.kloubert@gmx.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
import * as Net from 'net';
/**
* 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 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.
*/
export function asBuffer(data: any, encoding?: string): Buffer {
let result: Buffer = 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;
}
/**
* 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 createSimplePromiseCompletedAction<TResult>(resolve: (value?: TResult | PromiseLike<TResult>) => void,
reject?: (reason: any) => void): SimpleCompletedAction<TResult> {
return (err?, result?) => {
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();
}
/**
* Checks if a value is (null) or (undefined).
*
* @param {any} val The value to check.
*
* @return {boolean} Is (null)/(undefined) or not.
*/
export function isNullOrUndefined(val: any): boolean {
return null === val ||
'undefined' === typeof val;
}
/**
* 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.
*/
export function normalizeString(val: any, normalizer?: (str: string) => string): string {
if (!normalizer) {
normalizer = (str) => str.toLowerCase().trim();
}
return normalizer(toStringSafe(val));
}
/**
* 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.
*/
export function readSocket(socket: Net.Socket, numberOfBytes?: number): Promise<Buffer> {
return new Promise<Buffer>((resolve, reject) => {
let completed = createSimplePromiseCompletedAction(resolve, reject);
try {
let buff: Buffer = 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);
}
});
}
/**
* 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.
*/
export function toBooleanSafe(val: any, defaultValue: any = false): boolean {
if (isNullOrUndefined(val)) {
return defaultValue;
}
return !!val;
}
/**
* 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.
*/
export function toStringSafe(str: any, defValue: any = ''): string {
if (isNullOrUndefined(str)) {
str = '';
}
str = '' + str;
if ('' === str) {
str = defValue;
}
return str;
}