@n3okill/utils
Version:
Many javascript helpers
23 lines • 778 B
JavaScript
import * as NodePath from "path";
import { isUndefined } from "../type";
/**
* Transforms an array of strings into an object
* @param str Array of strings to be transformed
* @param separator Separator used in string, default to node "path" sep
* @returns
*/
export function stringsPathToObject(str, separator = NodePath.sep) {
const res = {};
str.forEach((s) => {
const parts = s.split(separator);
let o = res;
parts.forEach((p) => {
// eslint-disable-next-line security/detect-object-injection
o[p] = !isUndefined(o[p]) ? o[p] : {};
// eslint-disable-next-line security/detect-object-injection
o = o[p];
});
});
return res;
}
//# sourceMappingURL=stringsPathToObject.js.map