48add87f192fc62fdaa14a924e02a4382adca50e7017905c276062dfd49af66d05494c1721eb63fcfa9d8f7f69e4143cc7a9225ffd2b6a5298c980d0077f91 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # dot-prop [![Build Status](https://travis-ci.org/sindresorhus/dot-prop.svg?branch=master)](https://travis-ci.org/sindresorhus/dot-prop)
  2. > Get, set, or delete a property from a nested object using a dot path
  3. ## Install
  4. ```
  5. $ npm install dot-prop
  6. ```
  7. ## Usage
  8. ```js
  9. const dotProp = require('dot-prop');
  10. // Getter
  11. dotProp.get({foo: {bar: 'unicorn'}}, 'foo.bar');
  12. //=> 'unicorn'
  13. dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep');
  14. //=> undefined
  15. dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value');
  16. //=> 'default value'
  17. dotProp.get({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot');
  18. //=> 'unicorn'
  19. // Setter
  20. const object = {foo: {bar: 'a'}};
  21. dotProp.set(object, 'foo.bar', 'b');
  22. console.log(object);
  23. //=> {foo: {bar: 'b'}}
  24. const foo = dotProp.set({}, 'foo.bar', 'c');
  25. console.log(foo);
  26. //=> {foo: {bar: 'c'}}
  27. dotProp.set(object, 'foo.baz', 'x');
  28. console.log(object);
  29. //=> {foo: {bar: 'b', baz: 'x'}}
  30. // Has
  31. dotProp.has({foo: {bar: 'unicorn'}}, 'foo.bar');
  32. //=> true
  33. // Deleter
  34. const object = {foo: {bar: 'a'}};
  35. dotProp.delete(object, 'foo.bar');
  36. console.log(object);
  37. //=> {foo: {}}
  38. object.foo.bar = {x: 'y', y: 'x'};
  39. dotProp.delete(object, 'foo.bar.x');
  40. console.log(object);
  41. //=> {foo: {bar: {y: 'x'}}}
  42. ```
  43. ## API
  44. ### get(object, path, defaultValue?)
  45. ### set(object, path, value)
  46. Returns the object.
  47. ### has(object, path)
  48. ### delete(object, path)
  49. Returns a boolean of whether the property existed before being deleted.
  50. #### object
  51. Type: `object`
  52. Object to get, set, or delete the `path` value.
  53. You are allowed to pass in `undefined` as the object to the `get` and `has` functions.
  54. #### path
  55. Type: `string`
  56. Path of the property in the object, using `.` to separate each nested key.
  57. Use `\\.` if you have a `.` in the key.
  58. The following path components are invalid and results in `undefined` being returned: `__proto__`, `prototype`, `constructor`.
  59. #### value
  60. Type: `unknown`
  61. Value to set at `path`.
  62. #### defaultValue
  63. Type: `unknown`
  64. Default value.
  65. ---
  66. <div align="center">
  67. <b>
  68. <a href="https://tidelift.com/subscription/pkg/npm-dot-prop?utm_source=npm-dot-prop&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
  69. </b>
  70. <br>
  71. <sub>
  72. Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
  73. </sub>
  74. </div>