版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1 год назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # YAML <a href="https://www.npmjs.com/package/yaml"><img align="right" src="https://badge.fury.io/js/yaml.svg" title="npm package" /></a>
  2. `yaml` is a definitive library for [YAML](https://yaml.org/), the human friendly data serialization standard.
  3. This library:
  4. - Supports both YAML 1.1 and YAML 1.2 and all common data schemas,
  5. - Passes all of the [yaml-test-suite](https://github.com/yaml/yaml-test-suite) tests,
  6. - Can accept any string as input without throwing, parsing as much YAML out of it as it can, and
  7. - Supports parsing, modifying, and writing YAML comments and blank lines.
  8. The library is released under the ISC open source license, and the code is [available on GitHub](https://github.com/eemeli/yaml/).
  9. It has no external dependencies and runs on Node.js as well as modern browsers.
  10. For the purposes of versioning, any changes that break any of the documented endpoints or APIs will be considered semver-major breaking changes.
  11. Undocumented library internals may change between minor versions, and previous APIs may be deprecated (but not removed).
  12. For more information, see the project's documentation site: [**eemeli.org/yaml**](https://eemeli.org/yaml/)
  13. To install:
  14. ```sh
  15. npm install yaml
  16. ```
  17. **Note:** These docs are for `yaml@2`. For v1, see the [v1.10.0 tag](https://github.com/eemeli/yaml/tree/v1.10.0) for the source and [eemeli.org/yaml/v1](https://eemeli.org/yaml/v1/) for the documentation.
  18. ## API Overview
  19. The API provided by `yaml` has three layers, depending on how deep you need to go: [Parse & Stringify](https://eemeli.org/yaml/#parse-amp-stringify), [Documents](https://eemeli.org/yaml/#documents), and the underlying [Lexer/Parser/Composer](https://eemeli.org/yaml/#parsing-yaml).
  20. The first has the simplest API and "just works", the second gets you all the bells and whistles supported by the library along with a decent [AST](https://eemeli.org/yaml/#content-nodes), and the third lets you get progressively closer to YAML source, if that's your thing.
  21. ```js
  22. import { parse, stringify } from 'yaml'
  23. // or
  24. import YAML from 'yaml'
  25. // or
  26. const YAML = require('yaml')
  27. ```
  28. ### Parse & Stringify
  29. - [`parse(str, reviver?, options?): value`](https://eemeli.org/yaml/#yaml-parse)
  30. - [`stringify(value, replacer?, options?): string`](https://eemeli.org/yaml/#yaml-stringify)
  31. ### Documents
  32. - [`Document`](https://eemeli.org/yaml/#documents)
  33. - [`constructor(value, replacer?, options?)`](https://eemeli.org/yaml/#creating-documents)
  34. - [`#anchors`](https://eemeli.org/yaml/#working-with-anchors)
  35. - [`#contents`](https://eemeli.org/yaml/#content-nodes)
  36. - [`#directives`](https://eemeli.org/yaml/#stream-directives)
  37. - [`#errors`](https://eemeli.org/yaml/#errors)
  38. - [`#warnings`](https://eemeli.org/yaml/#errors)
  39. - [`isDocument(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
  40. - [`parseAllDocuments(str, options?): Document[]`](https://eemeli.org/yaml/#parsing-documents)
  41. - [`parseDocument(str, options?): Document`](https://eemeli.org/yaml/#parsing-documents)
  42. ### Content Nodes
  43. - [`isAlias(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
  44. - [`isCollection(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
  45. - [`isMap(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
  46. - [`isNode(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
  47. - [`isPair(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
  48. - [`isScalar(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
  49. - [`isSeq(foo): boolean`](https://eemeli.org/yaml/#identifying-nodes)
  50. - [`new Scalar(value)`](https://eemeli.org/yaml/#scalar-values)
  51. - [`new YAMLMap()`](https://eemeli.org/yaml/#collections)
  52. - [`new YAMLSeq()`](https://eemeli.org/yaml/#collections)
  53. - [`doc.createAlias(node, name?): Alias`](https://eemeli.org/yaml/#working-with-anchors)
  54. - [`doc.createNode(value, options?): Node`](https://eemeli.org/yaml/#creating-nodes)
  55. - [`doc.createPair(key, value): Pair`](https://eemeli.org/yaml/#creating-nodes)
  56. - [`visit(node, visitor)`](https://eemeli.org/yaml/#modifying-nodes)
  57. ### Parsing YAML
  58. - [`new Lexer().lex(src)`](https://eemeli.org/yaml/#lexer)
  59. - [`new Parser(onNewLine?).parse(src)`](https://eemeli.org/yaml/#parser)
  60. - [`new Composer(options?).compose(tokens)`](https://eemeli.org/yaml/#composer)
  61. ## YAML.parse
  62. ```yaml
  63. # file.yml
  64. YAML:
  65. - A human-readable data serialization language
  66. - https://en.wikipedia.org/wiki/YAML
  67. yaml:
  68. - A complete JavaScript implementation
  69. - https://www.npmjs.com/package/yaml
  70. ```
  71. ```js
  72. import fs from 'fs'
  73. import YAML from 'yaml'
  74. YAML.parse('3.14159')
  75. // 3.14159
  76. YAML.parse('[ true, false, maybe, null ]\n')
  77. // [ true, false, 'maybe', null ]
  78. const file = fs.readFileSync('./file.yml', 'utf8')
  79. YAML.parse(file)
  80. // { YAML:
  81. // [ 'A human-readable data serialization language',
  82. // 'https://en.wikipedia.org/wiki/YAML' ],
  83. // yaml:
  84. // [ 'A complete JavaScript implementation',
  85. // 'https://www.npmjs.com/package/yaml' ] }
  86. ```
  87. ## YAML.stringify
  88. ```js
  89. import YAML from 'yaml'
  90. YAML.stringify(3.14159)
  91. // '3.14159\n'
  92. YAML.stringify([true, false, 'maybe', null])
  93. // `- true
  94. // - false
  95. // - maybe
  96. // - null
  97. // `
  98. YAML.stringify({ number: 3, plain: 'string', block: 'two\nlines\n' })
  99. // `number: 3
  100. // plain: string
  101. // block: |
  102. // two
  103. // lines
  104. // `
  105. ```
  106. ---
  107. Browser testing provided by:
  108. <a href="https://www.browserstack.com/open-source">
  109. <img width=200 src="https://eemeli.org/yaml/images/browserstack.svg" />
  110. </a>