Giter VIP home page Giter VIP logo

ini-parser's Introduction

@jedmao/ini-parser

NPM version npm license Travis Build Status codecov Dependency Status

npm

A highly forgiving and configurable INI parser for the informal INI file format.

Introduction

The INI file format is informal. Different parsers behave differently, in that they support different features. This parser aims to be a bit more flexible and configurable to suite your needs, whatever they may be. Also, it assumes INI files out there in the wild could have some pretty crazy things in them. This parser does its best to understand whatever crazy you throw at it.

No dependencies on Node.js here, so you should be able to use it in the browser.

Usage

; example.ini

a=b

[c]
d:e

[c]
f=g
import { readFileSync } from "fs";
import Parser from "@jedmao/ini-parser";

const { configure, parse } = new Parser(/* config */);

parse(fs.readFileSync("./example.ini"));

See Parser#parse API.

Result

[
  {
    "a": "b"
  },
  [
    [
      "c",
      {
        "d": "e"
      }
    ],
    [
      "c",
      {
        "f": "g"
      }
    ]
  ]
]

API

import Parser from "@jedmao/ini-parser";

new Parser( options?: ParseOptions )

Class constructor. Accepts ParseOptions for initial configuration.

Parser#configure( options?: ParseOptions )

Sets configuration options, preserving existing configuration and overriding only the new keys you provide.

Parser#resetConfiguration()

Resets configuration to default settings as if you created a new Parser().

Parser#parse( contents?: string )

Parses INI file contents as a string. The result will be an array:

  • Index 0 will have any/all root properties.
  • Index 1 will have an array of any/all sections that follow.

Note: repeated sections will also be repeated in the array.

See Usage for an example.

interface ParseOptions

interface ParseOptions {
  /**
   * Indicates accepted comment chars. Only works if you specify single-char
   * comment values in RegExp form. A setting of `false` turns off comments
   * completely, treating comment chars as normal string values.
   * @default {RegExp} /[#;]/
   */
  comment?: RegExp | false;
  /**
   * Accepts comment chars in a property key or value. If a space
   * follows the comment char, it is considered an actual comment.
   * Example: "#k;=#v; #z" -> { "#k;": "#v;" }
   * @default {false} false
   */
  isCommentCharInProp?: boolean;
  /**
   * Indicates accepted delimiter chars. Only works if you specify
   * single-char delimiter values in RegExp form.
   * @default {RegExp} /[=:]/
   */
  delimiter?: RegExp;
  /**
   * Indicates accepted newline sequences in the form of a RegExp.
   * @default {RegExp} /\r?\n/
   */
  newline?: RegExp;
  /**
   * By default, attempts to parse property values with `JSON.parse`.
   * If unsuccessful, returns property value as a string. You may also
   * provide your own resolve function here for custom property value
   * resolution.
   * @default {true} true
   */
  resolve?: boolean | ResolveCallback;
}

interface ResolveCallback

interface ResolveCallback {
  (value: string, key?: string, fallback?: typeof parseValue): any;
}

parseValue( value: string )

Attempts to parse a string value with JSON.parse. If unsuccessful, returns input string untouched.

Testing

Run the following command:

$ npm test

This will build scripts, run tests and generate a code coverage report. Anything less than 100% coverage will throw an error.

Watching

For much faster development cycles, run the following commands in 2 separate processes:

npm run build:watch

Compiles TypeScript source into the ./dist folder and watches for changes.

$ npm run watch

Runs the tests in the ./dist folder and watches for changes.

ini-parser's People

Contributors

jednano avatar jedmao avatar

Stargazers

hltdev8642 avatar The Sun Didn't Vanish avatar Manuel Domínguez Martín avatar Seth Whitaker avatar Ivan Nikolić avatar  avatar Dave Jansen avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.