-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_is.ts
71 lines (64 loc) · 1.79 KB
/
_is.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
// Copyright 2021-present the Equal authors. All rights reserved. MIT license.
import {
and,
isArray,
isFunction,
isJSONObject,
isObject,
isPrimitive,
pipe,
} from "./deps.ts";
import { instanceOf } from "./_utils.ts";
type IsFn = (val: unknown) => unknown;
const isFactory = (fn: IsFn) =>
<T, U extends T>(a: T, b: U): boolean => and(fn(a), fn(b));
const isObjectExcludeJSON = (val: unknown): val is Record<string, unknown> =>
and(isObject(val), () => !(isJSONObject(val)));
const _instanceOfFactory = pipe(instanceOf, isFactory);
const isBothArray = isFactory(isArray);
const isBothFunction = isFactory(isFunction);
const isBothJSONObject = isFactory(isJSONObject);
const isBothPrimitive = isFactory(isPrimitive);
const isBothObjectExcludeJSON = isFactory(isObjectExcludeJSON);
const isBothDate = _instanceOfFactory(Date);
const isBothRegExp = _instanceOfFactory(RegExp);
const isBothError = _instanceOfFactory(Error);
const isBothMap = _instanceOfFactory(Map);
const isBothSet = _instanceOfFactory(Set);
const isBothURL = _instanceOfFactory(URL);
const isBothArrayBuffer = _instanceOfFactory(ArrayBuffer);
const isBothURLSearchParams = _instanceOfFactory(URLSearchParams);
const isBothTypedArray = <T, U extends T>(a: T, b: U): boolean => {
return [
Int8Array,
Uint8Array,
Uint8ClampedArray,
Int16Array,
Uint16Array,
Int32Array,
Uint32Array,
Float32Array,
Float64Array,
BigInt64Array,
BigUint64Array,
]
.some((obj) => _instanceOfFactory(obj)(a, b));
};
export {
isBothArray,
isBothArrayBuffer,
isBothDate,
isBothError,
isBothFunction,
isBothJSONObject,
isBothMap,
isBothObjectExcludeJSON,
isBothPrimitive,
isBothRegExp,
isBothSet,
isBothTypedArray,
isBothURL,
isBothURLSearchParams,
isFactory,
};
export type { IsFn };