-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from Sv443-Network/version/1.16.0
- Loading branch information
Showing
9 changed files
with
116 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |