getPropertyByPath.js 938 B

12345678910111213141516171819202122232425262728
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getPropertyByPath = getPropertyByPath;
  6. // Resolves property names or property paths defined with period-delimited
  7. // strings or arrays of strings. Property names that are found on the source
  8. // object are used directly (even if they include a period).
  9. // Nested property names that include periods, within a path, are only
  10. // understood in array paths.
  11. function getPropertyByPath(source, path) {
  12. if (typeof path === 'string' && Object.prototype.hasOwnProperty.call(source, path)) {
  13. return source[path];
  14. }
  15. const parsedPath = typeof path === 'string' ? path.split('.') : path; // eslint-disable-next-line @typescript-eslint/no-explicit-any
  16. return parsedPath.reduce((previous, key) => {
  17. if (previous === undefined) {
  18. return previous;
  19. }
  20. return previous[key];
  21. }, source);
  22. }
  23. //# sourceMappingURL=getPropertyByPath.js.map