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.

102 lines
2.7 KiB

4 years ago
  1. An ini format parser and serializer for node.
  2. Sections are treated as nested objects. Items before the first
  3. heading are saved on the object directly.
  4. ## Usage
  5. Consider an ini-file `config.ini` that looks like this:
  6. ; this comment is being ignored
  7. scope = global
  8. [database]
  9. user = dbuser
  10. password = dbpassword
  11. database = use_this_database
  12. [paths.default]
  13. datadir = /var/lib/data
  14. array[] = first value
  15. array[] = second value
  16. array[] = third value
  17. You can read, manipulate and write the ini-file like so:
  18. var fs = require('fs')
  19. , ini = require('ini')
  20. var config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'))
  21. config.scope = 'local'
  22. config.database.database = 'use_another_database'
  23. config.paths.default.tmpdir = '/tmp'
  24. delete config.paths.default.datadir
  25. config.paths.default.array.push('fourth value')
  26. fs.writeFileSync('./config_modified.ini', ini.stringify(config, { section: 'section' }))
  27. This will result in a file called `config_modified.ini` being written
  28. to the filesystem with the following content:
  29. [section]
  30. scope=local
  31. [section.database]
  32. user=dbuser
  33. password=dbpassword
  34. database=use_another_database
  35. [section.paths.default]
  36. tmpdir=/tmp
  37. array[]=first value
  38. array[]=second value
  39. array[]=third value
  40. array[]=fourth value
  41. ## API
  42. ### decode(inistring)
  43. Decode the ini-style formatted `inistring` into a nested object.
  44. ### parse(inistring)
  45. Alias for `decode(inistring)`
  46. ### encode(object, [options])
  47. Encode the object `object` into an ini-style formatted string. If the
  48. optional parameter `section` is given, then all top-level properties
  49. of the object are put into this section and the `section`-string is
  50. prepended to all sub-sections, see the usage example above.
  51. The `options` object may contain the following:
  52. * `section` A string which will be the first `section` in the encoded
  53. ini data. Defaults to none.
  54. * `whitespace` Boolean to specify whether to put whitespace around the
  55. `=` character. By default, whitespace is omitted, to be friendly to
  56. some persnickety old parsers that don't tolerate it well. But some
  57. find that it's more human-readable and pretty with the whitespace.
  58. For backwards compatibility reasons, if a `string` options is passed
  59. in, then it is assumed to be the `section` value.
  60. ### stringify(object, [options])
  61. Alias for `encode(object, [options])`
  62. ### safe(val)
  63. Escapes the string `val` such that it is safe to be used as a key or
  64. value in an ini-file. Basically escapes quotes. For example
  65. ini.safe('"unsafe string"')
  66. would result in
  67. "\"unsafe string\""
  68. ### unsafe(val)
  69. Unescapes the string `val`