simplified-javascript
Version:
simplified-javascript is a beginner-friendly package to make coding easier.
262 lines (231 loc) • 8.24 kB
JavaScript
const fs = require('fs')
/**
* Returns a random number between a specifc minimum and maxiumum.
* @param {Number} Min
* @param {Number} Max
* @example
* @returns {Number}
*/
function randomLimit(Min, Max) {
if(!Min || !Max) throw new TypeError('Minimum or Maximum cannot be as left void')
if(isNaN(Min || Max)) throw new TypeError('The Minimum or Maximum must be number.')
return Math.floor(Math.random() * (Max + 1 - Min) + Min)
}
/**
* Generates a random number
* @param {Number} Max
* @example
* console.log(randomNumber(10))
* @returns
* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 10
*/
function randomNumber(Max) {
if (!Max) throw new TypeError('Maximum cannot be left as void')
if (isNaN(Max)) throw new TypeError('The Maximum must be number.')
return Math.floor(Math.random() * (Max + 1))
}
/**
* Gets a Random Element from an Array
* @param {Array} _Array_
* @example
* var anArray = [ 'm-1', 'm-2', 'm-3' ]
* console.log(randomElement(anArray))
* @returns
* It will be randomized whether 'm-1', 'm-2', or 'm-3'
*/
function randomElement(_Array_) {
if (!Array.isArray(_Array_)) throw new TypeError('Invalid parameter, it has to be an Array.')
return _Array_[Math.floor(Math.random() * _Array_.length)]
}
/**
* Determines if the declaration is an Object
* @param {any} Variable
* @example
* const Example = {
* Object: 'Text'
* }
* if (isObject(Example) === true) return console.log('It is an Object')
* if (isObject(Example) === false) return console.log('It is not an Object')
*/
function isObject(Variable) {
if (!Variable) throw new TypeError('Variable cannot be left as void.')
return Object.prototype.toString.call(Variable) === '[object Object]'
}
/**
* Gets a Random Property from an Object
* @param {Object} Object
* @example
* const anObject = {
* Object_1: 'Solid',
* Object_2: 'Liquid',
* Object_3: 'Gas'
* }
* console.log(randomProperty(anObject))
* @returns
* It will be randomized whether 'solid', 'liquid', or 'gas'
*/
function randomProperty(Object) {
if (!isObject(Object)) throw new TypeError('Invalid parameter, it has to be an Object.')
const Keys = Object.keys(Object)
return Object[Keys[ Keys.length * Math.random() << 0 ]]
}
/**
* Determines if the declaration is a Number
* @param {Number} Variable
* @example
* const Example = 'abcde'
* if (isNumber(Example) === true) return console.log('It is a Number')
* if (isNumber(Example) === false) return console.log('It is a not Number')
*/
function isNumber(Variable) {
if (!Variable) throw new TypeError('Parameter cannot be left as void.')
return typeof(Variable) == 'number'
}
/**
* Generates a Random String with a specified length
* @param {Number} Length
* @example
* console.log(randomString(10))
* @returns
* HI5RnwMCEI
*/
function randomString(Length) {
if (!Length) throw new TypeError('Length cannot be left as void.')
if (isNaN(Length)) throw new TypeError('Invalid parameter, it has to be a Number.')
const Characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
let String = ''
for (let i = 0; i < Length; i++) {
String += Characters.charAt(Math.floor(Math.random() * Characters.length))
}
return String
}
/**
* Determines if the declaration is a String
* @param {String} Variable
* @example
* const Example = 'Example of a String'
* if (isString(Example) === true) return console.log('It is a String')
* if (isString(Example) === false) return console.log('This is not a String')
*/
function isString(Variable) {
if (!Variable) throw new TypeError('Parameter cannot be left as void.')
return typeof(Variable) == 'string'
}
/**
* Determines if the declaration is a Function
* @param {Function} Variable
* @example
* if (isFunction(isNumber) == true) console.log('It is a Function')
* if (isFunction(isNumber) == false) console.log('It is not a Function')
*/
function isFunction(Variable) {
if (!Variable) throw new TypeError('Parameter cannot be left as void.')
return Object.prototype.toString.call(Variable).slice(8, -1) === 'Function'
}
/**
* Converts Numbers inside of an Array to Strings
* @param {Array} _Array_
* @example
* const arrayOfNums = [
* 1,2,3,4,5
* ]
* console.log(arrayNumbersToString(arrayOfNums))
* @returns \[ '1', '2', '3', '4', '5' ]
*/
function arrayNumbersToString(_Array_) {
if (!_Array_) throw new TypeError('Array cannot be left as void.')
if (!Array.isArray(_Array_)) throw new TypeError('Invalid parameter, it has to be an Array')
return _Array_.map(String)
}
/**
* Gets the half value of the given number
* @param {Number} Number
* @example
* console.log(halfNumber(100))
* @returns 50
*/
function halfNumber(Number) {
if (!Number) throw new TypeError('Number cannot be left as void.')
if (isNaN(Number)) throw new TypeError('Invalid Number, it has to be a Number.')
const Result = (1/2) * parseInt(Number)
return Result
}
/**
* @param {Array} _Array_
*/
function arrayShuffle(_Array_) {
for (let i = 0, length = _Array_.length, Swap = 0, Temp = ''; i < length; i++) {
Swap = Math.floor(Math.random() * (i + 1))
Temp = _Array_[Swap]
_Array_[Swap] = _Array_[i]
_Array_[i] = Temp
}
return _Array_
}
/**
* @param {Array} Chances
*/
function Percentage_Chance(Values, Chances) {
for (let i = 0, Empty_Array = []; i < Chances.length; i++) {
for (let j = 0; j < Chances[i]; j++) {
Empty_Array.push(i)
}
}
return Values[arrayShuffle(Empty_Array)['0']]
}
/**
* Sets a percentage of getting an element inside an array
* @param {Array} Array
* @param {Array | Number} Chances
* @example
* const Elements = [ 'Mage', 'Necromancer', 'Wizard' ]
* const Chance = [ 80, 40, 20 ]
* console.log(Chances(Elements, Chance))
*/
function Chances(Array, Chances) {
if (isNaN(...Chances)) throw new TypeError('Invalid Array of Numbers, it has to be a Number inside of an Array.')
if (isNaN(parseInt(...Chances))) throw new TypeError('Invalid Array of Numbers, it has to be a Number inside of an Array.')
if (isString(Array)) throw new TypeError('Invalid parameter, it has to be an Array.')
if (isObject(Array)) throw new TypeError('Invalid parameter, it has to be an Array.')
if (isFunction(Array)) throw new TypeError('Invalid parameter, it has to be an Array.')
const Checking_Chances = Chances.sort()
if (Checking_Chances.length === 1) throw new TypeError('The Chances has to have at least 2 elements.')
if (Checking_Chances.length === 0) throw new TypeError('The Chances cannot be empty.')
if (isString(Chances)) throw new TypeError('Invalid Array of Numbers, it has to be a Number inside of an Array.')
if (isObject(Chances)) throw new TypeError('Invalid Array of Numbers, it has to be a Number inside of an Array.')
if (isFunction(chances)) throw new TypeError('Invalid Array of Numbers, it has to be a Number inside of an Array.')
const Checking_Array = Array.sort()
if (Checking_Array.length === 1) throw new TypeError('The Array has to have at least 2 elements.')
if (Checking_Array.length === 0) throw new TypeError('The Array cannot be empty.')
for (var i = 0; i < 20; i++) {
return Percentage_Chance(Array, Chances)
}
}
/**
* Capitalizes a string.
* @param {String} Message
* @example
* const Message = 'rune'
* console.log(Capitalize(Message))
* @returns {String}
*/
function Capitalize(Message) {
const Capitalizing = Message[0].toUpperCase()
const Capitalized = Capitalizing + Message.slice(1)
return Capitalized
}
module.exports = {
randomLimit,
randomNumber,
randomElement,
isObject,
randomProperty,
isNumber,
randomString,
isString,
isFunction,
arrayNumbersToString,
halfNumber,
Chances,
Capitalize,
}