An ini format parser and serializer for node.
Sections are treated as nested objects. Items before the first heading are saved on the object directly.
node.js 的 ini 格式解析器和序列化器。
“节”被视为嵌套对象。第一个标题之前的项目直接保存在对象上。
用法考虑一个如下所示的ini文件config.ini
:
; this comment is being ignored
scope = global
[database]
user = dbuser
password = dbpassword
database = use_this_database
[paths.default]
datadir = /var/lib/data
array[] = first value
array[] = second value
array[] = third value
您可以像这样读取、操作和编写 ini 文件:
var fs = require('fs'), ini = require('ini')
var config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'))
config.scope = 'local'
config.database.database = 'use_another_database'
config.paths.default.tmpdir = '/tmp'
delete config.paths.default.datadir
config.paths.default.array.push('fourth value')
fs.writeFileSync('./config_modified.ini', ini.stringify(config, { section: 'section' }))
这将导致一个名为的文件config_modified.ini
被写入具有以下内容的文件系统:
[section]
scope=local
[section.database]
user=dbuser
password=dbpassword
database=use_another_database
[section.paths.default]
tmpdir=/tmp
array[]=first value
array[]=second value
array[]=third value
array[]=fourth value
API
decode(inistring)
将格式化为嵌套对象的 ini 样式解码。
parse(inistring)别名decode(inistring)
将对象编码object
为 ini 样式的格式化字符串。如果给出了可选参数section
,则对象的所有顶级属性都放入此部分,并且section
-string 将添加到所有子部分,请参见上面的用法示例。
该options
对象可能包含以下内容:
section
一个字符串,它将是section
编码的 ini 数据中的第一个。默认为无。whitespace
布尔值,指定是否在字符周围放置空格=
。默认情况下,省略空格,以便对一些不能很好容忍它的挑剔的旧解析器友好。但是有些人发现它更易于阅读,并且带有空格。
出于向后兼容性的原因,如果string
传入了一个选项,则假定它是该section
值。
别名encode(object, [options])
转义字符串val
,使其可以安全地用作 ini 文件中的键或值。基本上转义引号。例如
ini.safe('"unsafe string"')
would result in
"\"unsafe string\""
unsafe(val)
对字符串进行转义val