diff --git a/public/dictionaryBuilder.js b/public/dictionaryBuilder.js index ab378484b5442ad404f04cbe8548998709b19b33..5fe9c4283e7b4aae797f5e2e2e6eecb2db1f9113 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 92a40aee80781dce06bf46c8978c348ae7f77755..f359c74a2387abb61e81fe1f9efcea3a6f83a9b0 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"]);