Giter Club home page Giter Club logo

config-ini's Introduction

Introduction for config-ini Build Status Coverage Status

  • Description JavaScript Configuration file(.ini) content parser, similar to python ConfigParser without I/O operations. Only one JavaScript file without any other dependencies. Compatible with NodeJS, TypeScript and Browsers.

  • Author Erxin(Edwin) Shang

Install

$ npm install config-ini-parser

or

$ bower install config-ini

Simple example ini content

  • If there is no section supplied for the options then a default section will be created
optionName0=value0
optionName2=value2

[sectionName0]
optionName0=value0
optionName1=value1
...
[sectionName1]
optionName0=value0
optionName1=value1
optionName2=value2
...

Use cases

  • For node
var ConfigIniParser = require("config-ini-parser").ConfigIniParser;
parser = new ConfigIniParser(); //Use default delimiter
parser.parse(iniContent);
var value = parser.get("section", "option");
parser.stringify('\n');
var ConfigIniParser = require("config-ini-parser").ConfigIniParser;
var delimiter = "\r\n"; //or "\n" for *nux

parser = new ConfigIniParser(delimiter); //If don't assign the parameter delimiter then the default value \n will be used
parser.parse(iniContent);
var value = parser.get("section", "option");
value = parser.get(null, "option"); //access the default section
value = parser.getOptionFromDefaultSection("option"); //access the default section
parser.stringify('\n'); //get all the ini file content as a string
  • For browser, add config-ini.js to html pages
var ConfigIniParser = require("config-ini-parser").ConfigIniParser;
parser = new ConfigIniParser(); //Use default delimiter
parser.parse(iniContent);
var value = parser.get("section", "option");
parser.stringify('\r\n');
var delimiter = "\r\n"; //or "\n" for *nux. by default it will use \n

parser = new ConfigIniParser(delimiter); //If don't assign the parameter delimiter then the default value \n will be used
parser.parse(iniContent);
var value = parser.get("section", "option");
value = parser.get(null, "option"); //access the default section
value = parser.getOptionFromDefaultSection("option"); //access the default section
  • Reference the config-ini.d.ts file in a typescript file
///<reference path="..\\node_modules\\config-ini-parser\\config-ini.d.ts"/>

const ConfigIniParser = require("config-ini-parser").ConfigIniParser;
//or with import statements
//import { ConfigIniParser } from "config-ini-parser";

let p = new ConfigIniParser();
try {
    p.addSection("abc");
} catch (e) {
    if (e == ConfigIniParser.Errors.ErrorDuplicateSectionError) {
        console.error("Duplicated section");
    }
}

APIs

//create a new config ini parser instance, if the delimiter is ignore then '\n' will be used
ConfigIniParser([delimiter]);

//return parser itself
.addSection(sectionName);

//return the option value
.get(sectionName, optionName[, defaultValue]) ;

//return the option value from default section
.getOptionFromDefaultSection(optionName[, defaultValue]);

//return option value and convert to boolean
.getBoolean(sectionName, optionName);

//return option value and convert to boolean from the default section
.getBooleanFromDefaultSection(optionName);

//return option value and converted to number
.getNumber(sectionName, optionName);

//return value and converted to number from default section
.getNumberFromDefaultSection(optionName);

//return boolean
.isHaveSection(sectionName);

//return boolean
.isHaveOption(sectionName, optionName);

//return boolean
.isHaveOptionInDefaultSection(optionName);

//return all the items in the specify section as [[optionName, optionValue]]
.items(sectionName);

//return all the option names under a specify section into an array
.options(sectionName);

//parse a ini content
.parse(iniContent);

//remove a specify option from the section if it exist and successful removed then return true, if not exist then return false
.removeOption(sectionName, optionName);

//remove a specify option from the default section if it exist and successful removed then return true, if not exist then return false
.removeOptionFromDefaultSection(optionName);

//remove a specify section if it exist and successful removed then return true, if not exist then return false
.removeSection(sectionName);

//return all the section names into an array
.sections();

//set the value of the option in a given section, if the option is not exist then it will be added, if the section is not exist then exception will be raise
.set(sectionName, optionName, value);

//set the option to the given value in the default section. if the option is not exit then it will be added.
.setOptionInDefaultSection(optionName, value);

