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

rand-helper

v2.0.0

Published

The module rand-helper is a JavaScript library designed to generate random values of different data types and manipulate numbers.

Downloads

22

Readme

RandomHelper

npm version GitHub license GitHub issues GitHub stars

rand-helper is a JavaScript library designed to generate random values of different data types and manipulate numbers. The module uses 5 algorithms to generate a random number.

There may be mistakes! I did not finish some algorithms, some module functions may not work

Installation

To install the rand-helper module, run one of the following commands:

npm install rand-helper   // for stable version
npm install git+https://github.com/Coder-TheBeJIIHiu/randomHelper   // for the latest beta version

Telegram chat

CLICK

GROUP

Usage

Examples

const { getRandomInt, getRandomString, getRandomElement, getRandomIntArray, getRandomHex, getRandomRGB, getRandomHSV, getRandomBoolean, dropChance } = require('rand-helper');

// Generate a random integer between 0 and 10
const randomNumber = getRandomInt(0, 10); // 4

// Generate a random string of a given length
const randomString = getRandomString(12, {
  useCaps: true,
  useInt: true,
  useSpecialSymbols: false,
  useRussianLetters: false,
  useRussianLettersCaps: false
}); // GBRnGa9QOp4

// Generate a random element from an array
const arr = ['apple', 'banana', 'orange'];
const randomElement = getRandomElement(arr); // apple

const randomIntArray = getRandomIntArray(1, 10, 5); // [ 8, 5, 9, 3, 7 ]

const randomHex = getRandomHex({
  includeHash: true
}); // #cf5eb8

const randomRGB = getRandomRGB({
  includeAlpha: true,
  useArray: false
}); // rgba(224, 50, 129, 0.24)

const randomHSV = getRandomHSV({
  includeAlpha: true,
  useArray: false
}); // hsva(343, 0.49, 0.33, 0.55)

const randomBoolean = getRandomBoolean(); // false

const items = [
    { n: 'Apple', dropChance: 1 },
    { n: 'Knife', dropChance: 20 },
    { n: 'Spoon', dropChance: 50 },
    { n: 'Ice Cream', dropChance: 70 }
];
const index = dropChance(items);
  
if (index === -1) {
    console.log('No item was dropped.');
} else {
    const itemName = items[index].n;
    console.log(`You got a ${itemName}!`); // You got a Spoon!
}

F module

const { F } = require('rand-helper');

const myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const myData = [
  { name: 'Alice', age: '32' },
  { name: 'Bob', age: '24' },
  { name: 'Charlie', age: '45' },
  { name: 'Dave', age: '19' },
  { name: 'Eve', age: '5' },
  { name: 'Frank', age: '100' }
];

const myRange = F.range(1, 10); // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

const myChunks = F.chunk(myArray, 3); // [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]

// Sort data by name (in alphabetical order)
const dataSortedByName = F.sortBy(data, { sortby: 'name' });
  
// Output: [
//   { name: 'Alice', age: '32' },
//   { name: 'Bob', age: '24' },
//   { name: 'Charlie', age: '45' },
//   { name: 'Dave', age: '19' },
//   { name: 'Eve', age: '5' },
//   { name: 'Frank', age: '100' }
// ]
  
// Sort data by age (in ascending order)
const dataSortedByAge = F.sortBy(data, { sortby: 'age', numeric: true });

// Output: [
//   { name: 'Eve', age: '5' },
//   { name: 'Dave', age: '19' },
//   { name: 'Bob', age: '24' },
//   { name: 'Alice', age: '32' },
//   { name: 'Charlie', age: '45' },
//   { name: 'Frank', age: '100' }
// ]
  
// Sort data by age (in descending order)
const dataSortedByAgeDesc = F.sortBy(data, { sortby: 'age', numeric: true, reverse: true });

// Output: [
//   { name: 'Frank', age: '100' },
//   { name: 'Charlie', age: '45' },
//   { name: 'Alice', age: '32' },
//   { name: 'Bob', age: '24' },
//   { name: 'Dave', age: '19' },
//   { name: 'Eve', age: '5' }
// ]

F.debounced example

const { F } = require('rand-helper')

// Example 1: Debouncing a function that writes to a file
const fs = require('fs');

const writeFileDebounced = F.debounced(fs.writeFile, 500);

// Write to a file multiple times in quick succession
writeFileDebounced('file.txt', 'Hello world 1', () => {}); // Writes data after 500ms
writeFileDebounced('file.txt', 'Hello world 2', () => {}); // Cancels previous write, writes new data after 500ms
writeFileDebounced('file.txt', 'Hello world 3', () => {}); // Cancels previous write, writes new data after 500ms

// Example 2: Debouncing a function that calculates a sum
const sum = (a, b) => {
  console.log(`Calculating sum of ${a} and ${b}...`);
  return a + b;
};

const debouncedSum = F.debounced(sum, 1000);

// Calculate the sum multiple times in quick succession
console.log(debouncedSum(2, 3)); // Returns undefined
console.log(debouncedSum(4, 5)); // Returns undefined
console.log(debouncedSum(6, 7)); // Cancels previous calculation, returns 13 after 1000ms