@zodash/to-path
Version:
Transform path string to path array
36 lines (35 loc) • 768 B
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toPath = void 0;
/**
* Transform path string to path array
*
* @param pathString path string
* @returns path array
*
* @example
*
* toPath('a.b.c')
* // => ['a', 'b', 'c']
*
* toPath('a[0].b.c')
* // => ['a', '0', 'b', 'c']
*
* toPath('a.0.b.c')
* // => ['a', '0', 'b', 'c']
*
* toPath('a[].b.c')
* // => ['a', '[]', 'b', 'c']
*
* toPath('SERVICE_CONFIG_ID', '_')
* // => ['SERVICE', 'CONFIG', 'ID']
*/
function toPath(pathString, separator = '.') {
if (!pathString)
return [];
return pathString
.replace(/\[(\w+)\]/g, `${separator}$1`)
.replace(/(\[\])/g, `${separator}$1`)
.split(separator);
}
exports.toPath = toPath;