您当前的位置: 首页 > 

顺其自然~

暂无认证

  • 2浏览

    0关注

    1317博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

nodejs读写ini

顺其自然~ 发布时间:2022-04-07 08:54:04 ,浏览量:2

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)

encode(object, [options])

将对象编码object为 ini 样式的格式化字符串。如果给出了可选参数section,则对象的所有顶级属性都放入此部分,并且section-string 将添加到所有子部分,请参见上面的用法示例。

options对象可能包含以下内容:

  • section一个字符串,它将是section编码的 ini 数据中的第一个。默认为无。
  • whitespace布尔值,指定是否在字符周围放置空格 =。默认情况下,省略空格,以便对一些不能很好容忍它的挑剔的旧解析器友好。但是有些人发现它更易于阅读,并且带有空格。

出于向后兼容性的原因,如果string传入了一个选项,则假定它是该section值。

stringify(object, [options])

别名encode(object, [options])

safe(val)

转义字符串val,使其可以安全地用作 ini 文件中的键或值。基本上转义引号。例如

ini.safe('"unsafe string"')

would result in

"\"unsafe string\""
unsafe(val)

对字符串进行转义val

关注
打赏
1662339380
查看更多评论
立即登录/注册

微信扫码登录

0.0399s