An easy-to-use library to make your life easier when working with random numbers or random choices in javascript.

Overview

vrandom

JavaScript Style Guide

An easy-to-use library to make your life easier when working with random numbers or random choices in javascript.

Table of contents

Installation & Usage

Install the package with npm

npm i vrandom

Import the package where you need it (both commonjs and module syntax supported)

import vrandom from "vrandom";

or

const vrandom = require("vrandom");

Use the function you need by accessing it through the vrandom object

vrandom.flip();

Tests

The library is fully tested. If you are contributing and you are creating a new feature please add tests to it. We use standardjs to keep the formatting of the code uniform across all files. Every time the tests are run, the format check is run as well with the command npx standard. If you are trying to run the tests but it's not working, it might be because the files aren't formatted correctly. To format the files following the standard format please run

npx standard --fix

and try running npm t again.

There is a CI workflow set-up that runs on every pull-request, so if tests fail it will be impossible to merge the PR.

If you want to see the tests running:

  1. Clone the repo locally
  2. Install dependencies
npm i
  1. run tests and format check
npm t
  1. run tests and see test coverage
npm run test-coverage

Docs

flip

Usage:

vrandom.flip();

// possible output: true

It doesn't take any arguments, it returns true 50% of the time and false 50% of the time.

int

Usage:

vrandom.int(min, max, ?inclusive = true);

It takes three arguments: min, max and inclusive

If no third argument is provided inclusive defaults to true.

The first two arguments both HAVE to be integers and max HAS to be greater than min, or it will throw an error.

It returns a random integer between min and max - min and max included IF inclusive is set to true (or not provided).

It returns a random integer between min and max - max excluded IF inclusive is set to false.

Example:

vrandom.int(1, 10);
// will return an integer between 1 and 10 - 10 included
// possible output: 10
vrandom.int(1, 10, false);
// will return an integer between 1 and 10 - 10 NOT included
// possible output: 9
// non-possible output: 10

float

Usage:

vrandom.float(min, max, ?decimals = 2);

It takes 2 arguments, plus 1 optional argument if only two arguments are provided the third argument defaults to 2.

The first and second arguments have to be numbers (floats or ints) the third argument has to be an integer.

The function will return a random floating point number between min and max - min and max INCLUDED. The returned floating point number will have a maximum of 2 decimal places if no third argument is specified.

If a third argument is passed, that will be the maximum number of decimals that the returned floating point number will have. Maximum of 15.

Note: the returned floating point number might have LESS decimals than the number specified but not more.

The function will return an error if: less than 2 arguments are passed, the third argument is greater than 15 or less than 0, if min is greater or equal to max, the third argument is not an integer.

Example:

vrandom.float(0.5, 10.1, 5);
// possible output: 2.47185
// possible output: 10.1
// possible output: 0.5427

pick

Usage:

vrandom.pick(array);

It takes one argument, and it HAS to be an array, if not it will throw an error.

It will return a random element from that array.

Example:

const arr = [1, 2, 3, 4, "hello", "world", true, false];
vrandom.pick(arr);

// possible output: 1

Will return a random element from the array 'arr'

pickFromStr

Usage:

vrandom.pickFromStr(string);

Same as pick, but it takes a string as an argument and returns a random character from that string.

Takes 1 argument, and it needs to be a string, or it will throw an error.

If provided string is empty, it will return an empty string.

Example:

const string = "hello";
vrandom.pickFromStr(string);

// possible output: "e"

Returns random character from a string.

shuffle

Usage:

vrandom.shuffle(array);

It takes one argument, and it HAS to be an array.

It will make a shallow copy of the provided array, and return a new array with the same elements as the provided array, but in (possibly) a different order, every element is shuffled and is assigned a new position (the initial position of the element is included in the possible positions where the element ends up). The original array remains unchanged.

Example:

const arr = [1, 2, "hello", "world", true, false];
vrandom.shuffle(arr);

// possible output: [1, "hello", true, false, 2, "world"]

console.log(arr); // [1, 2, "hello", "world", true, false]

string

Usage:

vrandom.string(size, ?charset = "alphanumeric");

It takes 2 arguments. First argument is the length of the desired output. It needs to be an integer greater than 0, or it will throw an error. Second argument is a string representing the type of charset that should be used to generate output. At the moment there is only 2 possible values:

  • "alphanumeric" [0-9a-zA-Z]
  • "alphabetic" [a-zA-Z]

If the second argument is omitted it will default to "alphanumeric".

Example:

vrandom.string(5);
// possible output: "Zd5r3"

vrandom.string(5, "alphanumeric");
// possible output: "1zZ3L"

vrandom.string(5, "alphabetic");
// possible output: "LzkAm"
// non-possible output: "1oLL4"

words

Usage:

vrandom.words(size, ?format = "lowercase")

Takes 2 arguments, it return an array of random words.

First argument is the size of the array that will be returned (the number of random words in the output). It needs to be a positive integer, or it will throw an error.

Second argument is optional (it will default to "lowercase" if not passed in). It's the desired format of the individual strings in the returned array. At the moment there is possible options:

  • "lowercase"
  • "uppercase"
  • "capitalized"

If the second argument is passed in, but it doesn't match any of the possible options, it will throw an error.

Example:

vrandom.words(5);
// possible output: ["ability", "main", "farm", "took", "current"]

vrandom.words(5, "lowercase");
// possible output: ["ability", "main", "farm", "took", "current"]

vrandom.words(5, "uppercase");
// possible output: ["ABILITY", "MAIN", "FARM", "TOOK", "CURRENT"]

vrandom.words(5, "capitalized");
// possible output: ["Ability", "Main", "Farm", "Took", "Current"]

letter

Usage:

vrandom.letter();

It doesn't take any arguments, it returns a random letter from the english alphabet. The returned letter can be lowercase OR uppercase.

Example:

vrandom.letter();
// possible output: "a"
// possible output: "A"
// possible output: "m"
// possible output: "Z"

uppercaseLetter

Usage:

vrandom.uppercaseLetter();

It doesn't take any arguments, it returns a random uppercase letter from the english alphabet.

Example:

vrandom.uppercaseLetter();
// possible output: "A"
// possible output: "M"
// possible output: "Z"

lowercaseLetter

Usage:

vrandom.lowercaseLetter();

It doesn't take any arguments, it returns a random lowercase letter from the english alphabet.

Example:

vrandom.lowercaseLetter();
// possible output: "a"
// possible output: "m"
// possible output: "z"

Creators & Contributors

You might also like...

A JavaScript Library To Make Your Work Work Easier/Faster

A JavaScript Library To Make Your Work Work Easier/Faster

Functionality.js About ✍️ This Is A JavaScript Library To Make Your Work Easier/Faster, You Can See Functionalty.js Website From Here Project Created

Jun 23, 2022

TypeScript isomorphic library to make work with Telegram Web Apps init data easier.

Telegram Web Apps Init Data TypeScript isomorphic library to make work with Telegram Web Apps init data easier. Feel free to use it in browser and Nod

Oct 7, 2022

no-comma is a javascript library for dealing with inputted numbers that include commas

no-comma no-comma is a javascript library for dealing with inputted numbers that include commas. Nocomma will allow you to check if the number contain

Jan 27, 2022

front.phone is a Javascript library that identifies, validates and formats phone numbers.

front.phone front.phone is a Javascript library that identifies, validates and formats phone numbers. Demo The main goal of this project is to create

Oct 27, 2022

A small JavaScript library to generate YouTube-like ids from numbers.

A small JavaScript library to generate YouTube-like ids from numbers.

Hashids is small JavaScript library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database ids to the user: htt

Dec 30, 2022

Chrome Extensions are something which we use very frequently in our day-to-day life for example Grammarly

Chrome-Extension Chrome Extensions are something which we use very frequently in our day-to-day life for example Grammarly, In this project similarly,

Dec 14, 2022

For this workshop, we're going to learn more about cloud computing by exploring how to use Pulumi to build, configure, and deploy a real-life, modern application using Docker

For this workshop, we're going to learn more about cloud computing by exploring how to use Pulumi to build, configure, and deploy a real-life, modern application using Docker. We will create a frontend, a backend, and a database to deploy the Pulumipus Boba Tea Shop. Along the way, we'll learn more about how Pulumi works.

Dec 29, 2022

A library for boolean aliases to help you make your code more confusing and make your coworkers hate you.

yup-nope A library for boolean aliases to help you make your code more confusing and make your coworkers hate you. Installation Using npm: npm install

Dec 10, 2022

Open! Inclusive! Collaborative! A community for enthusiasts exploring new technologies, working on innovative ideas and helping each other grow together. Open Issues, Raise ideas, Make Pull Requests!

Open! Inclusive! Collaborative! A community for enthusiasts exploring new technologies, working on innovative ideas and helping each other grow together. Open Issues, Raise ideas, Make Pull Requests!

About Us OplnCo previously known as Devstucom represents Open Inclusive Collaborative. We as a community help our fellow students build skills through

