tokenize-words
Version:
Break down text into array of words.
2 lines (1 loc) • 2.36 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).fullfiller=t()}(this,(function(){"use strict";class e extends Error{constructor(e,t){super(e),this.library=t}toString(){return`Error in \`${this.library}\` library:\n\t${this.message}`}}const t=(e,t)=>`Given \`text\` doesn't have enough keywords to construct \`wordsArray\` containing the minimum quantity of words required. Minimum number of words required: ${e}. Number of words received: ${t}.`;function n(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}const r=e=>n(e).replace("\\.","\\.?");function s(e,t){if(!/\p{Lu}/u.test(e[0]))return e;const n=new RegExp(`[^.\\s]\\s+${r(e)}(\\s|\\.|$)`).test(t),s=e.toLowerCase();return new RegExp(`(^|\\s|\\.)${r(s)}(\\s|\\.|$)`).test(t)&&!n?s:e}function u(e){return e.replace(/(?:^|\.\s+)(\p{Lu}\S*)/gu,((e,t,n,r)=>/^[\p{Lu}]+$/u.test(t)?e:e.replace(t,s(t,r))))}function o(e,t){const r=e.slice(0,-1),s=function(e,t){const r=e.replace(/\.$/,""),s=(t.match(new RegExp(`(^|\\s)${n(e)}(?=\\s|$)`,"g"))||[]).length;return 1!==s&&s>(t.match(new RegExp(`(^|\\s)${n(r)}(?=\\s|$)`,"g"))||[]).length}(e,t);return s?e:r}function c(e,t,r){return/^[.]+$/.test(e)?"":/^[\d.,:%$]+$/.test(u=e)&&/\d/.test(u)?e.replace(/\.$/,""):/^\.|\.$/.test(e)&&1===e.match(/\./g)?.length?e.startsWith(".")?function(e,t){const r=e.substring(1);return(t.match(new RegExp(`(^|[^\\p{L}\\p{Nd}])${n(e)}(?=([^\\p{L}\\p{Nd}]|$))`,"gu"))||[]).length>1?e:s(r,t)}(e,r):o(e,r):/[\p{Ll}\p{Nd}]\.[\p{Lu}\p{Nd}]/u.test(e)&&1===e.match(/\./g)?.length?function(e,t){const[,n,r]=/^(.+)\.(.+)$/.exec(e);return`${n} ${s(r,t)}`}(e,r):e;var u}function i(e){return e.replace(/\S*\.\S*/g,c)}function l(e,t,n,r){return[t,r].every((e=>/^\d$/.test(e)))?t+n:`${t} `}function p(e){return e.replace(/["()[\]{}<>–—;?!]+/g," ").replace(/(^|.)(,|:)(?=(.|$))/g,l).replace(/\.{2,}|…/g," ").replace(/\n+/g," ").replace(/(^|\s)(\p{Lu}\.(\s|$)){2,}/gu,(e=>` ${e.replace(/\s/g,"")} `))}function g(e){return e.replace(/(^|\s)[^\s\p{L}\p{Nd}]+(?=(\s|$))/gu,"")}const a={lengthMin:0};return function(n,r={}){const s={...a,...r},o=function(e){return[p,u,i,g].reduce(((e,t)=>t(e)),e)}(n).match(/\S+/g)||[],c=o.length;if(c<s.lengthMin)throw new e(t(s.lengthMin,c),"tokenize-words");return o}}));