Easy-INI - a simple way to manipulate INI files content
Table of Contents
Getting Started
why should you use this package?
- You don't want to manipulate an object and just use stright and forward methods (you can if you insist)
- You care about preserving comments
- You want easy formatting and cool logic like merge and replace
basic example
const INI = require('easy-ini')
const myINI = new INI('cool=awesome')
myINI.putStringInSection('; so important','[WOW]')
const giveItBack = myINI.createINIString()
Class Methods:
constructor(,{})
Create ini object
str
,- optional:
defSec
= 'DEFAULT_SECTION!', - optional:
eol
= require('os').EOL) - optional:
autoTrim
= true
const myINI = new INI('')
const myINI = new INI('',{eol: '\r\n'})
const myINI = new INI('',{defSec: 'NEXT_LEVEL_DEFAULT_SECTION'})
createINIString
({})
Make an ini string from the object, every param is optional and do what it says
- shouldTrim = false,
- shouldFix = false,
- cleanHashComments = false,
- cleanBreakComment = false,
- cleanEmptyLines = false,
- noEmptySections = false,
- noSections = false
const newINIString = myINI.createINIString()
createSimpleObject
()
Make an Object including only they types that are specified
const newINIString = myINI.createSimpleObject()
getKeyIfExists
()
Get line object by referance {type, key, val}
myINI.getKeyIfExists('cool').val = 'ouch'
findAndChangeKeyIfExists
()
finds a pair by key and replace value
myINI.findAndChangeKeyIfExists('cool','OUCH')
findAndRemoveKeyIfExists
()
remove key value pair from object
myINI.findAndRemoveKeyIfExists('cool')
removeEverythingButSections
()
remove all other sections
- sections = []
- partialMatch = false
myINI.removeEverythingButSections(['[GLOBALS]'])
myINI.removeEverythingButSections(['GLOB'], true)
findAndRemoveSectionIfExists
()
remove entire section from object
- sectionName
- partialMatch = false
myINI.findAndRemoveSectionIfExists('[DO_NOT_REMOVE]')
putStringInSection
()
adds a line to the end of a section
- string,
- sectionName = this.defSecName
myINI.putStringInSection('#comment','[BLAHBLAH]')
getLinesByMatch
()
find all lines containing a string
myINI.getLinesByMatch('#INCLUDE=')
removeLineByMatch
()
matches keys, values or comments
- token,
- global = false,
- _done = false (internal use)
myINI.removeLineByMatch(';DUAH', true)
findAndReplace
()
searches for the token and replaces with the value if found
- token,
- value = '',
- global = false,
- _done = false (internal use)
myINI.findAndReplace('<<BASE_DOMAIN>>', 'mashu-mashu-mashu.com', true)
solveDuplicates
()
fixes ini object so and removes duplicate keys leaving first or last occurence
- preferFirstOccurrence = false
myINI.solveDuplicates()
solveSelfReferences
()
use values to replace matching content wrapped by a given prefix and suffix
myINI.solveSelfReferences('%', '%')
mergeWith
()
merges with another ini object
- anotherINIObject
- before = false
myINI.mergeWith(notMyINI)
Good To Know:
- the INI class accepets a string input for the constructor ( not a path to an ini file )
- the default section is a representation for the first lines that are not under any section (could be the whole file)
- considers text only lines as garbage
- You can edit
myINI.iniData
directly - line types:
- 0: empty line
- 1: hash comment
- 2: break comment
- 3: section
- 4: pair
- 5: garbage
Use Case Examples:
handling ini dependency
const fs = require('fs')
const INI = require('easy-ini')
const productConfig = new INI(fs.readFileSync('./amazing_app_info.ini',{encoding: 'utf8'}))
let includes
while (includes = productConfig.getLinesByMatch("#INCLUDE")){
if (includes.length == 0) {break}
productConfig.removeLineByMatch('#INCLUDE', true)
for (const include of includes.reverse()) {
const includePath = include.split('=')[1]
const tempINI = new INI(fs.readFileSync(includePath, {encoding: 'utf8'}))
productConfig.mergeWith(tempINI, true)
}
}
productConfig.solveDuplicates()
const finalConfig = productConfig.createINIString()
fs.writeFileSync("./final.ini", finalConfig)
ini template proccessing
const fs = require('fs')
const INI = require('easy-ini')
const webCon = new INI(fs.readFileSync('./website_config.ini',{encoding: 'utf8'}))
webCon.findAndReplace('<<BaseDomain>>', 'easy-ini.com', true)
webCon.findAndReplace('<<SubDomain>>', 'download', true)
webCon.findAndReplace('<<Author>>', 'Gal Angel', true)
webCon.solveSelfReferences('%', '%')
const upCon = webCon.createINIString()
fs.writeFileSync("./to_upload.ini", upCon)
Author
License
GNU General Public License v3.0
Acknowledgments
- stackOverflow
- coffee
- my cats