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.

110 lines
3.0 KiB

4 years ago
  1. object-path
  2. ===========
  3. Access deep properties using a path
  4. [![NPM version](https://badge.fury.io/js/object-path.png)](http://badge.fury.io/js/object-path)
  5. [![Build Status](https://travis-ci.org/mariocasciaro/object-path.png)](https://travis-ci.org/mariocasciaro/object-path)
  6. [![Coverage Status](https://coveralls.io/repos/mariocasciaro/object-path/badge.png)](https://coveralls.io/r/mariocasciaro/object-path)
  7. [![devDependency Status](https://david-dm.org/mariocasciaro/object-path/dev-status.svg)](https://david-dm.org/mariocasciaro/object-path#info=devDependencies)
  8. ![Downloads](http://img.shields.io/npm/dm/object-path.svg)
  9. ## Install
  10. ### Node.js
  11. ```
  12. npm install object-path
  13. ```
  14. ### Browser
  15. ```
  16. bower install object-path
  17. ```
  18. ### Typescript typings
  19. ```
  20. tsd query object-path --action install --save
  21. ```
  22. ## Usage
  23. ```javascript
  24. var obj = {
  25. a: {
  26. b: "d",
  27. c: ["e", "f"]
  28. }
  29. };
  30. var objectPath = require("object-path");
  31. //get deep property
  32. objectPath.get(obj, "a.b"); //returns "d"
  33. //get the first non-undefined value
  34. objectPath.coalesce(obj, ['a.z', 'a.d', ['a','b']], 'default');
  35. //empty a given path (but do not delete it) depending on their type,so it retains reference to objects and arrays.
  36. //functions that are not inherited from prototype are set to null.
  37. //object instances are considered objects and just own property names are deleted
  38. objectPath.empty(obj, 'a.b'); // obj.a.b is now ''
  39. objectPath.empty(obj, 'a.c'); // obj.a.c is now []
  40. objectPath.empty(obj, 'a'); // obj.a is now {}
  41. //works also with arrays
  42. objectPath.get(obj, "a.c.1"); //returns "f"
  43. objectPath.get(obj, ["a","c","1"]); //returns "f"
  44. //can return a default value with get
  45. objectPath.get(obj, ["a.c.b"], "DEFAULT"); //returns "DEFAULT", since a.c.b path doesn't exists, if omitted, returns undefined
  46. //set
  47. objectPath.set(obj, "a.h", "m"); // or objectPath.set(obj, ["a","h"], "m");
  48. objectPath.get(obj, "a.h"); //returns "m"
  49. //set will create intermediate object/arrays
  50. objectPath.set(obj, "a.j.0.f", "m");
  51. //will insert values in array
  52. objectPath.insert(obj, "a.c", "m", 1); // obj.a.c = ["e", "m", "f"]
  53. //push into arrays (and create intermediate objects/arrays)
  54. objectPath.push(obj, "a.k", "o");
  55. //ensure a path exists (if it doesn't, set the default value you provide)
  56. objectPath.ensureExists(obj, "a.k.1", "DEFAULT");
  57. //deletes a path
  58. objectPath.del(obj, "a.b"); // obj.a.b is now undefined
  59. objectPath.del(obj, ["a","c",0]); // obj.a.c is now ['f']
  60. //tests path existence
  61. objectPath.has(obj, "a.b"); // true
  62. objectPath.has(obj, ["a","d"]); // false
  63. //bind object
  64. var model = objectPath({
  65. a: {
  66. b: "d",
  67. c: ["e", "f"]
  68. }
  69. });
  70. //now any method from above is supported directly w/o passing an object
  71. model.get("a.b"); //returns "d"
  72. model.get(["a.c.b"], "DEFAULT"); //returns "DEFAULT"
  73. model.del("a.b"); // obj.a.b is now undefined
  74. model.has("a.b"); // false
  75. ```
  76. ### Credits
  77. * [Mario Casciaro](https://github.com/mariocasciaro) - Author
  78. * [Paulo Cesar](https://github.com/pocesar) - Major contributor