-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathutils.ts
75 lines (65 loc) · 2.16 KB
/
utils.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
'worklet';
/** Checks if the `window` global object is available. */
function isWindowAvailable(): boolean {
return typeof window !== 'undefined';
}
/** Checks if the `navigator` global object is available. */
function isNavigatorAvailable(): boolean {
return typeof navigator !== 'undefined';
}
const htmlEscapes = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'`': '`',
};
const reUnescapedHtml = /[&<>"'`]/g;
const reHasUnescapedHtml = RegExp(reUnescapedHtml.source);
/**
* Converts the characters "&", "<", ">", '"', and "'" in `string` to their
* corresponding HTML entities.
* Source: https://github.com/lodash/lodash/blob/main/src/escape.ts
*/
function escapeText(string: string): string {
return string && reHasUnescapedHtml.test(string) ? string.replace(reUnescapedHtml, (chr) => htmlEscapes[chr as keyof typeof htmlEscapes]) : string || '';
}
const htmlUnescapes = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
''': "'",
'`': '`',
' ': ' ',
};
const reEscapedHtml = /&(?:amp|lt|gt|quot|#(x27|x60|32));/g;
const reHasEscapedHtml = RegExp(reEscapedHtml.source);
/**
* The inverse of `escape`this method converts the HTML entities
* `&`, `<`, `>`, `"` and `'` in `string` to
* their corresponding characters.
* Source: https://github.com/lodash/lodash/blob/main/src/unescape.ts
* */
function unescapeText(string: string): string {
return string && reHasEscapedHtml.test(string) ? string.replace(reEscapedHtml, (entity) => htmlUnescapes[entity as keyof typeof htmlUnescapes] || "'") : string || '';
}
/**
* Checks if the given variable is a function
* @param {*} variableToCheck
* @returns {boolean}
*/
function isFunction(variableToCheck: unknown): boolean {
return variableToCheck instanceof Function;
}
/**
* Checks if the given variable is an object
* @param {*} obj
* @returns {boolean}
*/
function isObject(obj: unknown): boolean {
const type = typeof obj;
return type === 'function' || (!!obj && type === 'object');
}
export {isWindowAvailable, isNavigatorAvailable, escapeText, unescapeText, isFunction, isObject};