ini-processor
v1.0.0
Published
Parses INI files to json, and can convert them back to INI code.
Downloads
7
Readme
INI file processor
What this does
Lets you read INI files, get values from them, and you can generate custom INI files with this.
How its formatted when parsed
This is the INI file:
[Label Name]
; Test strings.
testString = String
testStringSpaces = String with spaces
; Test numbers.
testNumber = 1
testNumberFloat = 1.2345653234
testNumberNaN = NaN
; Test booleans.
testBoolTrue = true
testBoolFalse = false
And when pharsed, it becomes this array:
[
{ type: 'section', label: 'Label Name' },
{ type: 'comment', comment: ' Test strings.' },
{ type: 'variable', name: 'testString', value: 'String' },
{
type: 'variable',
name: 'testStringSpaces',
value: 'String with spaces'
},
{ type: 'comment', comment: ' Test numbers.' },
{ type: 'variable', name: 'testNumber', value: 1 },
{ type: 'variable', name: 'testNumberFloat', value: 1.2345653234 },
{ type: 'variable', name: 'testNumberNaN', value: NaN },
{ type: 'comment', comment: ' Test booleans.' },
{ type: 'variable', name: 'testBoolTrue', value: true },
{ type: 'variable', name: 'testBoolFalse', value: false }
]
The type represents what type of part it is of the INI. Each type has its own properties, they are described below.
Parsed formats
Section
The section type indicates a "section" of the ini. How a section looks like in INI file format: [Foo Bar]
.
Properties:
- label - The sections label.
Variable
The variable type indicates a variable of the ini. How a section looks like in INI file format: Foo=Bar
.
Properties:
- name - The variables name, before the equals sign.
- value - The variables value, after the equals sign.
Comment
The comment type indicates a comment of the ini. How a section looks like in INI file format: ; Comment
.
Properties:
- comment - The comments text.
Newline
The \n
newline character, used to create spacings between value names.
Must have ignoreNewLines set to false on options on the "from" function.
Properties: none
API
Basic API usage API. If you want to view the full API, check the source code.
from (String containing INI code, options)
This converts the provided INI string to a parsed JSON array, formatted like shown in the "How its formatted when parsed" section above. Provide a JSON object to options to pass in custom arguments to change how stuff is parsed.
Options:
- convertValues - Default: true - Convert variable value from strings to numbers, floats, booleans, etc. If possible.
- ignoreComments - Default: false - If true, ignore the
; Comment label
part of the INI string, don't add comments to the returned array. - ignoreNewLines - Default: true - If true, ignore adding newline objects to the array. Usefull for making sure when you convert it back to INI string (using the to function), its formatted close as possible to its read string version.
Returns: array
to (Parsed INI array, Experimental - Extra Pretty Code)
This converts the JSON array returned from the "from" function, back into an array. If "Experimental - Extra Pretty Code" is true, then this adds a spacing between variables and comments when sections are used. Like this: ``````
Returns: INI file string
readValue (Parsed INI array, Variable Name)
This returns the value of the Variable Name if found. Otherwise, it returns undefined.
Returns: Value of provided variable name
readValueFromSection (Parsed INI array, Variable Name, Section Name)
Like above, but with Section Name provided. This returns the value of the Variable Name in the specified Section Name if found. Otherwise, it returns undefined.
Returns: Value of provided variable name from Section Name
setValue (Parsed INI array, Variable Name, New variable value)
This sets the variable value to the specified new variable value.
Returns: nothing
setValueFromSection (Parsed INI array, Variable Name, Section Name, New variable value)
Like above, but with Section Name provided. This sets the variable value to the specified New Variable Value inside the specified Section Name.
This sets the variable value to the specified new variable value. Returns: nothing