379ea49194373c47d0eaa8dfde6773a31cfd2445fa63531af39ab2c7a7992bafb4752d5ffe04a79d3fe472bf2c5725ff540c38636ea6899d765f114a01413d 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # confbox
  2. <!-- automd:badges color=yellow bundlephobia packagephobia -->
  3. [![npm version](https://img.shields.io/npm/v/confbox?color=yellow)](https://npmjs.com/package/confbox)
  4. [![npm downloads](https://img.shields.io/npm/dm/confbox?color=yellow)](https://npm.chart.dev/confbox)
  5. [![bundle size](https://img.shields.io/bundlephobia/minzip/confbox?color=yellow)](https://bundlephobia.com/package/confbox)
  6. <!-- /automd -->
  7. Parsing and serialization utils for [YAML](https://yaml.org/) ([js-yaml](https://github.com/nodeca/js-yaml)), [TOML](https://toml.io/) ([smol-toml](https://github.com/squirrelchat/smol-toml)), [JSONC](https://github.com/microsoft/node-jsonc-parser) ([jsonc-parser](https://github.com/microsoft/node-jsonc-parser)), [JSON5](https://json5.org/) ([json5](https://github.com/json5/json5)), and [JSON](https://www.json.org/json-en.html).
  8. ✨ Zero dependency and tree-shakable
  9. ✨ Types exported out of the box
  10. ✨ Presrves code style (indentation and whitespace)
  11. > [!TIP]
  12. > Use [unjs/c12](https://github.com/unjs/c12) for a full featured configuration loader!
  13. ## Usage
  14. Install package:
  15. <!-- automd:pm-i no-version -->
  16. ```sh
  17. # ✨ Auto-detect
  18. npx nypm install confbox
  19. # npm
  20. npm install confbox
  21. # yarn
  22. yarn add confbox
  23. # pnpm
  24. pnpm install confbox
  25. # bun
  26. bun install confbox
  27. # deno
  28. deno install confbox
  29. ```
  30. <!-- /automd -->
  31. Import:
  32. <!-- automd:jsimport cjs cdn src="./src/index.ts" -->
  33. **ESM** (Node.js, Bun, Deno)
  34. ```js
  35. import {
  36. parseJSON5,
  37. stringifyJSON5,
  38. parseJSONC,
  39. stringifyJSONC,
  40. parseYAML,
  41. stringifyYAML,
  42. parseJSON,
  43. stringifyJSON,
  44. parseTOML,
  45. stringifyTOML,
  46. } from "confbox";
  47. ```
  48. **CommonJS** (Legacy Node.js)
  49. ```js
  50. const {
  51. parseJSON5,
  52. stringifyJSON5,
  53. parseJSONC,
  54. stringifyJSONC,
  55. parseYAML,
  56. stringifyYAML,
  57. parseJSON,
  58. stringifyJSON,
  59. parseTOML,
  60. stringifyTOML,
  61. } = require("confbox");
  62. ```
  63. **CDN** (Deno, Bun and Browsers)
  64. ```js
  65. import {
  66. parseJSON5,
  67. stringifyJSON5,
  68. parseJSONC,
  69. stringifyJSONC,
  70. parseYAML,
  71. stringifyYAML,
  72. parseJSON,
  73. stringifyJSON,
  74. parseTOML,
  75. stringifyTOML,
  76. } from "https://esm.sh/confbox";
  77. ```
  78. <!-- /automd -->
  79. <!-- automd:jsdocs src="./src/index" -->
  80. ### `parseJSON(text, options?)`
  81. Converts a [JSON](https://www.json.org/json-en.html) string into an object.
  82. Indentation status is auto-detected and preserved when stringifying back using `stringifyJSON`
  83. ### `parseJSON5(text, options?)`
  84. Converts a [JSON5](https://json5.org/) string into an object.
  85. ### `parseJSONC(text, options?)`
  86. Converts a [JSONC](https://github.com/microsoft/node-jsonc-parser) string into an object.
  87. ### `parseTOML(text)`
  88. Converts a [TOML](https://toml.io/) string into an object.
  89. ### `parseYAML(text, options?)`
  90. Converts a [YAML](https://yaml.org/) string into an object.
  91. ### `stringifyJSON(value, options?)`
  92. Converts a JavaScript value to a [JSON](https://www.json.org/json-en.html) string.
  93. Indentation status is auto detected and preserved when using value from parseJSON.
  94. ### `stringifyJSON5(value, options?)`
  95. Converts a JavaScript value to a [JSON5](https://json5.org/) string.
  96. ### `stringifyJSONC(value, options?)`
  97. Converts a JavaScript value to a [JSONC](https://github.com/microsoft/node-jsonc-parser) string.
  98. ### `stringifyTOML(value)`
  99. Converts a JavaScript value to a [TOML](https://toml.io/) string.
  100. ### `stringifyYAML(value, options?)`
  101. Converts a JavaScript value to a [YAML](https://yaml.org/) string.
  102. <!-- /automd -->
  103. <!-- automd:fetch url="gh:unjs/.github/main/snippets/readme-contrib-node-pnpm.md" -->
  104. ## Contribution
  105. <details>
  106. <summary>Local development</summary>
  107. - Clone this repository
  108. - Install the latest LTS version of [Node.js](https://nodejs.org/en/)
  109. - Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`
  110. - Install dependencies using `pnpm install`
  111. - Run tests using `pnpm dev` or `pnpm test`
  112. </details>
  113. <!-- /automd -->
  114. ## License
  115. <!-- automd:contributors license=MIT author=pi0 -->
  116. Published under the [MIT](https://github.com/unjs/confbox/blob/main/LICENSE) license.
  117. Made by [@pi0](https://github.com/pi0) and [community](https://github.com/unjs/confbox/graphs/contributors) 💛
  118. <br><br>
  119. <a href="https://github.com/unjs/confbox/graphs/contributors">
  120. <img src="https://contrib.rocks/image?repo=unjs/confbox" />
  121. </a>
  122. <!-- /automd -->
  123. <!-- automd:with-automd -->
  124. ---
  125. _🤖 auto updated with [automd](https://automd.unjs.io)_
  126. <!-- /automd -->