Skip to content

Simple ini file parser ! Support comments, easy to use API. It has 0 dependencies and can run in the browser or using node.

License

Notifications You must be signed in to change notification settings

thegostisdead/js-ini-parser

Repository files navigation

ini parser

A simple parser for ini files. Preserve comments and support global section.
npm version npm downloads codecov example workflow

🚧 This project is under development.

Table of Contents

Installation

Install package:

# npm
npm install js-ini-parser

# yarn
yarn add js-ini-parser

# pnpm
pnpm install js-ini-parser

Import:

// ESM
import { parseIni, stringifyIni } from "js-ini-parser";

// CommonJS
const { parseIni, stringifyIni } = require("js-ini-parser");

Usage

API

  • parseIni(text, ParserOptions)
  • stringifyIni(object, {})

ParserOptions

  • allowGlobalSection: boolean - Allow global section (default: false)
  • globalSectionName: string - Name of the global section (default: global)
  • allowEmptyValue: boolean - Allow empty value (default: false)
  • debug: boolean - Enable debug mode (default: false)

Parse text input to object

import { parseIni } from "js-ini-parser";

const ini = `
[server]
; this is a comment
host = 
port = 8080
`;

const options = {
  allowGlobalSection: true,
  globalSectionName: 'global'
}

const parsed = parseIni(ini, options);

console.log(parsed);

Parse text from file to object

import * as fs from "node:fs/promises";
import { parseIni } from "js-ini-parser";


const options = {
  allowGlobalSection: true,
  globalSectionName: 'global'
}

const fileContent = await fs.readFile("./config.ini", "utf-8");
const parsed = parseIni(fileContent, options);

Edit object and convert to text

⚠️️ Not available yet but will be soon !
Currently, you can edit the object manually and stringify it back to text using stringifyIni

  • In the future, you will be able to edit the object using the API (addComment, updateKeyValue, etc...)
  • Or by accessing to data like this: parsed.server.host = 'localhost'
import { parse, stringify } from "js-ini-parser";

const ini = `
[server]
; this is a comment
host =
port = 8080
`;

const options = {
  allowGlobalSection: true,
  globalSectionName: 'global'
}

const parsed = parseIni(ini, options);

// convert to text
const text = stringifyIni(parsed, {})

Contributing

  • Clone this repository
  • Install dependencies using pnpm install
  • Run interactive tests using pnpm dev

License

Published under MIT License.

About

Simple ini file parser ! Support comments, easy to use API. It has 0 dependencies and can run in the browser or using node.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published