wix-style-react
Version:
wix-style-react
42 lines (34 loc) • 918 B
JavaScript
const trimPath = (path, pathSeparator) => {
const escapedString = pathSeparator.replace(/[[\](){}?*+^$\\.|-]/g, '\\$&');
return path.replace(
new RegExp(`^[ ${escapedString}]+|[ ${escapedString}]+$`, 'g'),
'',
);
};
const explodePath = (path, pathSeparator) => {
const trimedPath = trimPath(path, pathSeparator);
if (trimedPath === '') {
return [];
}
return trimedPath.split(pathSeparator);
};
export default (
path,
baseUrlLink = '',
baseUrlValue = null,
pathSeparator = '/',
) => {
const pathArr = explodePath(path, pathSeparator);
const options = [];
let link = baseUrlLink;
const idOffset = baseUrlValue ? 1 : 0;
if (baseUrlValue) {
options.push({ id: 0, value: baseUrlValue, link });
}
pathArr.map((segment, id) => {
link += `/${segment}`;
options.push({ id: idOffset + id, value: segment, link });
return options;
});
return options;
};