-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
/
Copy pathshared.utils.ts
51 lines (45 loc) · 1.67 KB
/
shared.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
export const isUndefined = (obj: any): obj is undefined =>
typeof obj === 'undefined';
export const isObject = (fn: any): fn is object =>
!isNil(fn) && typeof fn === 'object';
export const isPlainObject = (fn: any): fn is object => {
if (!isObject(fn)) {
return false;
}
const proto = Object.getPrototypeOf(fn);
if (proto === null) {
return true;
}
const ctor =
Object.prototype.hasOwnProperty.call(proto, 'constructor') &&
proto.constructor;
return (
typeof ctor === 'function' &&
ctor instanceof ctor &&
Function.prototype.toString.call(ctor) ===
Function.prototype.toString.call(Object)
);
};
export const addLeadingSlash = (path?: string): string =>
path && typeof path === 'string'
? path.charAt(0) !== '/'
? '/' + path
: path
: '';
export const normalizePath = (path?: string): string =>
path
? path.startsWith('/')
? ('/' + path.replace(/\/+$/, '')).replace(/\/+/g, '/')
: '/' + path.replace(/\/+$/, '')
: '/';
export const stripEndSlash = (path: string) =>
path[path.length - 1] === '/' ? path.slice(0, path.length - 1) : path;
export const isFunction = (val: any): val is Function =>
typeof val === 'function';
export const isString = (val: any): val is string => typeof val === 'string';
export const isNumber = (val: any): val is number => typeof val === 'number';
export const isConstructor = (val: any): boolean => val === 'constructor';
export const isNil = (val: any): val is null | undefined =>
isUndefined(val) || val === null;
export const isEmpty = (array: any): boolean => !(array && array.length > 0);
export const isSymbol = (val: any): val is symbol => typeof val === 'symbol';