//convert back the configuration content into delimiter separated string, if delimiter is
//ignore then '\n' will be used
.stringify([delimiter]);

Error types

Defined several kinds of built-in error types

ConfigIniParser.Errors.Error;
ConfigIniParser.Errors.ErrorNoSection;
ConfigIniParser.Errors.ErrorNoOption;
ConfigIniParser.Errors.ErrorDuplicateSectionError;
ConfigIniParser.Errors.ErrorCallParseMultipleTimes;
ConfigIniParser.Errors.ErrorIncorrectArgumentType;

license

GPL-3.0

config-ini's People

Contributors

dcermak avatar dependabot[bot] avatar mftrips avatar qwerchuan avatar shangerxin avatar simon-laux avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

config-ini's Issues

"This expression is not constructable" error when consuming the ConfigIniParser from typescript

I have tried to fix the typescript declarations in #11, but apparently the export in:

declare const _exported: { ConfigIniParser: ConfigIniParser }

is not correct. When I try to use the class in typescript code:

import { ConfigIniParser } from "config-ini-parser";
const parser = new ConfigIniParser();

then typescript complains with:

src/account.ts:96:22 - error TS2351: This expression is not constructable.
  Type 'ConfigIniParser' has no construct signatures.

96   const parser = new ConfigIniParser();
                        ~~~~~~~~~~~~~~~

Unfortunately I have been unable to solve this myself. If anyone has an idea, any pointers would be greatly appreciated.

Two points ":" causing error

I have ini file

[FOLDERS]
DATA_ROOT_FOLDER=C:\wamp64\www\MyElectronAngularWorksFinal

When I do

parser.get('FOLDERS','DATA_ROOT_FOLDER')

I got [ConfigIniParser Error]: The specify option not found

When I remove the : string it works

ConfigIniParser Error: The specify option not found

I'm getting an error on the line:

var ConfigIniParser = require("config-ini-parser").ConfigIniParser;
ConfigIniParser Error: The specify option not found
    at /home/ericiam/jbconnect-test/node_modules/config-ini-parser/config-ini.js:21:39
    at Object.<anonymous> (/home/ericiam/jbconnect-test/node_modules/config-ini-parser/config-ini.js:423:2)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/home/theuser/jbconnect-test/workflows/primer3.end.js:12:17

node v10.x

Can't use false, 0 or '' as default value.

If I use any of those values as the third parameter for the get method, it throws errorNoOption.
Maybe you should replace "if (defaultValue)" with "if (defaultValue === undefined)".

Typings for default section

I'm trying out the library with Typescript. It seems like accessing the default section requires one to pass null to methods like items() and get(). Maybe the type definition needs to include null as well?

image

trim issue

Hello, could you change this line:

value = line.substr(assignPosition + assignIdentifierLength);

to

value = line.substr(assignPosition + assignIdentifierLength).trim();

Otherwise, it's keeping spaces in values. Thank you for the package.

Error parsing section header with spaces

I'm trying to parse a php.ini file but the package is throwing an error when it reaches section headers with whitespace between the characters. For example, the section header "[CLI Server]" will throw an error.

The _sectionRegex regex expression does not cover this case. Setting the _sectionRegex variable to /^[([^\]\r\n]+)]/ has solved this problem for me.

valid config gives error

hello, there is an issue with the module

[SMS_DC_Countries]
1=[ "1", "2" ]
61=[ "1", "2" ]
44=[ "1", "2", "16" ]
31=[ "16"]
32=[ "16"] ;; <-- this gives error {name: "ConfigIniParser Error", message: "Found duplicated section in the given ini file"} what is not section but value
353=[ "1", "2", "16" ]
420=[ "1", "2", "16" ]
421=[ "1", "2", "16" ]

please help

Found duplicated section in the given ini file

I have my ini file which I created manually containing only this
[DB]

const iniContent = fs.readFileSync(filePath, 'utf-8'); console.log(iniContent); //this gives [DB]
parser.parse(iniContent);

This gives me error: ConfigIniParser Error Found duplicated section in the given ini file

How is it possible ? I've tryied many delimitters (I am on windows) like \n or \r\n

EDIT:

It's my fault I've called .parse method two times sorry

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.