npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sscovil/random

v1.3.1

Published

An assortment of useful randomness functions, with TypeScript type declarations.

Downloads

4

Readme

Random

An assortment of useful randomness functions, with TypeScript type declarations.

Installation

yarn add @sscovil/random
# OR
npm install @sscovil/random

Usage

// ES Module Syntax
import random from "@sscovil/random";

// CommonJS Syntax
const random = require("@sscovil/random");

random.color()

Returns a random hex color (e.g. "#C2123B", "#A2FF9A", "#0DC43F").

const color = random.color();
const isValid = (color) => !!String(color).match(/^#[0-9A-Fa-f]{6}$/);

expect(isValid(color)).toBe(true);

random.distribute(length, value, min, max)

Given a desired array length and an integer value, returns an array of integers that add up to value randomly distributed among its elements.

const length = 5;
const value = 10;
const result = random.distribute(length, value); // returns an array like [0, 3, 1, 5, 1]
const sumArray = (arr) => arr.reduce((a, b) => a + b, 0);

expect(result.length).toBe(length);
expect(sumArray(result)).toBe(value);

Accepts an optional third argument to specify a min value for each element in the array (default is 0).

const length = 5;
const value = 100;
const min = 10;
const result = random.distribute(length, value, min); // returns an array like [11, 33, 24, 17, 15]
const sumArray = (arr) => arr.reduce((a, b) => a + b, 0);

expect(result.length).toBe(length);
expect(sumArray(result)).toBe(value);

for (let i = 0; i < result.length; i++) {
    expect(result[i]).toBeGreaterThanOrEqual(min);
}

Ignores value if it is less than length * min, and returns an array of length with each element set to the value of min.

const length = 5;
const value = 10; // this is less that length * min, so it will be ignored
const min = 10;
const result = random.distribute(length, value, min); // returns [10, 10, 10, 10, 10]
const sumArray = (arr) => arr.reduce((a, b) => a + b, 0);

expect(result.length).toBe(length);
expect(sumArray(result)).toBe(length * min);

for (let i = 0; i < result.length; i++) {
    expect(result[i]).toBe(min);
}

Accepts an optional fourth parameter to specify a max value for each element in the array.

const length = 10;
const value = 25;
const min = 1;
const max = 5;
const result = random.distribute(length, value, min, max); // returns an array like [2, 2, 3, 3, 5, 4, 1, 1, 2, 2]
const sumArray = (arr) => arr.reduce((a, b) => a + b, 0);

expect(sumArray(result)).toBe(value);
for (let j = 0; j < result.length; j++) {
    expect(result[j]).toBeGreaterThanOrEqual(min);
    expect(result[j]).toBeLessThanOrEqual(max);
}

random.pick(arr)

Returns a random element from a given array.

const arr = ["foo", "bar", "baz"];
const result = random.pick(arr);

expect(arr).toContain(result);

Works with an array of objects, or any other data type.

const arr = [{ name: "foo" }, { name: "bar" }, { name: "baz" }];
const result = random.pick(arr);

expect(arr).toContain(result);

Returns null if the value passed in is an empty array, or is not an array.

expect(random.pick([])).toBe(null);
expect(random.pick({ name: "foo" })).toBe(null);
expect(random.pick("foobar")).toBe(null);
expect(random.pick(123)).toBe(null);

random.integer(min, max)

Returns an integer between min and max (inclusive).

const min = 1;
const max = 10;
const result = random.integer(min, max);

expect(Number.isInteger(result)).toBe(true);
expect(result).toBeGreaterThanOrEqual(min);
expect(result).toBeLessThanOrEqual(max);

random.sample(arr, size)

Returns a random sample of elements from a given array. This uses the shuffle function (see below) internally.

const arr = ["north", "east", "south", "west"];
const size = 2;
const result = random.sample(arr, size);

expect(Array.isArray(result)).toBe(true);
expect(result.length).toBe(size);
expect(arr).toContain(result[0]);
expect(arr).toContain(result[1]);
expect(result[0]).not.toBe(result[1]); // result will not contain duplicate values

Returns a rearranged version of arr if size >= arr.length.

const arr = ["n", "ne", "e", "se", "s", "sw", "w", "nw"];
const size = arr.length;
const result = random.sample(arr, size);

expect(Array.isArray(result)).toBe(true);
expect(result.join()).not.toBe(arr.join()); // new array elements are in a different order
expect(result.sort()).toEqual(arr.sort()); // all values from orginal array are present

Returns an empty array if arr or size are invalid.

expect(random.sample([], 2)).toEqual([]);
expect(random.sample({ foo: 1, bar: 2, baz: 3 }, 2)).toEqual([]);
expect(random.sample([1, 2, 3], "2")).toEqual([]);
expect(random.sample([1, 2, 3], -2)).toEqual([]);

random.shuffle(arr)

Returns a new array with all the elements of the original array, in a random order.

const arr = []
for (let i = 0; i < 100; i++) {
  arr.push(i) // generate an array with 100 unique elements
}
const result = random.shuffle(arr)

expect(result.length).toBe(arr.length)
expect(result).not.toEqual(arr)
expect(result.sort()).toEqual(arr.sort())

Running Tests

yarn run test
# OR
npm test