package.dist.esm.regex.js.map Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unicode-variants Show documentation
Show all versions of unicode-variants Show documentation
Unicode variant string matching
The newest version!
{"version":3,"file":"regex.js","sources":["../../lib/regex.mjs"],"sourcesContent":["\n/**\n * Convert array of strings to a regular expression\n *\tex ['ab','a'] => (?:ab|a)\n * \tex ['a','b'] => [ab]\n * @param {string[]} chars\n * @return {string}\n */\nexport const arrayToPattern = (chars) =>{\n\n\tchars = chars.filter( Boolean );\n\n\tif( chars.length < 2 ){\n\t\treturn chars[0] || '';\n\t}\n\n\treturn (maxValueLength(chars) == 1) ? '['+chars.join('')+']' : '(?:'+chars.join('|')+')';\n};\n\n/**\n * @param {string[]} array\n * @return {string}\n */\nexport const sequencePattern = (array)=>{\n\n\tif( !hasDuplicates(array) ){\n\t\treturn array.join('');\n\t}\n\n\tlet pattern = '';\n\tlet prev_char_count = 0;\n\n\tconst prev_pattern = ()=>{\n\t\tif( prev_char_count > 1 ){\n\t\t\tpattern += '{'+prev_char_count+'}';\n\t\t}\n\t}\n\n\tarray.forEach((char,i)=>{\n\n\t\tif( char === array[i-1] ){\n\t\t\tprev_char_count++;\n\t\t\treturn;\n\t\t}\n\n\t\tprev_pattern();\n\n\t\tpattern += char;\n\t\tprev_char_count = 1;\n\t});\n\n\tprev_pattern();\n\n\treturn pattern;\n\n}\n\n\n\n/**\n * Convert array of strings to a regular expression\n *\tex ['ab','a'] => (?:ab|a)\n * \tex ['a','b'] => [ab]\n * @param {Set} chars\n * @return {string}\n */\nexport const setToPattern = (chars)=>{\n\tlet array = toArray(chars);\n\treturn arrayToPattern(array);\n}\n\n\n\n/**\n *\n * https://stackoverflow.com/questions/7376598/in-javascript-how-do-i-check-if-an-array-has-duplicate-values\n * @param {any[]} array\n */\nexport const hasDuplicates = (array) => {\n return (new Set(array)).size !== array.length;\n}\n\n\n/**\n * https://stackoverflow.com/questions/63006601/why-does-u-throw-an-invalid-escape-error\n * @param {string} str\n * @return {string}\n */\nexport const escape_regex = (str) => {\n\treturn (str + '').replace(/([\\$\\(\\)\\*\\+\\.\\?\\[\\]\\^\\{\\|\\}\\\\])/gu, '\\\\$1');\n};\n\n/**\n * Return the max length of array values\n * @param {string[]} array\n *\n */\nexport const maxValueLength = (array) => {\n\treturn array.reduce( (longest, value) => Math.max(longest,unicodeLength(value)),0);\n}\n\n\n/**\n * @param {string} str\n */\nexport const unicodeLength = (str) => {\n\treturn toArray(str).length;\n}\n\n/**\n * @param {any} p\n * @return {any[]}\n */\nexport const toArray = (p) => Array.from(p);\n"],"names":["arrayToPattern","chars","filter","Boolean","length","maxValueLength","join","sequencePattern","array","hasDuplicates","pattern","prev_char_count","prev_pattern","forEach","char","i","setToPattern","toArray","Set","size","escape_regex","str","replace","reduce","longest","value","Math","max","unicodeLength","p","Array","from"],"mappings":";AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACaA,MAAAA,cAAc,GAAIC,KAAD,IAAU;AAEvCA,EAAAA,KAAK,GAAGA,KAAK,CAACC,MAAN,CAAcC,OAAd,CAAR,CAAA;;AAEA,EAAA,IAAIF,KAAK,CAACG,MAAN,GAAe,CAAnB,EAAsB;AACrB,IAAA,OAAOH,KAAK,CAAC,CAAD,CAAL,IAAY,EAAnB,CAAA;AACA,GAAA;;EAED,OAAQI,cAAc,CAACJ,KAAD,CAAd,IAAyB,CAA1B,GAA+B,GAAA,GAAIA,KAAK,CAACK,IAAN,CAAW,EAAX,CAAJ,GAAmB,GAAlD,GAAwD,KAAML,GAAAA,KAAK,CAACK,IAAN,CAAW,GAAX,CAAN,GAAsB,GAArF,CAAA;AACA,EATM;AAWP;AACA;AACA;AACA;;AACaC,MAAAA,eAAe,GAAIC,KAAD,IAAS;AAEvC,EAAA,IAAI,CAACC,aAAa,CAACD,KAAD,CAAlB,EAA2B;AAC1B,IAAA,OAAOA,KAAK,CAACF,IAAN,CAAW,EAAX,CAAP,CAAA;AACA,GAAA;;EAED,IAAII,OAAO,GAAG,EA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