Skip to content

Commit

Permalink
Merge pull request #47 from Sv443-Network/version/1.16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Sv443 authored Jun 29, 2022
2 parents 24d4eba + bef9f55 commit 64c6650
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
"ecmaVersion": 2020
},
"rules": {
"no-console": "off"
Expand Down
21 changes: 16 additions & 5 deletions SvCoreLib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ declare module "svcorelib" {
//#SECTION randomization

/**
* 🔹 Highly random number generator with upper and lower boundary. 🔹
* 🔹 Random number generator with upper and lower boundary. 🔹
*
* ❗ Warning! This RNG is not cryptographically secure, so don't do any password hashing or stuff that needs to be highly secure with this function!
* ❗ Warning! This RNG is not cryptographically secure, so don't do any password hashing or stuff that needs to be highly secure with this function!
* @param min Lower boundary of the RNG
* @param max Upper boundary of the RNG
* @throws Throws a TypeError if the arguments are not of type `number`
Expand All @@ -210,15 +210,26 @@ declare module "svcorelib" {
function randRange(min: number, max: number): number;

/**
* 🔹 Highly random number generator with upper boundary.
* 🔹 Random number generator with upper boundary.
* This overload automatically sets the lower boundary to 0. 🔹
*
* ❗ Warning! This RNG is not cryptographically secure, so don't do any password hashing or stuff that needs to be highly secure with this function!
* ❗ Warning! This RNG is not cryptographically secure, so don't do any password hashing or stuff that needs to be highly secure with this function!
* @param max Upper boundary of the RNG - using this overload will set the lower boundary to 0
* @throws Throws a TypeError if the arguments are not of type `number`
* @since 1.14.0
* @version 1.16.0 No longer depending on the `performance` module
*/
function randRange(max: number): number;
function randRange(max: number): number;

/**
* 🔹 Makes sure the provided `num` is always in between `min` and `max` 🔹
* @param num The number to clamp between `min` and `max`
* @param min Lower boundary
* @param max Upper boundary
* @throws Throws a TypeError if the parameters are not of type `number` or are NaN
* @since 1.16.0
*/
function clamp(num: number, min: number, max: number): number;

/**
* 🔹 Randomizes all items in an array and returns it 🔹
Expand Down
1 change: 1 addition & 0 deletions SvCoreLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = {
replaceAt: imp("functions/replaceAt"),
byteLength: imp("functions/byteLength"),
randRange: imp("functions/randRange"),
clamp: imp("functions/clamp"),
randomizeArray: imp("functions/randomizeArray"),
randomItem: imp("functions/randomItem"),
removeDuplicates: imp("functions/removeDuplicates"),
Expand Down
13 changes: 12 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## SvCoreLib - Changelog
### Latest version: [1.15.0](#1150)
### Latest version: [1.16.0](#1160)


<br>
Expand All @@ -9,6 +9,7 @@
<br>

> **SvCoreLib**
> - [1.16.0](#1160)
> - [1.15.0](#1150)
> - [1.14.0](#1140)
> - [1.14.1](#1141)
Expand Down Expand Up @@ -43,6 +44,16 @@

<br><br>

## 1.16.0

- Additions
- Added clamp() to ensure a number is between a min and max limit
- Fixes
- randRange() now doesn't depend on the `performance` module anymore
- Updated deps

<br>

## 1.15.0
- Breaking changes
- Shortened namespace names:
Expand Down
27 changes: 27 additions & 0 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Otherwise, see the table of contents just below.
- [randomItem()](#randomitem) - returns a random item from an array
- [randomizeArray()](#randomizearray) - randomizes the items in an array
- [randRange()](#randrange) - returns a random number in the provided range
- [clamp()](#clamp) - makes sure a number is always in between a min and max limit
- [readableArray()](#readablearray) - converts an array to a better readable string
- [removeDuplicates()](#removeduplicates) - removes duplicate items in an array
- [halves()](#halves) - returns both halves of an array
Expand Down Expand Up @@ -1449,6 +1450,32 @@ This namespace, accessed with just `scl`, offers many miscellaneous functions.
<br><br><br>
> ### clamp()
> Highly random number generator where you can specify an upper and lower boundary.
>
> Specify the upper and lower boundary with the parameters `min` and `max`
> ```ts
> scl.clamp(num: number, min: number, max: number): number
> ```
>
> <br><details><summary><b>Example Code - click to show</b></summary>
>
> ```js
> const { clamp } = require("svcorelib");
>
> for(let i = 1; i <= 10; i++)
> console.log(clamp(i, 3, 6));
>
> // output:
> // 3, 3, 3, 4, 5, 6, 6, 6, 6, 6
> ```
>
> </details>
<br><br><br>
> ### readableArray()
> Converts an array to a better readable string.
>
Expand Down
64 changes: 32 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "svcorelib",
"version": "1.15.0",
"version": "1.16.0",
"description": "Core Library used in the projects of Sv443 and the Sv443 Network. Contains tons of miscellaneous QoL features.",
"main": "SvCoreLib.js",
"scripts": {
"test": "snyk test",
"test": "node test",
"lint": "eslint ."
},
"repository": {
Expand Down
17 changes: 17 additions & 0 deletions src/functions/clamp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const allOfType = require("./allOfType");

function clamp(...params)
{
const [num, min, max] = params;

if(!allOfType(params, "number"))
throw new TypeError("Parameters for clamp() need to be of type number");
if(params.map(p => isNaN(p)).includes(true))
throw new TypeError("Parameters for clamp() can't be NaN");
if(min > max)
throw new TypeError("Parameter min can't be higher than max in clamp()");

return Math.max(min, Math.min(num, max));
}

module.exports = clamp;
21 changes: 8 additions & 13 deletions src/functions/randRange.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
const { performance } = require("perf_hooks");

function randRange(...args)
{
let min, max;

if(typeof args[0] === "number" && typeof args[1] === "number")
{
// using (min, max) overload
// using randRange(min, max)
[ min, max ] = args;
}
else if(typeof args[0] === "number" && typeof args[1] !== "number")
{
// using (max) overload
// using randRange(max)
min = 0;
max = args[0];
}
else
throw new TypeError(`Wrong parameter provided for "min" and/or "max" in scl.randRange() - (expected: "number" and "number | undefined", got: "${typeof min}" and "${typeof max}")`);
throw new TypeError(`Wrong parameter provided in scl.randRange() - (expected: "number" and "number|undefined", got: "${typeof min}" and "${typeof max}")`);

min = parseInt(min);
max = parseInt(max);

if(isNaN(min) || isNaN(max))
throw new TypeError(`Invalid parameters provided in scl.randRange() - "min" and "max" can't be NaN`);

if(min > max)
throw new TypeError(`Invalid parameters provided for "min" and/or "max" in scl.randRange() - make sure "min" is not bigger than "max"`);
max++;
throw new TypeError(`Invalid parameters provided in scl.randRange() - make sure "min" is not bigger than "max"`);

let d = Date.now();
if(typeof performance != "undefined" && typeof performance.now == "function")
d += performance.now();

let r = (d + Math.random() * (max - min)) % (max - min) | 0;
return r += min;
return Math.floor(Math.random() * (max - min + 1)) + min;
}

module.exports = randRange;

0 comments on commit 64c6650

Please sign in to comment.