-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeadingNode.ts
79 lines (62 loc) · 2.72 KB
/
HeadingNode.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
76
77
78
79
import { ComposedElement } from '../ComposedElement';
import type { Node } from '../Node';
export interface HeadingNode<Child extends Node> extends ComposedElement<`${HeadingNode.Type}`, Child> {
role?: `${HeadingNode.Role}`;
}
export namespace HeadingNode {
export enum Type {
HEADING_ONE = 'heading-one',
HEADING_TWO = 'heading-two',
}
export enum Role {
TITLE = 'title',
SUBTITLE = 'subtitle',
}
export function isHeadingNode<Heading extends HeadingNode<Child>, Child extends Node>(value: any): value is Heading;
export function isHeadingNode<Heading extends HeadingNode<Child>, Child extends Node>(
value: any,
type: Type,
): value is Heading;
export function isHeadingNode(value: any, type?: `${Type}`) {
if (type === undefined) {
return (
ComposedElement.isComposedElement(value) &&
(value.type === Type.HEADING_ONE || value.type === Type.HEADING_TWO)
);
}
return ComposedElement.isComposedElement(value, type);
}
export function isTitleHeadingNode<Heading extends HeadingNode<Child>, Child extends Node>(
value: any,
): value is Heading & { role: HeadingNode.Role.TITLE };
export function isTitleHeadingNode(value: any) {
return isHeadingNode(value) && value.role === HeadingNode.Role.TITLE;
}
export function isSubtitleHeadingNode<Heading extends HeadingNode<Child>, Child extends Node>(
value: any,
): value is Heading & { role: HeadingNode.Role.SUBTITLE };
export function isSubtitleHeadingNode(value: any) {
return isHeadingNode(value) && value.role === HeadingNode.Role.SUBTITLE;
}
export function validateHeadingNode<Heading extends HeadingNode<Child>, Child extends Node>(
value: any,
validateChildNode: (node: any) => Child | null,
): Heading | null;
export function validateHeadingNode<Heading extends HeadingNode<Child>, Child extends Node>(
value: any,
type: `${Type}`,
validateChildNode: (node: any) => Child | null,
): Heading | null;
export function validateHeadingNode(value: any, ...params: [ValidateFn] | [`${Type}`, ValidateFn]) {
if (params.length === 2) {
const [type, validateChildNode] = params;
const isValid = ComposedElement.validateComposedElement(value, type, validateChildNode);
return isValid ? value : null;
}
const [validateChildNode] = params;
const isValid =
isHeadingNode(value) && ComposedElement.validateComposedElement(value, value.type, validateChildNode);
return isValid ? value : null;
}
}
type ValidateFn = (node: any) => Node | null;