-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsplitPattern.js
More file actions
32 lines (25 loc) · 1.03 KB
/
Copy pathsplitPattern.js
File metadata and controls
32 lines (25 loc) · 1.03 KB
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
const splitPattern = (pattern, withSpaces) => {
const initialSpace = pattern.split('x')[0];
const stepsWithSpaces = pattern.split('x');
if (initialSpace.length === 0) stepsWithSpaces.shift();
const counter = initialSpace.length === 0 ? 0 : 1;
for (let i = counter; i < stepsWithSpaces.length; i++) stepsWithSpaces[i] = 'x' + stepsWithSpaces[i];
const noEmptySteps = (steps) => steps.filter((step) => step.length > 0);
if (withSpaces) return noEmptySteps(stepsWithSpaces);
const stepsAndSpaces = [];
stepsWithSpaces.forEach((step) => {
const spaceIndex = step.indexOf('-');
if (spaceIndex !== -1) {
const splitHappens = [step.slice(0, spaceIndex), step.slice(spaceIndex)];
stepsAndSpaces.push(...splitHappens);
} else {
stepsAndSpaces.push(step);
}
});
const stepsAndSpacesSeparated = [];
noEmptySteps(stepsAndSpaces)
.map((step) => step.split(/(?=-)/g))
.forEach((step) => stepsAndSpacesSeparated.push(...step));
return stepsAndSpacesSeparated;
};
module.exports = { splitPattern };