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 = const myINI = 'cool=awesome'myINIconst giveItBack = myINI
Class Methods:
constructor(,{}) Create ini object
str
,- optional:
defSec
= 'DEFAULT_SECTION!', - optional:
eol
= require('os').EOL) - optional:
autoTrim
= true
// You can start with an empty INIconst myINI = '' // By default the EOL is the taken from the OS, set it if neededconst myINI = ''eol: '\r\n' // DebSec is needed to represent the first lines that may not be under a section// By default it's: [DEFAULT_SECTION!] , if for some reason you actually use this name for a section, provide another to avoid unwanted behaviorconst myINI = ''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
// To get true representation use without any parameterconst newINIString = myINI
createSimpleObject
()
Make an Object including only they types that are specified
- includeTypes = [4] supported types
// by passing [3,4] , will also create a level for sectionsconst newINIString = myINI
getKeyIfExists
()
Get line object by referance {type, key, val}
- inputKey
// Changing the returned object will change the originalmyINIval = 'ouch'// Returnes null if failed to find
findAndChangeKeyIfExists
()
finds a pair by key and replace value
- inputKey,
- inputValue = ''
// Same as using getKeyIfExists but without returning referancemyINI// Returnes true for success, otherwize false
findAndRemoveKeyIfExists
()
remove key value pair from object
- inputKey
// Remove a pair (key=value) by matching the key with input stringmyINI// Returnes true for success, otherwize false
removeEverythingButSections
()
remove all other sections
- sections = []
- partialMatch = false
// wanting to remove everything elsemyINI// can also be ussed for partial matachesmyINI
findAndRemoveSectionIfExists
()
remove entire section from object
- sectionName
- partialMatch = false
// Remove an entire sectionmyINI// Returnes true for success, otherwize false
putStringInSection
()
adds a line to the end of a section
- string,
- sectionName = this.defSecName
// If section does not exist, will create it at the endmyINI// If the input is a key=value pair, and key exists in section , will change its value// Also true when no section is provided (will use default one)
getLinesByMatch
()
find all lines containing a string
- token
// Will return an array with with all matches across all sectionsmyINI
removeLineByMatch
()
matches keys, values or comments
- token,
- global = false,
- _done = false (internal use)
// Will return true if at least one line was removed, else falsemyINI
findAndReplace
()
searches for the token and replaces with the value if found
- token,
- value = '',
- global = false,
- _done = false (internal use)
// if global is false will change only the first occurrencemyINI// Will return true if at least one line was removed, else false
solveDuplicates
()
fixes ini object so and removes duplicate keys leaving first or last occurence
- preferFirstOccurrence = false
// Will remove duplicate keys across the entire ini objectmyINI// Returnes true when finished
solveSelfReferences
()
use values to replace matching content wrapped by a given prefix and suffix
- prefix
- suffix
/* super_secret_config.ini: something=whatever say=ha dude=%say%%say%%say% %relax% relax=%something% */ myINI /* ==> something=whatever say=ha dude=hahaha whatever relax=whatever */
mergeWith
()
merges with another ini object
- anotherINIObject
- before = false
// If before is true will place new values at the beginning of each sectionmyINI
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 = const INI = const productConfig = fs let includeswhile includes = productConfig if includeslength == 0 break productConfig for const include of includes const includePath = include1 const tempINI = fs productConfig productConfigconst finalConfig = productConfigfs
ini template proccessing
const fs = const INI = const webCon = fs// latest=GGEZ.v69.zip// download-url=<<SubDomain>>.<<BaseDomain>>/download/%latest%webConwebConwebConwebConconst upCon = webConfs
Author
License
GNU General Public License v3.0
Acknowledgments
- stackOverflow
- coffee
- my cats