A small command-line utility for inspecting the contents of package.json files.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

12345678910111213141516171819202122
  1. const traversePath = (object, path) => {
  2. const [key, ...rest] = path;
  3. if (key === undefined) return object;
  4. if (object.hasOwnProperty(key)) {
  5. return traversePath(object[key], rest);
  6. }
  7. return `Property '${key}' does not exist`;
  8. };
  9. const pj = (contents, path) => {
  10. try {
  11. const object = JSON.parse(contents);
  12. return path.length === 0 ? object : traversePath(object, path);
  13. } catch (e) {
  14. return new Error('Not a valid JSON file');
  15. }
  16. };
  17. module.exports = {
  18. traversePath: traversePath,
  19. pj: pj,
  20. };