'use strict' /* this is some crazy messy code. don't judge me */ const jsonPath = require('jsonpath'); const obj = require('./data/plotto.json'); const _ = require('underscore'); /* let added = obj.conflicts; for(let ck of Object.keys(obj.conflicts)) { let conflict = obj.conflicts[ck]; let rg = /\[(\d+)\]/g; let idxs = []; let m; while(m = rg.exec(conflict.description)) { idxs.push({ index1: m.index, index2: m.index + m[1].length + 2, name: ck + '-' + m[1] }); } if(idxs.length == 0){ continue; } delete obj.conflicts[ck]; //console.log(ck, ':', conflict.description); let prefix = conflict.description.slice(0, idxs[0].index1); for(let i=0; i 1) { return { op:'+', v:addOp}; } return idstr; })); repl.push(refNodes); prevStart = m.index + m[0].length + 1; } while((m = rg.exec(item.description))); item.description = repl; //console.log(item.conflictid, ':', desc); //console.log('===', JSON.stringify(repl)); } function expand(item) { if(typeof(item) == "string") { item = fixToCh(item); } if(typeof(item) == "string") { let ret = item; let rg = /^(\d+[a-z]?)\s+(\**)-(\**)/g; let m = rg.exec(item); if(m) { ret = { v: m[1] }; if(m[0].length < item.length) { ret.v = ret.v + ' ' + item.slice(m[0].length).trim(); } ret.v = ret.v.trim(); ret.start = m[2]; ret.end = m[3]; //console.log(item, ' ---> ', JSON.stringify(ret)); } let k = ret.v || ret; if(!obj.conflicts[k]) { let mm = k.match(/^\d+[a-z]? /); let d = ''; if(mm) { d = obj.conflicts[mm[0].trim()]; if(d) { d = d.description; } } mm = _.filter(allKeys, function(ak){ let rg = new RegExp('^'+k+'(?:(?:[a-z]?-\\d+)|[a-z])$','g'); return rg.test(ak); }); if(mm && mm.length > 0) { let ret2 = {}; if(ret !== item) { ret2 = { op: '?', v: _.map(mm, function(x, y) { return Object.assign({}, ret, {v: x}); }) }; } else { ret2 = { op: '?', v: mm }; } if(ret2.length == 1){ ret2 = ret2[0]; } //console.log('OUTLIER!!', stk.join('/'), '[' + k + ']:', item, ' ---> ', JSON.stringify(ret), ' ____ ', JSON.stringify(ret2)); ret = ret2; } else { console.log('!!!!!OUTLIER!!', stk.join('/'), '[' + k + ']:', item, ' ---> ', JSON.stringify(ret), ' ____ ', d); } //console.log('"' + k + '",'); } return ret; } else if(Array.isArray(item)) { return item.map(expand); } else if(item.v) { item.v = expand(item.v); } return item; } function fixToCh(tmpl) { let orig = tmpl; let didChange = false; let ret = { v: tmpl }; let ch = /\s+ch (?:(?:[a-zA-Z0-9-]+|“[^”]+”) to (?:[a-zA-Z0-9-]+|“[^”]+”))(?:(?: &|,| and) (?:[a-zA-Z0-9-]+|“[^”]+”) to (?:[a-zA-Z0-9-]+|“[^”]+”))*(?: &|,| and)?/; let m = ch.exec(tmpl); if(m) { let val = []; val.push(tmpl.slice(0, m.index)); didChange = true; let tfm = ret.tfm = ret.tfm|| {}; let dat = m[0]; let ch2 = /(?:([a-zA-Z0-9-]+)|“([^”]+)”) to (?:([a-zA-Z0-9-]+)|“([^”]+)”)/g; let m2; while(m2 = ch2.exec(dat)) { tfm[(m2[1] || m2[2]).trim()] = (m2[3] || m2[4]).trim(); } val.push(tmpl.slice(m.index + m[0].length)); ret.v = val.join(' ').trim(); tmpl = ret.v; } let tr = / tr ([A-Z-0-9]+) & ([A-Z-0-9]+)(?:&| |,)*(?= |$)/; m = tr.exec(tmpl); if(m) { let val = []; val.push(tmpl.slice(0, m.index)); let tfm = ret.tfm = ret.tfm || {}; let dat = m[0]; tfm[m[1].trim()] = m[2].trim(); tfm[m[2].trim()] = m[1].trim(); val.push(tmpl.slice(m.index + m[0].length)); ret.v = val.join(' ').trim(); tmpl = ret.v; didChange = true; } if(!didChange) return tmpl; return ret; } console.log(JSON.stringify(obj, null, '\t'));