-
Notifications
You must be signed in to change notification settings - Fork 0
/
typeGuards.ts
42 lines (35 loc) · 1.19 KB
/
typeGuards.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
import type { IsoString } from '~/types/utils';
import type { Tier, TierWithoutNotRatable } from '~/types/randomDefense';
import { RatedTier } from '~/types/tierHider';
export const isObject = (data: unknown): data is object => {
return typeof data === 'object' && data !== null;
};
export const isNumericObject = (
data: unknown,
): data is Record<number, unknown> => {
return (
isObject(data) && Object.keys(data).every((key) => !isNaN(Number(key)))
);
};
export const isNumericArray = (data: unknown): data is number[] => {
return (
Array.isArray(data) && data.every((value) => typeof value === 'number')
);
};
export const isIsoString = (data: unknown): data is IsoString => {
return (
typeof data === 'string' &&
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/.test(data)
);
};
export const isTier = (data: unknown): data is Tier => {
return typeof data === 'number' && data % 1 === 0 && data >= 0 && data <= 31;
};
export const isTierWithoutNotRatable = (
data: unknown,
): data is TierWithoutNotRatable => {
return isTier(data) && data !== 31;
};
export const isRatedTier = (data: unknown): data is RatedTier => {
return isTier(data) && data !== 0 && data !== 31;
};