JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 645
  • Score
    100M100P100Q99715F
  • License ISC

A powerful Random Picker of elements with many options. Easy to use.

Package Exports

  • rand-picker

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (rand-picker) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

rand-picker

NPM version CI Coverage Status GitHub top language GitHub

A powerful Random newPicker of elements with many options. Easy to use.


How to use

Install:

npm i rand-picker

Import (ES Modules):

import { newPicker } from "rand-picker";

Create a new picker:

let data = [1, 2, 3, 4, 5, 6];
let picker = newPicker(data);

Typing (TypeScript):

import { newPicker, Picker } from "rand-picker";

let data: number[] = [1, 2, 3, 4, 5, 6];
let picker: Picker<number> = newPicker(data);

Pick a random element:

let element1 = picker.pickOne();
let [element2] = picker.pick();

Pick multiple elements:

let elements = picker.pick(40); // Picks 40 random elements

Remove element after to be picked:

let picker = newPicker(data, {
    removeOnPick: true
});
picker.pick(2);
console.log(picker.length); // 4
console.log(data.length); // 4

Warning: picker mutates 'data' parameter.

Weighted picker:

let picker = newPicker(['A', 'B'], {
    weighted: true
});
picker.put('A', 25); // Edits weight of 'A' to 25
picker.put('B', 25); // Edits weight of 'B' to 25
picker.put('C', 50); // Add 'C' and puts its height to 50

Note: it's not necessary to sum up 100, weights are relative about the elements.

Note 2: added weights does nothing if 'weighted' option is not enabled.

Options on pick:

  • unique:
picker.pick(5, {
    unique: true
}); // gets 5 unique elements
console.log(data.length); // 6. Don't modify data array

let elements = picker.pick(10, {
    unique: true
}); // tries to get 10 unique elements
console.log(elements.length); // 6. 'data' has only 6 unique values
  • sequential:
picker.pick(2, {
    sequential: true
}); // gets a pair of sequential elements: [1, 2], [2, 3], [3, 4], [4, 5] or [5, 6]

Note: both options can be combined.

Other functions:

picker.throwDart(num); // Gives 'num' between 0 and weight, returns the determinated element for that number.

picker.weight // Returns total picker weights

picker.length // Return data length

picker.getWeight(obj) // Returns the assigned weight for 'obj'

picker.duplicate(options?) // Return a picker copy, with a new data and weight arrays

picker.remove(obj) // Removes 'obj' from picker and returns it

Note: if weights are not enabled, it takes all them as 1 for weight-related functions.


Daniel Sales Álvarez. 2021.