-
Notifications
You must be signed in to change notification settings - Fork 60
/
openScadParams.ts
102 lines (98 loc) · 2.59 KB
/
openScadParams.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
import { CadhubParams } from 'src/components/Customizer/customizerConverter'
interface OpenScadParamsBase {
caption: string
name: string
group: string
initial: number | string | boolean | number[]
type: 'string' | 'number' | 'boolean'
}
interface OpenScadNumberParam extends OpenScadParamsBase {
type: 'number'
initial: number | number[]
max?: number
min?: number
step?: number
options?: { name: string; value: number }[]
}
interface OpenScadStringParam extends OpenScadParamsBase {
type: 'string'
initial: string
maxLength?: number
options?: { name: string; value: string }[]
}
interface OpenScadBooleanParam extends OpenScadParamsBase {
type: 'boolean'
initial: boolean
}
export type OpenScadParams =
| OpenScadNumberParam
| OpenScadStringParam
| OpenScadBooleanParam
export function openScadToCadhubParams(
input: OpenScadParams[]
): CadhubParams[] {
return input
.map((param): CadhubParams => {
const common: { caption: string; name: string } = {
caption: param.caption,
name: param.name,
}
switch (param.type) {
case 'boolean':
return {
type: 'boolean',
input: 'default-boolean',
...common,
initial: param.initial,
}
case 'string':
if (!Array.isArray(param?.options)) {
return {
type: 'string',
input: 'default-string',
...common,
initial: param.initial,
maxLength: param.maxLength,
}
} else {
return {
type: 'string',
input: 'choice-string',
...common,
initial: param.initial,
options: param.options,
}
}
case 'number':
if (
!Array.isArray(param?.options) &&
!Array.isArray(param?.initial)
) {
return {
type: 'number',
input: 'default-number',
...common,
initial: param.initial,
min: param.min,
max: param.max,
step: param.step,
}
} else if (
Array.isArray(param?.options) &&
!Array.isArray(param?.initial)
) {
return {
type: 'number',
input: 'choice-number',
...common,
initial: param.initial,
options: param.options,
}
} // TODO else vector
break
default:
return
}
})
.filter((a) => a)
}