-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
516 lines (442 loc) · 11.8 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
'use strict';
import { EOL } from 'os';
import {compose, serialize} from 'yaml-js';
import {load, dump} from 'js-yaml';
import {
isArray,
isString,
isObject,
isUndefined,
isNull,
isNumber,
isEqual,
repeat,
each,
includes,
last
} from 'lodash';
import YAWNError from './error.js';
const NULL_TAG = 'tag:yaml.org,2002:null';
const STR_TAG = 'tag:yaml.org,2002:str';
const INT_TAG = 'tag:yaml.org,2002:int';
const FLOAT_TAG = 'tag:yaml.org,2002:float';
const MAP_TAG = 'tag:yaml.org,2002:map';
const SEQ_TAG = 'tag:yaml.org,2002:seq';
const SPACE = ' ';
const DASH = '-';
// export default class YAWN {
export default class YAWN {
constructor(str) {
if (!isString(str)) {
throw new TypeError('str should be a string');
}
this.yaml = str;
}
get json() {
return load(this.yaml);
}
set json(newJson) {
// if json is not changed do nothing
if (isEqual(this.json, newJson)) {
return;
}
const ast = compose(this.yaml);
if (isUndefined(newJson)) {
this.yaml = '';
return;
}
// -------------------------------------------------------------------------
// check if entire json is changed
// -------------------------------------------------------------------------
let newTag = getTag(newJson);
if (ast.tag !== newTag) {
let newYaml = cleanDump(newJson);
// replace this.yaml value from start to end mark with newYaml if node is
// primitive
if (!isObject(newJson)) {
this.yaml = replacePrimitive(ast, newYaml, this.yaml, 0);
// if node is not primitive
} else {
this.yaml = replaceNode(ast, newYaml, this.yaml, 0);
}
return;
}
// -------------------------------------------------------------------------
// NULL_TAG, STR_TAG, INT_TAG, FLOAT_TAG
// -------------------------------------------------------------------------
if (includes([NULL_TAG, STR_TAG, INT_TAG, FLOAT_TAG], ast.tag)) {
this.yaml = replacePrimitive(ast, newJson, this.yaml, 0);
return;
}
// -------------------------------------------------------------------------
// MAP_TAG
// -------------------------------------------------------------------------
if (ast.tag === MAP_TAG) {
let json = this.json;
this.yaml = updateMap(ast, newJson, json, this.yaml, 0);
}
// -------------------------------------------------------------------------
// SEQ_TAG
// -------------------------------------------------------------------------
if (ast.tag === SEQ_TAG) {
this.yaml = updateSeq(ast, newJson, this.yaml, 0);
}
// Trim trailing whitespaces
this.yaml = this.yaml
.split(EOL)
.map(line=> line.replace(/[ \t]+$/, ''))
.join(EOL);
}
toString() {
return this.yaml;
}
toJSON() {
return this.json;
}
getRemark(path) {
const ast = compose(this.yaml);
let pathlist = path.split('.');
let node = getNode(ast, pathlist);
return node && getNodeRemark(node, this.yaml);
}
setRemark(path, remark) {
const ast = compose(this.yaml);
let pathlist = path.split('.');
let node = getNode(ast, pathlist);
return !!node && !!(this.yaml = setNodeRemark(node, remark, this.yaml));
}
}
/*
* Determines the AST tag of a JSON object
*
* @param {any} - json
* @returns {boolean}
* @throws {YAWNError} - if json has weird type
*/
function getTag(json) {
let tag = null;
if (isArray(json)) {
tag = SEQ_TAG;
} else if (isObject(json)) {
tag = MAP_TAG;
} else if (isNull(json)) {
tag = NULL_TAG;
} else if (isNumber(json)) {
if (json % 10 === 0) {
tag = INT_TAG;
} else {
tag = FLOAT_TAG;
}
} else if (isString(json)) {
tag = STR_TAG;
} else {
throw new YAWNError('Unknown type');
}
return tag;
}
/*
* Update a sequence with new JSON
*
* @param {Node} ast
* @param {object} newJson
* @param {string} yaml
*
* @returns {string}
*
*/
function updateSeq(ast, newJson, yaml, offset) {
let values = load(serialize(ast));
let min = Math.min(values.length, newJson.length);
for (let i = 0; i < min; i++) {
const newYaml = changeArrayElement(ast.value[i], cleanDump(newJson[i]), yaml, offset);
offset = offset + newYaml.length - yaml.length;
yaml = newYaml;
}
if (values.length > min) {
for (let i = min; i < values.length; i++) {
const newYaml = removeArrayElement(ast.value[i], yaml, offset);
offset = offset + newYaml.length - yaml.length;
yaml = newYaml;
}
} else if (newJson.length > min) {
yaml = insertAfterNode(ast, cleanDump(newJson.slice(min)), yaml, offset);
}
return yaml;
}
/*
* update a map structure with new values
*
* @param {AST} ast - a map AST
* @param {any} newJson
* @param {any} - json
* @param {string} yaml
* @returns {boolean}
* @throws {YAWNError} - if json has weird type
*/
function updateMap(ast, newJson, json, yaml, offset) {
// look for changes
each(ast.value, pair => {
let [keyNode, valNode] = pair;
// node is deleted
if (isUndefined(newJson[keyNode.value])) {
// TODO: can we use of the methods below?
const newYaml = yaml.substr(0, keyNode.start_mark.pointer + offset) +
yaml.substring(getNodeEndMark(valNode).pointer + offset);
offset = offset + newYaml.length - yaml.length;
yaml = newYaml;
return;
}
let value = json[keyNode.value];
let newValue = newJson[keyNode.value];
// primitive value has changed
if (newValue !== value && !isArray(valNode.value)) {
// replace the value node
const newYaml = replacePrimitive(valNode, newValue, yaml, offset);
offset = offset + newYaml.length - yaml.length;
yaml = newYaml;
// remove the key/value from newJson so it's not detected as new pair in
// later code
delete newJson[keyNode.value];
}
// non primitive value has changed
if (!isEqual(newValue, value) && isArray(valNode.value)) {
// array value has changed
if (isArray(newValue)) {
// recurse
const newYaml = updateSeq(valNode, newValue, yaml, offset);
offset = offset + newYaml.length - yaml.length;
yaml = newYaml;
// map value has changed
} else {
// recurse
const newYaml = updateMap(valNode, newValue, value, yaml, offset);
offset = offset + newYaml.length - yaml.length;
yaml = newYaml;
// ast = compose(yaml);
// remove the key/value from newJson so it's not detected as new pair in
// later code
delete newJson[keyNode.value];
}
}
});
// look for new items to add
each(newJson, (value, key)=> {
// item is new
if (isUndefined(json[key])) {
let newValue = cleanDump({[key]: value});
const newYaml = insertAfterNode(ast, newValue, yaml, offset);
offset = offset + newYaml.length - yaml.length;
yaml = newYaml;
}
});
return yaml;
}
/*
* Place value in node range in yaml string
*
* @param node {Node}
* @param value {string}
* @param yaml {string}
*
* @returns {string}
*/
function replacePrimitive(node, value, yaml, offset) {
return yaml.substr(0, node.start_mark.pointer + offset) +
JSON.stringify(value) +
yaml.substring(node.end_mark.pointer + offset);
}
/*
* Place value in node range in yaml string
*
* @param node {Node}
* @param value {string}
* @param yaml {string}
*
* @returns {string}
*/
function replaceNode(node, value, yaml, offset) {
let indentedValue = indent(value, node.start_mark.column);
let lineStart = node.start_mark.pointer - node.start_mark.column + offset;
return yaml.substr(0, lineStart) +
indentedValue +
yaml.substring(getNodeEndMark(node).pointer + offset);
}
/*
* Place value after node range in yaml string
*
* @param node {Node}
* @param value {string}
* @param yaml {string}
*
* @returns {string}
*/
function insertAfterNode(node, value, yaml, offset) {
let indentedValue = indent(value, node.start_mark.column);
return yaml.substr(0, getNodeEndMark(node).pointer + offset) +
EOL +
indentedValue +
yaml.substring(getNodeEndMark(node).pointer + offset);
}
/*
* Removes a node from array
*
* @param {Node} node
* @param {string} yaml
*
* @returns {string}
*/
function removeArrayElement(node, yaml, offset) {
let index = node.start_mark.pointer - node.start_mark.column - 1 + offset;
return yaml.substr(0, index) +
yaml.substring(getNodeEndMark(node).pointer + offset);
}
/*
* Changes a node from array
*
* @param {Node} node
* @param value {string}
* @param {string} yaml
*
* @returns {string}
*/
function changeArrayElement(node, value, yaml, offset) {
let indentedValue = indent(value, node.start_mark.column);
// find index of DASH(`-`) character for this array
let index = node.start_mark.pointer + offset;
while (index > 0 && yaml[index] !== DASH) {
index--;
}
return yaml.substr(0, index + 2) +
indentedValue.substr(node.start_mark.column) +
yaml.substring(getNodeEndMark(node).pointer + offset);
}
/*
* Gets end mark of an AST
*
* @param {Node} ast
*
* @returns {Mark}
*/
function getNodeEndMark(ast) {
if (isArray(ast.value) && ast.value.length) {
let lastItem = last(ast.value);
if (isArray(lastItem) && lastItem.length) {
return getNodeEndMark(last(lastItem));
}
return getNodeEndMark(lastItem);
}
return ast.end_mark;
}
/*
* Indents a string with number of characters
*
* @param {string} str
* @param {integer} depth - can be negative also
*
* @returns {string}
*/
function indent(str, depth) {
return str
.split(EOL)
.filter(line => line)
.map(line => repeat(SPACE, depth) + line)
.join(EOL);
}
/*
* Dump a value to YAML sting without the trailing new line
*
* @param {any} value
*
* @returns {string}
*
*/
function cleanDump(value) {
if (value === null || isString(value) || isNumber(value)) {
return value;
}
let yaml = dump(value).replace(/\n$/, '');
if (EOL !== '\n') {
yaml = yaml.replace(/\n/g, EOL);
}
return yaml;
}
/*
* Gets remark of an AST
*
* @param {Node} ast
* @param {string} yaml
*
* @returns {string}
*/
function getNodeRemark(ast, yaml) {
let index = getNodeEndMark(ast).pointer;
while (index < yaml.length && yaml[index] !== '#' && yaml[index] !== EOL) {
++index;
}
if (EOL === yaml[index] || index === yaml.length) {
return '';
} else {
while (index < yaml.length && (yaml[index] === '#' || yaml[index] === ' ')) {
++index;
}
let end = index;
while (end < yaml.length && yaml[end] !== EOL) {
++end;
}
return yaml.substring(index, end);
}
}
/*
* Sets remark of an AST
*
* @param {Node} ast
* @param {string} remark
* @param {string} yaml
*
* @returns {boolean}
*/
function setNodeRemark(ast, remark, yaml) {
let index = getNodeEndMark(ast).pointer;
while (index < yaml.length && yaml[index] !== '#' && yaml[index] !== EOL) {
++index;
}
if (EOL === yaml[index] || index === yaml.length) {
return yaml.substr(0, index) + ' # ' + remark +
yaml.substring(index);
} else {
while (index < yaml.length && (yaml[index] === '#' || yaml[index] === ' ')) {
++index;
}
let end = index;
while (end < yaml.length && yaml[end] !== EOL) {
++end;
}
return yaml.substr(0, index) + remark +
yaml.substring(end);
}
}
/*
* Gets node of an AST which path
*
* @param {Node} ast
* @param {array} path
*
* @returns {Node}
*/
function getNode(ast, path) {
if (path.length) {
if (ast.tag === MAP_TAG) {
let value = ast.value;
for (let i = 0; i < value.length; ++i) {
let [keyNode, valNode] = value[i];
if (path[0] === keyNode.value) {
return getNode(valNode, path.slice(1));
}
}
return undefined;
} else if (ast.tag === SEQ_TAG) {
return ast.value[path[0]] && getNode(ast.value[path[0]], path.slice(1));
}
}
return ast;
}