-
Notifications
You must be signed in to change notification settings - Fork 2
/
dna.js
executable file
·260 lines (222 loc) · 6.05 KB
/
dna.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* DNA - Human-readable data format
*
* Copyright (c) 2015 Ali Shakiba
* Available under the MIT license
*/
var DNA = (function config(options) {
options = options || {};
var debug = options.debug || false;
function parse(input) {
var stream = new InputStream(input);
return read(stream, 0);
}
// one-pass post-order recursive read
function read(stream, indent) {
var data = null, text = null;
debug && console.log(indent);
// next line
while (!stream.eof) {
// if line is less indented (and not empty) return to parent context
debug && console.log('?', stream.line);
if (stream.text && stream.indent < indent) {
break;
}
// consume current line
var line = stream.shift();
if (line.key) {
// use as object or array
data = data || {};
debug && console.log('.', line.key);
var value = line.value;
// if next line is more indented read it as value
if (!stream.eof) {
debug && console.log('>', stream.line);
if (stream.indent > indent) {
value = read(stream, stream.indent);
}
}
if (line.pipe == 'list') {
debug && console.log('@list');
if (!isArray(data)) {
data[line.key] = line.key in data ? [ data[line.key] ] : [];
}
}
multimapSet(data, line.key, value);
} else {
// line contains a string, it is multiline string
text = text || [];
debug && console.log('+', line.text);
text.push(line.text);
}
debug && console.log('=', data, text);
}
var result = data || (text && text.join('\n').replace(/\n$/, ''));
debug && console.log('<<', JSON.stringify(result));
return result;
}
function InputStream(input) {
if (typeof input === 'string') {
input = input.split(/\n/);
}
if (input.length) {
var array = input;
var i = 0;
input = {
hasNext : function() {
return array.length > i;
},
next : function() {
return array.length > i ? array[i++] : undefined;
}
};
}
this.shift = function() {
var first = {};
for ( var key in this) {
if ('shift' !== key && 'eof' !== key) {
first[key] = this[key];
delete this[key];
}
}
if (input.hasNext()) {
var line = input.next();
this.line = line;
var regex = /^(\s*)(.*)$/.exec(line);
this.indent = regex[1].length;
this.text = regex[2];
if (regex = /^((\w|\-)+)\:\s*(.*)/.exec(this.text)) {
// key: value
this.key = regex[1];
this.value = regex[3];
} else if (regex = /^((\w|\-)*)[@|]((\w|\-)+)\:\s*(.*)/.exec(this.text)) {
// key@pipe: value
this.key = regex[1];
this.pipe = regex[3];
this.value = regex[5];
}
this.eof = false;
} else {
this.eof = true;
}
return first;
};
this.shift();
}
function stringify(obj, replacer, space) {
if (typeof replacer === 'string') {
space = replacer;
replacer = null;
} else if (typeof replacer !== 'function') {
replacer = null;
}
if (typeof space !== 'string' || !/^\s+$/.test(space)) {
space = ' ';
}
var output = [];
var ctx = {
space : space,
replacer : replacer,
cycle : [],
mark : function(value) {
if (indexOf(ctx.cycle, value) === -1) {
ctx.cycle.push(value);
return true;
} else {
return false;
}
},
push : function(value) {
output.push(value);
}
};
if (obj === null || typeof obj === 'undefined') {
} else if (isArray(obj)) {
for (var i = 0; i < obj.length; i++) {
write(ctx, '', i + ':', obj[i]);
}
} else if (typeof obj === 'object') {
for ( var key in obj) {
write(ctx, '', key + ':', obj[key]);
}
} else {
ctx.push(obj);
}
return output.join('\n');
}
function write(ctx, space, name, value) {
if (value === null || typeof value === 'undefined') {
ctx.push(space + name);
} else if (isArray(value)) {
if (ctx.mark(value)) {
for (var i = 0; i < value.length; i++) {
write(ctx, space, name, value[i]);
}
}
} else if (typeof value === 'object') {
if (ctx.mark(value)) {
ctx.push(space + name);
for ( var key in value) {
write(ctx, space + ctx.space, key + ':', value[key]);
}
}
} else if (typeof value === 'string') {
if (value.indexOf('\n') >= 0) {
ctx.push(space + name + '\n' + space + ctx.space
+ value.replace(/\n/g, '\n' + space + ctx.space));
} else {
ctx.push(space + name + ' ' + value);
}
} else {
ctx.push(space + name + ' ' + value);
}
}
function pipe(name, data, text) {
}
return {
parse : parse,
stringify : stringify,
config : config
};
function isArray(value) {
if (!value) {
return false;
} else if (typeof Array.isArray === 'function') {
return Array.isArray(value);
} else {
return '[object Array]' === Object.prototype.toString.call(value);
}
}
function indexOf(array, item) {
if (!isArray(array)) {
return -1;
} else if (typeof array.indexOf === 'function') {
return array.indexOf(item);
} else {
for (var i = 0; i < array.length; i++) {
if (item === array[i]) {
return i;
}
}
return -1;
}
}
function multimapSet(obj, key, value) {
if (!(key in obj)) {
obj[key] = value;
} else if (!isArray(obj[key])) {
obj[key] = [ obj[key], value ];
} else {
obj[key].push(value);
}
}
})();
if (typeof module !== 'undefined' && module.exports) {
module.exports = DNA;
} else if (typeof define === 'function' && define.amd) {
define(function() {
return DNA;
});
} else if (typeof window !== 'undefined') {
window.DNA = DNA;
}