A small command-line utility for inspecting the contents of package.json files.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lib.js 528B

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. };