-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathutil.ts
37 lines (33 loc) · 968 Bytes
/
util.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
export function bufFromString(str: string): Buffer {
const length = Buffer.byteLength(str);
const buffer = Buffer.allocUnsafe
? Buffer.allocUnsafe(length)
: new Buffer(length);
buffer.write(str);
return buffer;
}
export function emptyBuffer(): Buffer{
const buffer = Buffer.allocUnsafe
? Buffer.allocUnsafe(0)
: new Buffer(0);
return buffer;
}
export function filterArray(arr: any[], filter: number[]): any[] {
const rtn: any[] = [];
for (let i = 0; i < arr.length; i++) {
if (filter.indexOf(i) > -1) {
rtn.push(arr[i]);
}
}
return rtn;
}
export const trimLeft=String.prototype.trimLeft?function trimLeftNative(str:string){
return str.trimLeft();
}:function trimLeftRegExp(str:string){
return str.replace(/^\s+/, "");
}
export const trimRight=String.prototype.trimRight?function trimRightNative(str:string){
return str.trimRight();
}:function trimRightRegExp(str:string){
return str.replace(/\s+$/, "");
}