-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjoinPathData.index.js
206 lines (192 loc) · 5.44 KB
/
joinPathData.index.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import {
eachL,
equals2,
every,
flatL,
go,
isArray,
join,
mapL,
not,
tap,
toIter,
} from "fxjs/es";
import { IllegalArgumentError } from "../Errors/IllegalArgumentError.js";
/**
* @typedef {string} Command
* @description command is one of "M", "m", "L", "l", "H", "h", "V", "v", "C", "c", "S", "s", "Q", "q", "T", "t", "A", "a", "Z", "z".
*/
/**
* @typedef {number|Array<number>|Array<Array<number>>} Parameter
*/
const FN_PATH_TO_STRING_PATH_COMMAND_PARAMETERS = `$$toStringPathCommandParameters`;
const COMMAND_MLT = new Set(["M", "m", "L", "l", "T", "t"]);
const COMMAND_HV = new Set(["H", "h", "V", "v"]);
const COMMAND_C = new Set(["C", "c"]);
const COMMAND_SQ = new Set(["S", "s", "Q", "q"]);
const COMMAND_A = new Set(["A", "a"]);
const COMMAND_Z = new Set(["Z", "z"]);
const ARC_ARG_FLAG_SET = new Set([0, 1]);
/**
* Make SVG path data string of the input command + parameters object.
*
* @param {Object} path_command_parameters
* @param {Command} path_command_parameters.command
* @param {Parameter} path_command_parameters.parameters
* @returns {string} SVG path + parameters path data string
* @throws {IllegalArgumentError}
*/
export const $$toStringPathCommandParameters = ({ command, parameters }) => {
if (COMMAND_MLT.has(command)) {
return go(
parameters,
tap((l) => {
if (!isArray(l) || not(equals2(l.length, 2))) {
throw new IllegalArgumentError(
FN_PATH_TO_STRING_PATH_COMMAND_PARAMETERS,
`"parameters"`,
JSON.stringify({ command, parameters })
);
}
}),
eachL((a) => {
if (!Number.isFinite(a)) {
throw new IllegalArgumentError(
FN_PATH_TO_STRING_PATH_COMMAND_PARAMETERS,
`"parameters"`,
JSON.stringify({ command, parameters })
);
}
}),
join(" "),
(s) => `${command} ${s}`
);
}
if (COMMAND_HV.has(command)) {
if (!Number.isFinite(parameters)) {
throw new IllegalArgumentError(
FN_PATH_TO_STRING_PATH_COMMAND_PARAMETERS,
`"parameters"`,
JSON.stringify({ command, parameters })
);
}
return `${command} ${parameters}`;
}
const is_command_c = COMMAND_C.has(command);
const is_command_sq = COMMAND_SQ.has(command);
if (is_command_c || is_command_sq) {
return go(
parameters,
tap((l) => {
if (!isArray(l) || not(equals2(l.length, is_command_c ? 3 : 2))) {
throw new IllegalArgumentError(
FN_PATH_TO_STRING_PATH_COMMAND_PARAMETERS,
`"parameters"`,
JSON.stringify({ command, parameters })
);
}
}),
eachL((pair) => {
if (!isArray(pair) || not(equals2(pair.length, 2))) {
throw new IllegalArgumentError(
FN_PATH_TO_STRING_PATH_COMMAND_PARAMETERS,
`"parameters"`,
JSON.stringify({ command, parameters })
);
}
}),
flatL,
eachL((a) => {
if (!Number.isFinite(a)) {
throw new IllegalArgumentError(
FN_PATH_TO_STRING_PATH_COMMAND_PARAMETERS,
`"parameters"`,
JSON.stringify({ command, parameters })
);
}
}),
join(" "),
(s) => `${command} ${s}`
);
}
if (COMMAND_A.has(command)) {
if (!isArray(parameters) || not(equals2(parameters.length, 7))) {
throw new IllegalArgumentError(
FN_PATH_TO_STRING_PATH_COMMAND_PARAMETERS,
`"parameters"`,
JSON.stringify({ command, parameters })
);
}
const [
rx,
ry,
x_axis_rotation,
large_arc_flag,
sweep_flag,
x,
y,
] = parameters;
if (
not(every(Number.isFinite, [rx, ry, x_axis_rotation, x, y])) ||
not(
every((flag) => ARC_ARG_FLAG_SET.has(flag), [
large_arc_flag,
sweep_flag,
])
)
) {
throw new IllegalArgumentError(
FN_PATH_TO_STRING_PATH_COMMAND_PARAMETERS,
`"parameters"`,
JSON.stringify({ command, parameters })
);
}
return `${command} ${rx} ${ry} ${x_axis_rotation} ${large_arc_flag} ${sweep_flag} ${x} ${y}`;
}
if (COMMAND_Z.has(command)) {
if (!isArray(parameters) || parameters.length > 0) {
throw new IllegalArgumentError(
FN_PATH_TO_STRING_PATH_COMMAND_PARAMETERS,
`"parameters"`,
JSON.stringify({ command, parameters })
);
}
return command;
}
throw new IllegalArgumentError(
FN_PATH_TO_STRING_PATH_COMMAND_PARAMETERS,
`"command"`,
JSON.stringify({ command, parameters })
);
};
const FN_PATH_JOIN_PATH_DATA = `$$joinPathData`;
/**
* Make SVG path data string of the input list of command + parameters object.
* The return string is for "d" attributes of a SVG element.
*
* @param {Array<{command: Command, parameters: Parameter}>} [path_data=[]]
* @returns {string}
* @throws {IllegalArgumentError}
*/
export const $$joinPathData = (path_data = []) =>
go(
path_data,
toIter,
function* checkStartsWithM(iter) {
const { value, done } = iter.next();
if (done) {
return;
}
if (not(equals2(value.command.toLowerCase(), "m"))) {
throw new IllegalArgumentError(
FN_PATH_JOIN_PATH_DATA,
`"path_data"`,
`the first command is not one of "M" and "m".`
);
}
yield value;
yield* iter;
},
mapL($$toStringPathCommandParameters),
join(" ")
);