-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
normalizeInputTypes.ts
35 lines (31 loc) · 1.05 KB
/
normalizeInputTypes.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
import mapValues from 'lodash/mapValues.js';
import type {
ArgTypes,
GlobalTypes,
InputType,
StrictArgTypes,
StrictGlobalTypes,
StrictInputType,
} from '@storybook/types';
const normalizeType = (type: InputType['type']): StrictInputType['type'] => {
return typeof type === 'string' ? { name: type } : type;
};
const normalizeControl = (control: InputType['control']): StrictInputType['control'] =>
typeof control === 'string' ? { type: control } : control;
export const normalizeInputType = (inputType: InputType, key: string): StrictInputType => {
const { type, control, ...rest } = inputType;
const normalized: StrictInputType = {
name: key,
...rest,
};
if (type) normalized.type = normalizeType(type);
if (control) {
normalized.control = normalizeControl(control);
} else if (control === false) {
normalized.control = { disable: true };
}
return normalized;
};
export const normalizeInputTypes = (
inputTypes: ArgTypes | GlobalTypes
): StrictArgTypes | StrictGlobalTypes => mapValues(inputTypes, normalizeInputType);