Oct 13, 2022
Comments
  • Tests and type annotations

    Tests and type annotations

    Thanks for writing this package.

    • Adding some more tests to the existing test suites.
    • Adding some basic type annotations via JSDoc comments. This should help IDEs and users understand what types those functions take in and give back.
    • Adding a Prettier config file, although it might be better to actually run Prettier (and ESLint too) as part of the GitHub Actions workflow. Just to enforce consistent formatting across the project (as the config file I've added will do nothing on machines where Prettier isn't installed/enabled).
    • Updating shuffle so that it makes a shallow copy of its array argument before it begins shuffling. I think this is in line with what the docs currently mention.
    • Adding js to the code blocks in the README. Shouldn't do much other than provide syntax highlighting on platforms that support it.

    Should probably put the below in an issue instead of this PR, but it's a minor point. Might be worth checking the docs for float. If I'm reading correctly, docs suggest max is exclusive:

    https://github.com/ValerioCipolla/vrandom/blob/f37b299fa6479464bb2a243d2905ea93613e8d4e/README.md?plain=1#L118

    but the existing test suggests min >= return value <= max:

    https://github.com/ValerioCipolla/vrandom/blob/f37b299fa6479464bb2a243d2905ea93613e8d4e/tests/float.test.js#L20

    float's implementation uses toFixed which can round up.

    (0.125).toFixed(2)
    // '0.13'
    
    (0.121).toFixed(2)
    // '0.12'
    
    (0.129).toFixed(2)
    // '0.13'
    
    opened by crayola-eater 3
  • Test: Float test fail

    Test: Float test fail

     FAIL  tests/float.test.js
      ● float › should return a number between min and max
    
        expect(received).toBeLessThan(expected)
    
        Expected: < 1
        Received:   1
    
          40 |   it("should return a number between min and max", () => {
          41 |     expect(float(0, 1)).toBeGreaterThanOrEqual(0);
        > 42 |     expect(float(0, 1)).toBeLessThan(1);
             |                         ^
          43 |   });
          44 |
          45 |   it("should return a number with the specified number of decimals", () => {
    
          at Object.<anonymous> (tests/float.test.js:42:25)
    
    opened by kennarddh 2
  • Feat: Add build github action

    Feat: Add build github action

    Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

    Describe the solution you'd like A clear and concise description of what you want to happen.

    Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

    Additional context Add any other context or screenshots about the feature request here.

    opened by ValerioCipolla 0
  • feat: add keywords

    feat: add keywords

    Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

    Describe the solution you'd like A clear and concise description of what you want to happen.

    Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

    Additional context Add any other context or screenshots about the feature request here.

    opened by ValerioCipolla 0
Owner
Valerio Cipolla
Software engineer | Full stack JavaScript Developer
Valerio Cipolla
🤝 A set of Persian Helpers for NodeJS to make your life easier

Persian Helpers Persian Helpers is a minimal NodeJS package with a set of helpers/tools specifically for the Persian/Farsi language. If you like the p

Kasra Ghoreyshi 11 Dec 22, 2022
The Easel Javascript library provides a full, hierarchical display list, a core interaction model, and helper classes to make working with the HTML5 Canvas element much easier.

EaselJS EaselJS is a library for building high-performance interactive 2D content in HTML5. It provides a feature-rich display list to allow you to ma

CreateJS 8k Dec 29, 2022
A JavaScript module that shortens your code, makes life easier, and makes development faster!

Quxt A JavaScript module that shortens your code, makes life easier, and makes development faster! Installation npm install quxt Quick Start Check ind

Qux App 5 May 8, 2022
Conways-game-of-life - A Conway's Game Of Life project using Python

conways-game-of-life A Conway's Game Of Life project using Python JavaScript Devlog January 1st 2022: also need to remember Python's syntax a bit will

Felipe Melgaço Magesty Silveira 0 Sep 23, 2022
A template created with the intention of making my life easier when starting a project, and the lives of other people. :>

Express API A simple and improved api with ExpressJS. ?? | Glossary Dependencies How to Start Routes Controllers & Models License Author ?? | Dependec

Feh's 3 Sep 22, 2022
An easy-to-use JavaScript library aimed at making it easier to draw on SVG elements.

svg-pen-sketch An easy-to-use JavaScript library aimed at making it easier to draw on SVG elements when using a digital pen (such as the Surface Pen).

Kevin Desousa 8 Jul 27, 2022
Solid Forms provides several form control objects useful for making working with forms easier.

Solid Forms Solid Forms provides several form control objects useful for making working with forms easier. Demos and examples below. # solidjs yarn ad

John 28 Jan 2, 2023
A JavaScript Library To Make Your Work Work Easier/Faster

Functionalty.js (beta) About ✍️ This Is A JavaScript Library To Make Your Work Easier/Faster, You Can See Functionalty.js Website From Here Project Cr

Ali-Eldeba 16 Aug 30, 2022
A JavaScript Library To Make Your Work Work Easier/Faster

Functionality.js (beta) About ✍️ This Is A JavaScript Library To Make Your Work Easier/Faster, You Can See Functionalty.js Website From Here Project C

Ali-Eldeba 9 May 25, 2022