Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/iniPublic

An ini parser/serializer in JavaScript

License

NotificationsYou must be signed in to change notification settings

npm/ini

Repository files navigation

An INI format parser & serializer.

  • Sections are treated as nested objects.

  • Section-less items are treated as globals.

Usage

Consider an INI file such as the following:

; This comment is being ignoredscope = global[database]user = dbuserpassword = dbpassworddatabase = use_this_database[paths.default]datadir = /var/lib/dataarray[] = first valuearray[] = second valuearray[] = third value

You canread,modify andwrite it like so:

import{writeFile,readFile}from'node:fs/promises'import{stringify,parse}from'ini'//  Read INI file as textlettext=awaitreadFile(`./Original.ini`,{encoding :'utf-8'})//  Parse text data to objectconstconfig=parse(text)//  Modify data objectconfig.scope='local'config.database.database='use_another_database'config.paths.default.tmpdir='/tmp'deleteconfig.paths.default.datadirconfig.paths.default.array.push('fourth value')//  Stringify data objecttext=stringify(config,{section :'section'})//  Write INI file as textawaitwriteFile(`./Modified.ini`,text)

The written file will contain the following:

[section]scope=local[section.database]user=dbuserpassword=dbpassworddatabase=use_another_database[section.paths.default]tmpdir=/tmparray[]=first valuearray[]=second valuearray[]=third valuearray[]=fourth value

API

Parse

Attempts to turn the given INI string into a nested data object.

// You can also use `decode`constobject=parse(`<INI Text>`)

Stringify

Encodes the given data object as an INI formatted string.

// You can also use `encode`stringify(object,{/**     *  Whether to insert spaces before & after `=`     *     *  Disabled by default to have better     *  compatibility with old picky parsers.     */whitespace :false,/**     *  Whether to align the `=` character for each section.     *  -> Also enables the `whitespace` option     */align :false,/**     *  Identifier to use for global items     *  and to prepend to all other sections.     */    section,/**     *  Whether to sort all sections & their keys alphabetically.     */sort :false,/**     *  Whether to insert a newline after each section header.     *     *  The TOSHIBA & FlashAir parser require this format.     */newline :false,/**     *  Which platforms line-endings should be used.     *     *  win32 -> CR+LF     *  other -> LF     *     *  Default is the current platform     */    platform,/**     *  Whether to append `[]` to array keys.     *     *  Some parsers treat duplicate names by themselves as arrays     */bracketedArray :true})

For backwards compatibility any string passed as the
options parameter is treated as thesection option.

stringify(object,'section')

Un / Escape

Turn the given string into a safe to
use key or value in your INI file.

safe(`"unsafe string"`)// -> \"unsafe string\"

Or reverse the process with:

unsafe(`\\"safe string\\"`)// -> "safe string"

[8]ページ先頭

©2009-2025 Movatter.jp