/
types.ts
69 lines (51 loc) 路 1.59 KB
/
types.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
import { typeGuard } from "https://raw.githubusercontent.com/garronej/tsafe/v0.10.1/deno_dist/typeGuard.ts";
import { getPrototypeChain } from "./getPrototypeChain.ts";
type SetLike<T> = {
values: () => Iterable<T>;
};
export namespace SetLike {
export function match<T>(set: Object): set is SetLike<T> {
return (
typeGuard<SetLike<T>>(set, true) &&
typeof set.values === "function" &&
getPrototypeChain.isMatched(set, /Set/)
);
}
}
export type MapLike<T, U> = {
keys: () => Iterable<T>;
get(key: T): U | undefined;
};
export namespace MapLike {
export function match<T, U>(map: Object): map is MapLike<T, U> {
return (
typeGuard<MapLike<T, U>>(map, true) &&
typeof map.keys === "function" &&
typeof map.get === "function" &&
getPrototypeChain.isMatched(map, /Map/)
);
}
}
export namespace ArrayLike {
export function match<T>(arr: Object): arr is ArrayLike<T> {
return (
typeGuard<ArrayLike<T>>(arr, true) &&
typeof arr.length === "number" &&
arr.length !== 0 ?
(`${arr.length - 1}` in arr) :
getPrototypeChain.isMatched(arr, /Array/)
);
}
}
export type DateLike = {
getTime: ()=> number;
}
export namespace DateLike {
export function match(date: Object): date is DateLike {
return (
typeGuard<DateLike>(date, true) &&
typeof date.getTime === "function" &&
getPrototypeChain.isMatched(date, /Date/)
)
}
}