0.0.2 • Published 10 years ago
prop-ini v0.0.2
PropINI
Read, Write, Encode, Decode & Manage INI/Property files with ease.
Install
npm install prop-ini
Usage
1] Require the module in your code
var PropINI = require("prop-ini");2] Create new instance for PropINI
var piTheOne = PropIni.createInstance();3] Decode/Read data
var iniData = piTheOne.decode(<config>);<config> is an object with following props
config.data- Pass the INI/Properties format data directly as a stringconfig.file- Tell the function to get the data from this fileconfig.charset- Tell function to read file by this encodingconfig.lineSeparator- Tell function to split lines based on this char
The returned iniData is an object with following props
iniData.sections- Object containing sections as top-level nodes and properties of appropriate sections as child nodes for them._globalis a special section where properties without any section are availableiniData.sectionOrder- Array of section names in exact order matching the parsed data._globalwill be the first entry
On successfull decode the parsed values will be stored with the PropINI instance as properties. You may access them using piTheOne.sections and piThOne.sectionOrder. Handle with care !
4] Encode/Write data
var iniString = piTheOne.encode(<config>);<config> is an object with following props
config.sortSections- Sort section names usingString.prototype.localeCompareconfig.sortKeys- Sort properties/keys under each section withlocaleCompareconfig.file- Tell function write the encoded data to this fileconfig.charset- Tell function to write file with this encodingconfig.lineSeparator- Tell function to join lines based on this char
The returned iniString is a string type variable containg INI/Property file format data
Other convenience functions (After parsing data ...)
1] Get section names as an Array
var sections = piTheOne.getSections();sectionsis an Array containing all sections in the parsed instance data_globalis excluded- Parsed section order is maintained
2] Get anything you want !
var data = piTheOne.getData(<section>, <key>);sectionis the name of the section. In absense ofkeywhole section's object with child properties will be returnedkeyis the name of the property under some section. In absense ofsection,_globalsection will be used. String value of a key will be returned- In absense of both
section&key, whole data will be returned as an object withsections,sectionOrderas properties
3] Set anything you want !
var success = piTheOne.addData(<value>, <section>, <key>);sectionis the name of the section.valueshould be given as an object in this casekeyis the name of the property under some section. In absense ofsection,_globalsection will be used.valueshould be of String type in this case- In absense of both
section&key, you can set the whole data for the instance. An object withsections,sectionOrderas properties should be given forvalue.sectionsproperty should have_globalchild property.
4] Remove anything you want !
var success = piTheOne.removeData(<section>, <key>);sectionis the name of the section. In absense ofkeywhole section will be removed.keyis the name of the property under some section. In absense ofsection,_globalsection will be used.- In absense of both
section&key, whole instance data will be reset to empty state.
And, one more thing ...
- Since the data is exposed as instance properties you can directly process them as you wish. But please respect the format PropINI expects the data to be in.
piTheOne.sectionsshould always be an objectpiTheOne.sectionOrdershould always be an array and should be in sync withpiTheOne.sectionsdatapiTheOne.sectionsshould always have a child object_global
LEGAL STUFF
- Check project's license info