-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
70 lines (58 loc) · 1.75 KB
/
index.js
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
var _ = require('lodash'),
stringifyObject = require('stringify-object'),
grammar = require('./lib/grammar'),
options = require('./lib/options');
module.exports = function(_source, _options){
_options = options(_options);
var contents,
json,
expr = '__ALLOY_EXPR__--',
tiRegex = new RegExp('^' + expr + 'Ti\.'),
titaniumRegex = new RegExp('^' + expr + 'Titanium\.'),
exprRegex = new RegExp(_options.quote + expr + '(.+?)' + _options.quote, 'g');
contents = /^\s*\{[\s\S]+\}\s*$/gi.test(_source) ? _source : '{' + _source + '}';
contents = contents.replace(/(\s)(\\+)(\s)/g, '$1$2$2$3');
try {
json = grammar.parse(contents);
} catch (e) {
throw e.message + ' line: ' + e.line + ', column: ' + e.column + ', offset: ' + e.offset;
}
_.each(json, function(node, name){
var ordered = [],
result = {},
length = _options.order.length;
_.each(node, function(value, key){
var index = _.indexOf(_options.order, key);
if (_.isString(value)) {
if (_options.titanium_shorthand) {
value = value.replace(titaniumRegex, expr + 'Ti.');
} else {
value = value.replace(tiRegex, expr + 'Titanium.');
}
}
if (index > -1) {
ordered[index] = {
key: key,
value: value
};
} else {
ordered[length] = {
key: key,
value: value
};
length++;
}
});
_.each(_.without(ordered, undefined), function(node){
result[node.key] = node.value;
});
json[name] = result;
});
return _.map(json, function(node, name){
return _options.quote + name + _options.quote + ': ' +
stringifyObject(node, {
indent: _options.indent,
singleQuotes: _options.quote === '\'' ? true : false
});
}).join(_options.concatenation_comma ? ',\n' : '\n').replace(exprRegex, '$1') + _options.eof;
};