From d59a71cc93ecc7ea2fc6a97b17abf09cd93cbc31 Mon Sep 17 00:00:00 2001 From: "[1000i100] Millicent Billette" <git@1000i100.fr> Date: Wed, 2 May 2018 10:44:16 +0200 Subject: [PATCH] FEAT: advensed dictionaryBuilder finished exept escape chr --- public/dictionaryBuilder.js | 14 +++----------- public/dictionaryBuilder.test.js | 6 +++--- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/public/dictionaryBuilder.js b/public/dictionaryBuilder.js index ab37848..5fe9c42 100644 --- a/public/dictionaryBuilder.js +++ b/public/dictionaryBuilder.js @@ -46,20 +46,12 @@ function regLikeVariants(theString,allStrings) { return `(${res.join('|')})`; }; theString = theString.replace(/([^\)]){([^}]+)}/g,qtyApply); + theString = theString.replace(/^(.*)\(([^)]*)\)\{([^}]+)\}(.*)$/,(all,before,choices,qty,after)=> before+qtyApply('',`(${choices})`,qty)+after ); // handle (|) - let multiString = theString.replace(/^(.*)\(([^)]*)\)(.*)$/,(all,before,choices,after)=>choices.split('|').map(c=>{ - if(after.indexOf('{')!==0) return before+c+after; - else { - console.log(c); - const end = after.slice(1).split('}'); - const qty = end.shift(); - const afterQty = end.join('}'); - return before+qtyApply('',c,qty)+afterQty; - } - }).join('=$##$=') ).split('=$##$='); + let multiString = theString.replace(/^(.*)\(([^)]*)\)(.*)$/,(all,before,choices,after)=>choices.split('|').map(c=> before+c+after).join('=$##$=') ).split('=$##$='); console.log(multiString); multiString = [].concat(...multiString.map(str => (str.indexOf('(')!== -1)?regLikeVariants(str,allStrings):str)); - return multiString; + return dedup(multiString); } diff --git a/public/dictionaryBuilder.test.js b/public/dictionaryBuilder.test.js index 92a40ae..f359c74 100644 --- a/public/dictionaryBuilder.test.js +++ b/public/dictionaryBuilder.test.js @@ -36,10 +36,10 @@ describe('dictionaryBuilder', () => { expect(app.regLikeVariants("b{3,5}")).toEqual(["bbb","bbbb","bbbbb"]); }); it('regLikeVariants handle (string){qty}', () => { - expect(app.regLikeVariants("(toto|tata){0,2}")).toEqual(["","toto","tata","totototo","tototata","tatatoto","tatatata"]); + expect(app.regLikeVariants("(toto|tata){0,2}")).toEqual(["","toto","totototo","tata","tatatoto","tototata","tatatata"]); }); - xit('regLikeVariants handle nested ([a-f]|<ref>){0,1}', () => { - expect(app.regLikeVariants("autre mot")).toEqual(["autre mot","Autre Mot","AUTRE MOT"]); + it('regLikeVariants handle nested ([a-f]|<ref>){0,1}', () => { + expect(app.regLikeVariants("([B-D]|<ref>){0,1}",["ref::plop"])).toEqual(["","B","plop","C","D"]); }); xit('regLikeVariants handle plop:\\:', () => { expect(app.regLikeVariants("autre mot")).toEqual(["autre mot","Autre Mot","AUTRE MOT"]); -- GitLab