Skip to content

Commit

Permalink
Build 1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
BenOvermyer committed Jan 25, 2024
1 parent d667707 commit 55e74b9
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 54 deletions.
47 changes: 47 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,54 @@
/**
* This function takes a maximum value and returns a random number between 1
* and that value.
*
* @param {Number} max - The maximum value to return
* @returns {Number} - A random number between 1 and the maximum value
*/
export declare function simple(max: number): number;
/**
* This function takes an array and returns a random item from it.
*
* @param {Array} items - The array to get the item from
* @returns {Any} - A random item from the array
*/
export declare function item(items: any[]): any;
/**
* This function takes a minimum and maximum value and returns a random float
* between them, weighted towards the middle.
*
* @param {Number} min - The minimum value to return
* @param {Number} max - The maximum value to return
* @returns {Number} - A random float between the minimum and maximum values
*/
export declare function bellFloat(min: number, max: number): number;
/**
* This function takes an array and returns a random set of items from it.
*
* @param {Number} itemCount - The number of items to return
* @param {Array} items - The array to get the items from
* @returns {Array} - A random set of items from the array
*/
export declare function randomSet(itemCount: number, items: any[]): any[];
/**
* This function takes a length and returns a random string of that length.
*
* @param {Number} length - The length of the string to return
* @returns {String} - A random string of the specified length
*/
export declare function randomString(length: number): string;
/**
* This function takes an array and returns a shuffled version of it.
*
* @param {Array} items - An array of items to shuffle
* @returns {Array} - A shuffled version of the array
*/
export declare function shuffle(items: any[]): any[];
/**
* This function takes an array of objects with a commonality property
* and returns a random result, weighted by the commonality property.
*
* @param {Array} items - An array of objects with a commonality property
* @returns {Object} - A random object from the array, weighted by the commonality property
*/
export declare function weighted(items: any[]): any;
107 changes: 53 additions & 54 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,74 @@
'use strict';
import random from 'random';
import random from "random";
/**
* This function takes a maximum value and returns a random number between 1
* and that value.
*
* @param {Number} max - The maximum value to return
* @returns {Number} - A random number between 1 and the maximum value
*/
export function simple(max) {
/**
* This function takes a maximum value and returns a random number between 1
* and that value.
*
* @param {Number} max - The maximum value to return
* @returns {Number} - A random number between 1 and the maximum value
*/
return random.int(1, max);
}
/**
* This function takes an array and returns a random item from it.
*
* @param {Array} items - The array to get the item from
* @returns {Any} - A random item from the array
*/
export function item(items) {
/**
* This function takes an array and returns a random item from it.
*
* @param {Array} items - The array to get the item from
* @returns {Any} - A random item from the array
*/
return items[random.int(0, items.length - 1)];
}
/**
* This function takes a minimum and maximum value and returns a random float
* between them, weighted towards the middle.
*
* @param {Number} min - The minimum value to return
* @param {Number} max - The maximum value to return
* @returns {Number} - A random float between the minimum and maximum values
*/
export function bellFloat(min, max) {
/**
* This function takes a minimum and maximum value and returns a random float
* between them, weighted towards the middle.
*
* @param {Number} min - The minimum value to return
* @param {Number} max - The maximum value to return
* @returns {Number} - A random float between the minimum and maximum values
*/
const divisor = (max - min) / 3;
let result = min;
for (let i = 0; i < 3; i++) {
result += random.float(0, divisor);
}
return result;
}
/**
* This function takes an array and returns a random set of items from it.
*
* @param {Number} itemCount - The number of items to return
* @param {Array} items - The array to get the items from
* @returns {Array} - A random set of items from the array
*/
export function randomSet(itemCount, items) {
/**
* This function takes an array and returns a random set of items from it.
*
* @param {Number} itemCount - The number of items to return
* @param {Array} items - The array to get the items from
* @returns {Array} - A random set of items from the array
*/
let result = [];
const result = [];
items = shuffle(items);
for (let i = 0; i < itemCount; i++) {
result.push(items.pop());
}
return result;
}
/**
* This function takes a length and returns a random string of that length.
*
* @param {Number} length - The length of the string to return
* @returns {String} - A random string of the specified length
*/
export function randomString(length) {
/**
* This function takes a length and returns a random string of that length.
*
* @param {Number} length - The length of the string to return
* @returns {String} - A random string of the specified length
*/
let result = '';
let result = "";
for (let i = 0; i < length; i++) {
result += Math.random().toString(36).slice(2)[0];
}
return result;
}
/**
* This function takes an array and returns a shuffled version of it.
*
* @param {Array} items - An array of items to shuffle
* @returns {Array} - A shuffled version of the array
*/
export function shuffle(items) {
/**
* This function takes an array and returns a shuffled version of it.
*
* @param {Array} items - An array of items to shuffle
* @returns {Array} - A shuffled version of the array
*/
for (let i = items.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * i);
const temp = items[i];
Expand All @@ -78,21 +77,21 @@ export function shuffle(items) {
}
return items;
}
/**
* This function takes an array of objects with a commonality property
* and returns a random result, weighted by the commonality property.
*
* @param {Array} items - An array of objects with a commonality property
* @returns {Object} - A random object from the array, weighted by the commonality property
*/
export function weighted(items) {
/**
* This function takes an array of objects with a commonality property
* and returns a random result, weighted by the commonality property.
*
* @param {Array} items - An array of objects with a commonality property
* @returns {Object} - A random object from the array, weighted by the commonality property
*/
let ceiling = 0;
if (items.length == 1) {
if (items.length === 1) {
return items[0];
}
items.forEach(function (item) {
for (const item of items) {
ceiling += item.commonality;
});
}
let randomValue = random.int(0, ceiling);
for (let i = 0; i < items.length; i++) {
const item = items[i];
Expand Down

0 comments on commit 55e74b9

Please sign in to comment.