GithubHelp home page GithubHelp logo

unique-random-array's Introduction

unique-random-array

Get consecutively unique elements from an array

Useful for things like slideshows where you don't want to have the same slide twice in a row.

Install

npm install unique-random-array

Usage

import uniqueRandomArray from 'unique-random-array';

const random = uniqueRandomArray([1, 2, 3, 4]);

console.log(random(), random(), random(), random());
//=> 4 2 1 4

API

uniqueRandomArray(array)

Returns a function, that when called, will return a random element that's never the same as the previous, or undefined if the array is empty.

array

Type: unknown[]

Related

unique-random-array's People

Contributors

bendingbender avatar richienb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

unique-random-array's Issues

Issue with calling a function multiple times on an empty array

When passing an empty array as an argument, the first call to the function returns undefined, while the second call throws an error. It would be better to throw an error directly when passing an empty array, instead of throwing an error on the second call.

var b = await import("unique-random-array");
undefined
> var c = b.default([])
undefined
> c()
undefined
> c()
Uncaught RangeError: Maximum call stack size exceeded
    at Math.random (<anonymous>)
    at random (file:///Users/smile/Documents/muyu/pdd-pptr/node_modules/.pnpm/[email protected]/node_modules/unique-random/ind
ex.js:6:10)
    at random (file:///Users/smile/Documents/muyu/pdd-pptr/node_modules/.pnpm/[email protected]/node_modules/unique-random/ind
ex.js:9:69)
    at random (file:///Users/smile/Documents/muyu/pdd-pptr/node_modules/.pnpm/[email protected]/node_modules/unique-random/ind
ex.js:9:69)
    at random (file:///Users/smile/Documents/muyu/pdd-pptr/node_modules/.pnpm/[email protected]/node_modules/unique-random/ind
ex.js:9:69)
    at random (file:///Users/smile/Documents/muyu/pdd-pptr/node_modules/.pnpm/[email protected]/node_modules/unique-random/ind
ex.js:9:69)
    at random (file:///Users/smile/Documents/muyu/pdd-pptr/node_modules/.pnpm/[email protected]/node_modules/unique-random/ind
ex.js:9:69)
    at random (file:///Users/smile/Documents/muyu/pdd-pptr/node_modules/.pnpm/[email protected]/node_modules/unique-random/ind
ex.js:9:69)
    at random (file:///Users/smile/Documents/muyu/pdd-pptr/node_modules/.pnpm/[email protected]/node_modules/unique-random/ind
ex.js:9:69)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.