/
type.ts
88 lines (78 loc) · 2 KB
/
type.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
/**
* Returns `true` if incoming data is string, otherwise `false`
* @param v Tested value
*/
export function isString(v: any): v is string {
return typeof (v) === "string";
}
/**
* Returns `true` if incoming data is number, otherwise `false`
* @param v Tested value
*/
export function isNumber(v: any): v is number {
return typeof (v) === "number";
}
/**
* Returns `true` if incoming data is boolean, otherwise `false`
* @param v Tested value
*/
export function isBoolean(v: any): v is boolean {
return typeof (v) === "boolean";
}
/**
* Returns `true` if incoming data is undefined, otherwise `false`
* @param v Tested value
*/
export function isUndefined(v: any): v is undefined {
return typeof (v) === "undefined";
}
/**
* Returns `true` if incoming data is null, otherwise `false`
* @param v Tested value
*/
export function isNull(v: any): v is null {
return v === null;
}
/**
* Returns `true` if incoming data is empty (undefined or null), otherwise `false`
* @param v Tested value
*/
export function isEmpty(v: any): v is null | undefined {
return isUndefined(v) || isNull(v);
}
/**
* Returns `true` if incoming data is Function, otherwise `false`
* @param v Tested value
*/
export function isFunction(v: any): v is (...args: any[]) => any {
return typeof (v) === "function";
}
/**
* Returns `true` if incoming data is Object, otherwise `false`
* @param v Tested value
*/
export function isObject(v: any): v is object {
return typeof (v) === "object";
}
/**
* Returns `true` if incoming data is Array, otherwise `false`
* @param v Tested value
*/
export function isArray(v: any): v is any[] {
return Array.isArray(v);
}
/**
* Returns `true` if bit is enabled in flag value, otherwise `false`
* @param v Flag
* @param fv Bit value
*/
export function isFlag(v: number, fv: number) {
return (v & fv) === fv;
}
/**
* Removes padded ends from the string
* @param text Formatted string
*/
export function removePadding(text: string) {
return text.replace(/\0.*/g, "").trim();
}