@ou-imdt/utils
Version:
Utility library for interactive media development
17 lines (14 loc) • 613 B
JavaScript
import propertyPathRegex from './regex/propertyPathRegex.js';
/**
* splits path into an array of keys, optionally returning (extended) propertyPathRegex groups
* @see {@link propertyPathRegex}
* @param {string} path - a property path
* @returns {array}
*/
export default function splitPropertyPath(path, withGroups = false) {
const parts = [...path.matchAll(new RegExp(propertyPathRegex, 'g'))];
return parts.map(({ groups }) => {
const { before = '', key, type = '', after = '' } = groups;
return (withGroups ? Object.assign(groups, { part: `${before}${key}${type}${after}` }) : key);
});
}