Repository navigation
Expand file tree
/
Copy pathparser.js
More file actions
75 lines (60 loc) · 1.61 KB
/
Copy pathparser.js
File metadata and controls
75 lines (60 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use strict';
/**
* Automatic parser interface for configuration changes.
*
* @constructor
* @param {Configuration} config The parent configuration instance
* @api private
*/
function Parser(config) {
if (!(this instanceof Parser)) return new Parser(config);
this.config = config;
this.config.on('read', function read(type) {
this[type]();
}.bind(this));
}
/**
* Parse files with a `.json` extension.
*
* @returns {Parser}
*/
Parser.prototype.json = function json() {
this.config.data = JSON.parse(this.config.source);
return this;
};
/**
* Parses files with a `.cfg` extension.
*
* @returns {Parser}
*/
Parser.prototype.cfg = function cfg() {
var has = this.config.definitions.has
, data = this.config.source
, section
, current
, name
, hash
, key;
data.split(/\n/).forEach(function parse(line) {
line = line.trim();
// No content on line just return.
if (!line.length) return;
// Keep track of the section.
section = this.config.definitions.sections.filter(has.bind(this, line))[0];
if (section) {
name = line.split(' ')[1]; // Named section support.
return current = section;
}
// Check content against known keys in current section.
key = this.config.definitions.allowed[current].filter(has.bind(this, line))[0];
if (!key || !key.length) return;
// Store the value if we got a key match, and add the comment (if present).
hash = line.split('#');
this.config[current](name).add(key, hash[0].substr(key.length).trim())(hash[1]);
}, this);
return this;
};
//
// Expose the module.
//
module.exports = Parser;