Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
node-ini-parser/index.js /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
38 lines (30 sloc)
768 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var REG_GROUP = /^\s*\[(.+?)\]\s*$/ | |
| var REG_PROP = /^\s*([^#].*?)\s*=\s*(.*?)\s*$/ | |
| function parse(string){ | |
| var object = {} | |
| var lines = string.split('\n') | |
| var group | |
| var match | |
| for(var i = 0, len = lines.length; i !== len; i++){ | |
| if(match = lines[i].match(REG_GROUP)) | |
| object[match[1]] = group = object[match[1]] || {}; | |
| else if(group && (match = lines[i].match(REG_PROP))) | |
| group[match[1]] = match[2]; | |
| } | |
| return object; | |
| } | |
| function parseFile(file, callback){ | |
| fs.readFile(file, 'utf-8', function(error, data){ | |
| if(error) | |
| return callback(error); | |
| callback(null, parse(data)) | |
| }) | |
| } | |
| function parseFileSync(file){ | |
| return parse(fs.readFileSync(file, 'utf-8')) | |
| } | |
| module.exports = { | |
| parse: parse, | |
| parseFile: parseFile, | |
| parseFileSync: parseFileSync | |
| } |