A collection of useful functions for JavaScript & Typescript.
npm install @tolga1452/toolbox.js
const { randomNumber } = require('toolbox.js');
import { randomNumber } from '@tolga1452/toolbox.js';
-
convertToHex()
: Converts a decimal or RGB color code to a hexadecimal color code. -
convertToRGB()
: Converts a hexadecimal or decimal color code to an RGB color code. -
convertToDecimal()
: Converts a hexadecimal or RGB color code to a decimal color code. -
randomNumber()
: Generates a random number between the given min and max. -
links()
: Returns the links of the given string. -
randomItem()
: Returns a random item from the given array. -
toMilliseconds()
: Converts any time unit to milliseconds. -
check()
: Checks whether the given value istrue
orfalse
. If the value istrue
, returns the first parameter, otherwise returns the second parameter. -
shuffle()
: Shuffles the given array. -
chunk()
: Turns the given array into groups of the given size.
convertToHex(color: Decimal | RGB): Hexadecimal)
Converts a decimal or RGB color code to a hexadecimal color code.
Parameter | Type | Description |
---|---|---|
color |
Decimal | RGB
|
The color code to convert. |
Returns: Hexadecimal
import { convertToHex } from '@tolga1452/toolbox.js';
convertToHex(0x000000); // #000000
convertToHex([0, 0, 0]); // #000000
convertToRGB(color: Hexadecimal | Decimal): RGB
Converts a hexadecimal or decimal color code to an RGB color code.
Parameter | Type | Description |
---|---|---|
color |
Hexadecimal | Decimal
|
The color code to convert. |
Returns: RGB
import { convertToRGB } from '@tolga1452/toolbox.js';
convertToRGB(0x000000); // [0, 0, 0]
convertToRGB('#000000'); // [0, 0, 0]
convertToDecimal(color: Hexadecimal | RGB): Decimal
Converts a hexadecimal or RGB color code to a decimal color code.
Parameter | Type | Description |
---|---|---|
color |
Hexadecimal | RGB
|
The color code to convert. |
Returns: Decimal
import { convertToDecimal } from '@tolga1452/toolbox.js';
convertToDecimal([0, 0, 0]); // 0
convertToDecimal('#000000'); // 0
randomNumber(min: number, max: number): number
Generates a random number between the given min and max.
Parameter | Type | Description |
---|---|---|
min |
number |
The minimum number. |
max |
number |
The maximum number. |
Returns: number
import { randomNumber } from '@tolga1452/toolbox.js';
randomNumber(0, 10); // 5
links(str: string): string[]
Returns the links of the given string.
Parameter | Type | Description |
---|---|---|
str |
string |
The string to get the links from. |
Returns: string[]
import { links } from '@tolga1452/toolbox.js';
links("Check out my website: https://www.example.com"); // ["https://www.example.com"]
randomItem(arr: any[]): any
Returns a random item from the given array.
Parameter | Type | Description |
---|---|---|
arr |
any[] |
The array to get the item from. |
Returns: any
import { randomItem } from '@tolga1452/toolbox.js';
randomItem(["red", "green", "blue"]); // "red"
toMilliseconds(time: number, unit: TimeUnit): number
Converts any time unit to milliseconds.
Parameter | Type | Description |
---|---|---|
time |
number |
The time to convert. |
unit |
TimeUnit |
The unit of the time. |
Returns: number
import { toMilliseconds, TimeUnit } from '@tolga1452/toolbox.js';
toMilliseconds(1, TimeUnit.Seconds); // 1000
check(value: any, ifTrue: any, ifFalse: any): any
Checks whether the given value is true
or false
. If the value is true
, returns the first parameter, otherwise returns the second parameter.
Note: You don't have to give a boolean to value
. For example "text"
is returns true
and ""
is returns false
, or 1
is returns true
and 0
is returns false
.
Parameter | Type | Description |
---|---|---|
value |
any |
The value to check. |
ifTrue |
any |
The value to return if the value is true . |
ifFalse |
any |
The value to return if the value is false . |
Returns: any
import { check } from '@tolga1452/toolbox.js';
check(true, "Hello", "World"); // "Hello"
check(false, "Hello", "World"); // "World"
check("text", "Hello", "World"); // "Hello"
check("", "Hello", "World"); // "World"
shuffle(arr: any[]): any[]
Shuffles the given array.
Parameter | Type | Description |
---|---|---|
arr |
any[] |
The array to shuffle. |
Returns: any[]
import { shuffle } from '@tolga1452/toolbox.js';
shuffle(["red", "green", "blue"]); // ["blue", "red", "green"]
chunk(arr: any[], size: number): any[][]
Turns the given array into groups of the given size.
Parameter | Type | Description |
---|---|---|
arr |
any[] |
The array to chunk. |
size |
number |
The size of the chunks. |
Returns: any[][]
import { chunk } from '@tolga1452/toolbox.js';
chunk(["red", "green", "blue", "yellow", "orange"], 2); // [["red", "green"], ["blue", "yellow"], ["orange"]]
A Decimal color code is a number between 0 and 16777215 (0xFFFFFF).
export type Decimal = number;
An RGB color code is an array of 3 numbers between 0 and 255.
export type RGB = [number, number, number];
A Hexadecimal color code is a string that starts with a '#' and is followed by 6 hexadecimal characters.
export type Hexadecimal = `#${string}`;
The time units.
export enum TimeUnit {
Milliseconds,
Seconds,
Minutes,
Hours,
Days,
Weeks,
Months,
Years
};