generate-words-freqmap
Version:
Generate frequency map from array of strings.
2 lines (1 loc) • 1.54 kB
JavaScript
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(e="undefined"!=typeof globalThis?globalThis:e||self).fullfiller=r()}(this,(function(){"use strict";class e extends Error{constructor(e,r){super(e),this.library=r}toString(){return`Error in \`${this.library}\` library:\n\t${this.message}`}}const r=(e,r)=>`Given \`wordsArray\` doesn't have enough words to construct \`freqMap\` containing the minimum quantity of words required. Minimum number of words required: ${e}. Number of words received: ${r}.`;const t={emphasizeBy:2,wordsQuantityMin:0,tierWeightMin:1,tierWeightMax:-1,mergePosteriorTiersAt:-1,caseInsensitive:!1};return function(n,i,o){const s={...t,...o},u=function(e,r,t,n){return Object.entries(e).reduce(((i,o)=>{const s=Number(o[0]),u=o[1];return s<r||-1!==t&&s>t?i:-1!==n&&s>n?{...i,[n]:(i[n]||[]).concat(e[s])}:{...i,[s]:u}}),{})}(function(e){return Object.keys(e).reduce(((r,t)=>{const n=e[t];return{...r,[n]:(r[n]||[]).concat(t)}}),{})}(function(e,r,t){return r.reduce(((e,r)=>void 0===e[r]?e:{...e,[r]:Math.round(e[r]*t)}),e)}(function(e,r){return e.reduce(((e,t)=>{const n=r?t.toLowerCase():t;return{...e,[n]:Object.prototype.hasOwnProperty.call(e,n)?e[n]+1:1}}),{})}(n,s.caseInsensitive),i||[],s.emphasizeBy)),s.tierWeightMin,s.tierWeightMax,s.mergePosteriorTiersAt),c=function(e){return Object.values(e).reduce(((e,r)=>e+r.length),0)}(u);if(c<s.wordsQuantityMin)throw new e(r(s.wordsQuantityMin,c),"generate-words-freqmap");return u}}));