Skip to content
Snippets Groups Projects
Commit 3dace612 authored by Millicent Billette's avatar Millicent Billette
Browse files

WiP: extract branch from dictionary-tree (deprecated)

parent ccf60bfc
No related branches found
No related tags found
No related merge requests found
......@@ -131,3 +131,27 @@ export function serialize(treeStruct) {
if (isStep(treeStruct)) return treeStruct.step.map(serialize).join('');
throw new Error(`Error: how to serialize ${JSON.stringify(treeStruct)} RAW: ${treeStruct}`);
}
/*
export function extract(treeNavArray,fromTreeWrapped){
if(!Array.isArray(treeNavArray)) throw new Error(`treeNav: ${JSON.stringify(treeNavArray)} should be an integer array.`);
let extractedTree = '';
if(treeNavArray.length === 0) return fromTreeWrapped.pop();
if (typeof fromTreeWrapped[0] === 'string') throw new Error(`invalid treeNav pointer ${JSON.stringify(treeNavArray)} in ${serialize(fromTreeWrapped[0])} no branch ${treeNavArray[0]}.`);
if (Array.isArray(fromTreeWrapped[0])) {
const branch = treeNavArray.shift();
const extractedTree = fromTreeWrapped[0][branch]; // recursive here ?
//fromTreeWrapped[0] = [].concat(branch>0?fromTreeWrapped[0].slice(0,branch-1):[],fromTreeWrapped[0].slice(branch+1,fromTreeWrapped[0].length));
return extractedTree;
}
if (isStep(fromTreeWrapped[0])) {
const extractedTree = {step:[]};
fromTreeWrapped[0].step.forEach()
f
}
return extractedTree;
}
*/
......@@ -18,3 +18,21 @@ test('build complexe tree with (|) pattern', t => t.is(buildTreeStruct('(a(|b@@@
));
test('serialize tree to a(b|c)', t => t.is(app.serialize({step: ['a', ['b', 'c']]}), 'a(b|c)'));
test('serialize incorrect tree throw', t => t.throws(() => app.serialize({plop: ['a']})));
const exempleTree = ()=>app.buildTreeStruct('a(b(c|d)|e(f|g|h)ij(k|l)|@@@@m)')
test.skip('extract subtree ae(f|g|h)ij(k|l) from a(b(c|d)|e(f|g|h)ij(k|l)|@@@@m)',t =>{
const srcTree = exempleTree();
const treePointer = [1];
const extractedTree = app.extract(treePointer,[srcTree]);
t.is(JSON.stringify(extractedTree), expected({step: ['ae', ['f', 'g', 'h'], 'ij']}))
t.is(app.serialize(srcTree), 'a(b(c|d)|@@@@k)')
});
test.skip('extract subtree aehij from a(b(c|d)|e(f|g|h)ij(k|l)|@@@@m)',t =>{
const srcTree = exempleTree();
const treePointer = [1,2];
const extractedTree = app.extract(treePointer,[srcTree]);
t.is(extractedTree, 'aehij');
t.is(app.serialize(srcTree), 'a(b(c|d)|e(f|g)ij|@@@@k)')
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment