-
Notifications
You must be signed in to change notification settings - Fork 4
/
label.ts
139 lines (126 loc) · 2.92 KB
/
label.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { gray, underline } from "chalk";
import { Field, FieldTextTransformer, IField, IMessage } from "../message";
import { padEnd } from "../utils";
import stripColor = require("strip-color");
/**
* Label options
*/
export interface ILabelOptionsValue {
name?: string;
transformer?: FieldTextTransformer;
}
/**
* Label interface
*/
export interface ILabelOptions {
label?: ILabelOptionsValue;
}
/**
* Label configs
*/
export interface ILabels {
[index: string]: ILabelOptionsValue;
}
/**
* Normalized label
*/
interface INormalizedLabel {
name: string;
length: number;
transformed: string;
type: string;
}
/**
* Field mapping
*/
interface IFields {
[name: string]: IField;
}
/**
* Field config
*/
export interface ILabelConfig {
minLength?: number;
transformer?: FieldTextTransformer;
name?: string;
}
/**
* Label field
* @param config Field configuration
*/
export default function ({
minLength: defaultLength = 0,
transformer: defaultTransformer = (text) => underline(gray(text)),
name: defaultName = "label",
}: ILabelConfig = {}): Field {
const labels: ILabels = {};
let fields: IFields = {};
const isLabelMissing = (id: string) => {
return !(id in labels);
};
const registerLabel = (
id: string,
{ name, transformer }: ILabelOptionsValue = {}
) => {
labels[id] = normalizeLabel({
name: name || id,
transformer,
});
refreshFields();
};
const refreshFields = () => {
let highestLength = defaultLength;
fields = Object.entries(labels)
.map(
([type, { name, transformer }]): INormalizedLabel => {
const transformed = (transformer as FieldTextTransformer)(
name as string
);
const length = stripColor(transformed).length;
if (length > highestLength) {
highestLength = length;
}
return {
length,
name: name as string,
transformed,
type,
};
}
)
.map(
({ type, length, transformed, name }): IFields => {
return {
[type]: {
data: name,
name: defaultName,
text: padEnd(
transformed,
transformed.length + (highestLength - length)
),
},
};
}
)
.reduce((prev, curr) => Object.assign(prev, curr), {});
};
const normalizeLabel = ({
name,
transformer = defaultTransformer,
}: ILabelOptionsValue): ILabelOptionsValue => ({
name,
transformer,
});
return function handler(message: IMessage): IField {
const id = message.type.id || "";
if (isLabelMissing(id)) {
if (message.type.options) {
const options = message.type.options as ILabelOptions;
registerLabel(id, options.label as ILabelOptionsValue);
} else {
registerLabel(id, {});
}
}
return fields[id];
};
}