get-random-float
Version:
Gets random float
74 lines (56 loc) • 1.46 kB
JavaScript
/*
Imports
*/
const {
getRandomInt,
getRandomFloat,
likeFloat3,
isFloat,
isDirectChild
} = require('./lib');
/*
Main
*/
function Float(float) {
if (typeof float === "string") {
float = Number(float);
}
if (Object.getPrototypeOf(this) === Float.prototype) {
this.value = float === undefined ? Float.random() : float;
} else {
return float ?? Float.random();
}
}
Float.prototype.valueOf = function() {
return this.value;
};
Float.prototype.toFixed = function (numberOfDigitsAfterDecimalPoint) {
this.value = this.value.toFixed(numberOfDigitsAfterDecimalPoint);
return this;
};
Float.prototype.equals = function(inputFloat) {
return this.value === inputFloat;
}
Float.random = getRandomFloat;
Float.is = (inputValue) => {
if (typeof inputValue !== "number" && typeof inputValue !== "object") {
return false;
}
if (typeof inputValue === "object") {
if (isDirectChild(inputValue, Float)) {
inputValue = inputValue.value;
} else {
return false;
}
}
let result = isFloat(inputValue);
return result;
};
Float.like = (inputValue) => {
let result = likeFloat3(inputValue);
if (result === false) {
result = Float.is(inputValue);
}
return result;
}
module.exports = Float;