LCOV - code coverage report
Current view: top level - src/choose - chooser.js (source / functions) Hit Total Coverage
Test: cov_profile.lcov Lines: 27 27 100.0 %
Date: 2021-12-16 00:47:58 Functions: 1 1 100.0 %
Branches: 8 8 100.0 %

           Branch data     Line data    Source code
       1                 :         16 : const chooser = (position, arr, weights) => {
       2            [ + ]:        674 :   const calculatedWeights = arr.reduce((memo, _value, index) => {
       3            [ + ]:      28010 :     memo.push(
       4                 :      28010 :       (memo[index - 1] || 0) +
       5            [ + ]:      28010 :         (typeof weights[index] === "number" ? weights[index] : 1),
       6         [ +  + ]:      28010 :     );
       7                 :      28010 :     return memo;
       8                 :        674 :   }, []);
       9                 :        674 : 
      10                 :        674 :   const target = position * calculatedWeights[calculatedWeights.length - 1];
      11                 :        674 : 
      12                 :        674 :   // TODO: Binary Tree akin to: https://github.com/plantain-00/weighted-picker/blob/3cdd2c37856bc39c2304d7a597b237cecc845f7e/src/index.ts#L34
      13                 :        674 :   let index;
      14                 :        674 : 
      15                 :        674 :   calculatedWeights.every((weight, i) => {
      16            [ + ]:      14870 :     if (target > weight) {
      17            [ + ]:      28412 :       return true;
      18                 :      14870 :     } else {
      19            [ + ]:      15524 :       index = i;
      20                 :      15524 :       return false;
      21                 :      14870 :     }
      22                 :        674 :   });
      23                 :        674 : 
      24                 :        674 :   return index;
      25                 :         16 : };
      26                 :         16 : 
      27                 :          8 : export default chooser;

Generated by: LCOV version 1.15