LCOV - code coverage report
Current view: top level - randomjs/distribution - pick.ts (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 3 22 13.6 %
Date: 2021-03-12 10:43:40 Functions: 0 1 0.0 %

          Line data    Source code
       1             : import { Engine } from "../types.ts";
       2           1 : import { convertSliceArgument } from "../utils/convertSliceArgument.ts";
       3           1 : import { toInteger } from "../utils/toInteger.ts";
       4           1 : import { integer } from "./integer.ts";
       5             : 
       6             : /**
       7             :  * Returns a random value within the provided `source` within the sliced
       8             :  * bounds of `begin` and `end`.
       9             :  * @param source an array of items to pick from
      10             :  * @param begin the beginning slice index (defaults to `0`)
      11             :  * @param end the ending slice index (defaults to `source.length`)
      12             :  */
      13           0 : export function pick<T>(
      14           0 :   engine: Engine,
      15           0 :   source: ArrayLike<T>,
      16           0 :   begin?: number,
      17           0 :   end?: number
      18             : ): T {
      19           0 :   const length = source.length;
      20           0 :   if (length === 0) {
      21           0 :     throw new RangeError("Cannot pick from an empty array");
      22           0 :   }
      23           0 :   const start =
      24           0 :     begin == null ? 0 : convertSliceArgument(toInteger(begin), length);
      25           0 :   const finish =
      26           0 :     end === void 0 ? length : convertSliceArgument(toInteger(end), length);
      27           0 :   if (start >= finish) {
      28           0 :     throw new RangeError(`Cannot pick between bounds ${start} and ${finish}`);
      29           0 :   }
      30           0 :   const distribution = integer(start, finish - 1);
      31           0 :   return source[distribution(engine)];
      32           0 : }

Generated by: LCOV version 1.15