版博士V2.0程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

README.md 3.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <h1 align=center>
  2. <a href="http://chaijs.com" title="Chai Documentation">
  3. <img alt="ChaiJS" src="http://chaijs.com/img/chai-logo.png">
  4. </a>
  5. <br>
  6. pathval
  7. </h1>
  8. <p align=center>
  9. Tool for Object value retrieval given a string path for <a href="http://nodejs.org">node</a> and the browser.
  10. </p>
  11. <p align=center>
  12. <a href="./LICENSE">
  13. <img
  14. alt="license:mit"
  15. src="https://img.shields.io/badge/license-mit-green.svg?style=flat-square"
  16. />
  17. </a>
  18. <a href="https://github.com/chaijs/pathval/releases">
  19. <img
  20. alt="tag:?"
  21. src="https://img.shields.io/github/tag/chaijs/pathval.svg?style=flat-square"
  22. />
  23. </a>
  24. <a href="https://travis-ci.org/chaijs/pathval">
  25. <img
  26. alt="build:?"
  27. src="https://img.shields.io/travis/chaijs/pathval/master.svg?style=flat-square"
  28. />
  29. </a>
  30. <a href="https://coveralls.io/r/chaijs/pathval">
  31. <img
  32. alt="coverage:?"
  33. src="https://img.shields.io/coveralls/chaijs/pathval/master.svg?style=flat-square"
  34. />
  35. </a>
  36. <a href="https://www.npmjs.com/packages/pathval">
  37. <img
  38. alt="npm:?"
  39. src="https://img.shields.io/npm/v/pathval.svg?style=flat-square"
  40. />
  41. </a>
  42. <a href="https://www.npmjs.com/packages/pathval">
  43. <img
  44. alt="dependencies:?"
  45. src="https://img.shields.io/npm/dm/pathval.svg?style=flat-square"
  46. />
  47. </a>
  48. <a href="">
  49. <img
  50. alt="devDependencies:?"
  51. src="https://img.shields.io/david/chaijs/pathval.svg?style=flat-square"
  52. />
  53. </a>
  54. <br/>
  55. <a href="https://saucelabs.com/u/chaijs-pathval">
  56. <img
  57. alt="Selenium Test Status"
  58. src="https://saucelabs.com/browser-matrix/chaijs-pathval.svg"
  59. />
  60. </a>
  61. <br>
  62. <a href="https://chai-slack.herokuapp.com/">
  63. <img
  64. alt="Join the Slack chat"
  65. src="https://img.shields.io/badge/slack-join%20chat-E2206F.svg?style=flat-square"
  66. />
  67. </a>
  68. <a href="https://gitter.im/chaijs/chai">
  69. <img
  70. alt="Join the Gitter chat"
  71. src="https://img.shields.io/badge/gitter-join%20chat-D0104D.svg?style=flat-square"
  72. />
  73. </a>
  74. </p>
  75. ## What is pathval?
  76. Pathval is a module which you can use to retrieve or set an Object's property for a given `String` path.
  77. ## Installation
  78. ### Node.js
  79. `pathval` is available on [npm](http://npmjs.org). To install it, type:
  80. $ npm install pathval
  81. ### Browsers
  82. You can also use it within the browser; install via npm and use the `pathval.js` file found within the download. For example:
  83. ```html
  84. <script src="./node_modules/pathval/pathval.js"></script>
  85. ```
  86. ## Usage
  87. The primary export of `pathval` is an object which has the following methods:
  88. * `hasProperty(object, name)` - Checks whether an `object` has `name`d property or numeric array index.
  89. * `getPathInfo(object, path)` - Returns an object with info indicating the value of the `parent` of that path, the `name ` of the property we're retrieving and its `value`.
  90. * `getPathValue(object, path)` - Retrieves the value of a property at a given `path` inside an `object`'.
  91. * `setPathValue(object, path, value)` - Sets the `value` of a property at a given `path` inside an `object` and returns the object in which the property has been set.
  92. ```js
  93. var pathval = require('pathval');
  94. ```
  95. #### .hasProperty(object, name)
  96. ```js
  97. var pathval = require('pathval');
  98. var obj = { prop: 'a value' };
  99. pathval.hasProperty(obj, 'prop'); // true
  100. ```
  101. #### .getPathInfo(object, path)
  102. ```js
  103. var pathval = require('pathval');
  104. var obj = { earth: { country: 'Brazil' } };
  105. pathval.getPathInfo(obj, 'earth.country'); // { parent: { country: 'Brazil' }, name: 'country', value: 'Brazil', exists: true }
  106. ```
  107. #### .getPathValue(object, path)
  108. ```js
  109. var pathval = require('pathval');
  110. var obj = { earth: { country: 'Brazil' } };
  111. pathval.getPathValue(obj, 'earth.country'); // 'Brazil'
  112. ```
  113. #### .setPathValue(object, path, value)
  114. ```js
  115. var pathval = require('pathval');
  116. var obj = { earth: { country: 'Brazil' } };
  117. pathval.setPathValue(obj, 'earth.country', 'USA');
  118. obj.earth.country; // 'USA'
  119. ```