find-partial-anagrams
Version:
Takes a set of letters and returns all the words in a given word list that can be made using all or a subset of those letters.
2 lines (1 loc) • 1.41 kB
JavaScript
;Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const g="?";function u(t){return typeof t=="string"&&/^[A-Za-z\?]{1}$/.test(t)}function m(t,r){const n=[];let i=[];const s=`Input must be a string or array containing only letters (a-z) and wildcards (${g}).`,o="No word list was provided to search for matches. Provide an array of strings.";if(typeof t=="string"){if(!t||!/^[A-Za-z\?]+$/.test(t))throw new Error(s);i=t.toLowerCase().split("")}else if(Array.isArray(t)){if(t.length===0||t.some(e=>!u(e)))throw new Error(s);i=t}else throw new Error(s);if(!r||r.length===0)throw new Error(o);return r=r.filter(e=>e.length<=i.length),r.forEach(e=>{const l=e.split(""),c=i.slice(),a=[];l.forEach(d=>{const f=c.indexOf(d);let h=f>-1?f:c.indexOf(g);h>-1&&(c.splice(h,1),a.push(d))}),a.join("")===e&&n.push(e)}),n}function p(t){const r=t.sort();let n=[r[0]];const i=(s,o)=>{let e=0;for(;e<s.length&&e<o.length&&s[e]===o[e];)e++;return e};for(let s=1;s<r.length;s++){const o=i(r[s-1],r[s]),e=r[s].slice(o);n.push(`${o}${e}`)}return n}function w(t){var i,s;const r=t;let n=[r[0]];for(let o=1;o<r.length;o++){const e=(s=(i=r[o].match(/^[0-9]+/))==null?void 0:i[0])!=null?s:"0",l=n[o-1].slice(0,parseInt(e)),c=r[o].slice(e.length),a=`${l}${c}`;n.push(a)}return n}exports.compressWordList=p;exports.decompressWordList=w;exports.findPartialAnagrams=m;exports.isChar=u;