Skip to main content

toolbox.js

What is it?​

A collection of useful functions for JavaScript & Typescript.

Installation​

npm install @tolga1452/toolbox.js

Usage​

JavaScript​

const { randomNumber } = require('toolbox.js');

TypeScript​

import { randomNumber } from '@tolga1452/toolbox.js';

Functions​

Jump to Types

isDecimal()​

isDecimal(value: any): boolean

Checks whether the given value is a Decimal color code.

ParameterTypeDescriptionRequiredDefault
valueanyThe value to check.✅-

Returns: boolean

Example​

import { isDecimal } from '@tolga1452/toolbox.js';

isDecimal(3159888); // true
isDecimal("#4C8DFF"); // false

isRgb()​

isRgb(value: any): boolean

Checks whether the given value is a RGB color code.

ParameterTypeDescriptionRequiredDefault
valueanyThe value to check.✅-

Returns: boolean

Example​

import { isRgb } from '@tolga1452/toolbox.js';

isRgb([155, 119, 75]); // true
isRgb("#9B774B"); // false

isHex()​

isHex(value: any): boolean

Checks whether the given value is a Hexadecimal color code.

ParameterTypeDescriptionRequiredDefault
valueanyThe value to check.✅-

Returns: boolean

Example​

import { isHex } from '@tolga1452/toolbox.js';

isHex("#9B774B"); // true
isHex(3159888); // false

isHsl()​

isHsl(value: any): boolean

Checks whether the given value is a HSL color code.

ParameterTypeDescriptionRequiredDefault
valueanyThe value to check.✅-

Returns: boolean

Example​

import { isHsl } from '@tolga1452/toolbox.js';

isHsl([30, 100, 50]); // true
isHsl("#9B774B"); // false

convertToHex()​

convertToHex(color: Color | Hsl, fromHsl: boolean = false): Hexadecimal

Converts the given color code to a Hexadecimal color code.

ParameterTypeDescriptionRequiredDefault
colorColor | HslThe color code to convert.✅-
fromHslbooleanWhether the color code is a HSL color code.❌false

Returns: Hexadecimal

Example​

import { convertToHex } from '@tolga1452/toolbox.js';

convertToHex(4528206); // "#45184e"
convertToHex([69, 24, 78]); // "#45184e"
convertToHex([290, 53, 20], true); // "#45184e"

convertToRgb()​

convertToRgb(color: Color | Hsl, fromHsl: boolean = false): Rgb

Converts the given color code to an RGB color code.

ParameterTypeDescriptionRequiredDefault
colorColor | HslThe color code to convert.✅-
fromHslbooleanWhether the color code is a HSL color code.❌false

Returns: Rgb

Example​

import { convertToRgb } from '@tolga1452/toolbox.js';

convertToRgb(7313317); // [111, 151, 165]
convertToRgb("#6F97A5"); // [111, 151, 165]
convertToRgb([196, 23, 54], true); // [111, 151, 165]

convertToDecimal()​

convertToDecimal(color: Color | Hsl, fromHsl: boolean = false): Decimal

Converts the given color code to an Decimal color code.

ParameterTypeDescriptionRequiredDefault
colorColor | HslThe color code to convert.✅-
fromHslbooleanWhether the color code is a HSL color code.❌false

Returns: Decimal

Example​

import { convertToDecimal } from '@tolga1452/toolbox.js';

convertToDecimal([227, 84, 117]); // 14898293
convertToDecimal("#e35475"); // 14898293
convertToDecimal([346, 72, 61], true); // 14898293

convertToHsl()​

convertToHsl(color: Color): Hsl

Converts the given color code to an HSL color code.

ParameterTypeDescriptionRequiredDefault
colorColorThe color code to convert.✅-

Returns: Hsl

Example​

import { convertToHsl } from '@tolga1452/toolbox.js';

convertToHsl(3444029); // [126, 46, 38]
convertToHsl("#348d3d"); // [126, 46, 38]
convertToHsl([52, 141, 61]); // [126, 46, 38]

randomNumber()​

randomNumber(min: number, max: number): number

Generates a random number between the given min and max.

ParameterTypeDescriptionRequiredDefault
minnumberThe minimum number.✅-
maxnumberThe maximum number.✅-

Returns: number

Example​

import { randomNumber } from '@tolga1452/toolbox.js';

randomNumber(0, 10); // 5

links(str: string): string[]

Returns the links of the given string.

ParameterTypeDescriptionRequiredDefault
strstringThe string to get the links from.✅-

Returns: string[]

Example​

import { links } from '@tolga1452/toolbox.js';

links("Check out my website: https://www.example.com"); // ["https://www.example.com"]

randomItem()​

randomItem(arr: any[]): any

Returns a random item from the given array.

ParameterTypeDescriptionRequiredDefault
arrany[]The array to get the item from.✅-

Returns: any

Example​

import { randomItem } from '@tolga1452/toolbox.js';

randomItem(["red", "green", "blue"]); // "red"

toMilliseconds()​

toMilliseconds(time: number, unit: TimeUnit): number

Converts any time unit to milliseconds.

ParameterTypeDescriptionRequiredDefault
timenumberThe time to convert.✅-
unitTimeUnitThe unit of the time.✅-

Returns: number

Example​

import { toMilliseconds, TimeUnit } from '@tolga1452/toolbox.js';

toMilliseconds(1, TimeUnit.Seconds); // 1000

check()​

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.

ParameterTypeDescriptionRequiredDefault
valueanyThe value to check.✅-
ifTrueanyThe value to return if the value is true.✅-
ifFalseanyThe value to return if the value is false.✅-

Returns: any

Example​

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()​

shuffle(arr: any[]): any[]

Shuffles the given array.

ParameterTypeDescriptionRequiredDefault
arrany[]The array to shuffle.✅-

Returns: any[]

Example​

import { shuffle } from '@tolga1452/toolbox.js';

shuffle(["red", "green", "blue"]); // ["blue", "red", "green"]

chunk()​

chunk(arr: any[], size: number): any[][]

Turns the given array into groups of the given size.

ParameterTypeDescriptionRequiredDefault
arrany[]The array to chunk.✅-
sizenumberThe size of the chunks.✅-

Returns: any[][]

Example​

import { chunk } from '@tolga1452/toolbox.js';

chunk(["red", "green", "blue", "yellow", "orange"], 2); // [["red", "green"], ["blue", "yellow"], ["orange"]]

factorial()​

factorial(n: number): number

Returns the factorial of the given number.

ParameterTypeDescriptionRequiredDefault
nnumberThe number to get the factorial of.✅-

Returns: number

Example​

import { factorial } from '@tolga1452/toolbox.js';

factorial(5); // 120

binomialCoefficient()​

binomialCoefficient(n: number, k: number): number

Returns the binomial coefficient of the given numbers.

ParameterTypeDescriptionRequiredDefault
nnumberThe first number.✅-
knumberThe second number.✅-

Returns: number

Example​

import { binomialCoefficient } from '@tolga1452/toolbox.js';

binomialCoefficient(5, 2); // 10

binomialDistributionProbability()​

binomialDistributionProbability(successes: number, trials: number, probability: number): number

Returns the probability of the binomial distribution.

Suppose a biased coin comes up heads with probability 0.3 when tossed. The probability of seeing exactly 4 heads in 6 tosses is binomialDistributionProbability(4, 6, 0.3).

ParameterTypeDescriptionRequiredDefault
successesnumberThe number of successes.✅-
trialsnumberThe number of trials.✅-
probabilitynumberThe probability of success. The value must be between 0 and 1.✅-

Returns: number

Example​

import { binomialDistributionProbability } from '@tolga1452/toolbox.js';

binomialDistributionProbability(4, 6, 0.3); // 0.05953499999999999

colorBrightness()​

colorBrightness(color: Color | Hsl, fromHsl: boolean = false): number

Returns the brightness of the given color.

ParameterTypeDescriptionRequiredDefault
colorColor | HslThe color to get the brightness of.✅-
fromHslbooleanWhether the color is a HSL color code.❌false

Returns: number between 0 and 255

Example​

import { colorBrightness } from '@tolga1452/toolbox.js';

colorBrightness(6750105); // 197.625
colorBrightness("#66ff99"); // 197.625
colorBrightness([102, 255, 153]); // 197.625
colorBrightness([140, 100, 70], true); // 197.625

isLightColor()​

isLightColor(color: Color | Hsl, fromHsl: boolean = false): boolean

Checks whether the given color is a light color.

ParameterTypeDescriptionRequiredDefault
colorColor | HslThe color to get the brightness of.✅-
fromHslbooleanWhether the color is a HSL color code.❌false

Returns: boolean

Example​

import { isLightColor } from '@tolga1452/toolbox.js';

isLightColor(6750105); // true
isLightColor("#66ff99"); // true
isLightColor([102, 255, 153]); // true
isLightColor([140, 100, 70], true); // true

lightenHslColor()​

lightenHslColor(color: Hsl, amount: number = 25): Hsl

Makes the given HSL color lighter. This function is HSL only. For other color types, use lightenColor.

ParameterTypeDescriptionRequiredDefault
colorHslThe HSL color to lighten.✅-
amountnumberThe amount to lighten the color by. The value must be between 0 and 100.❌25

Returns: Hsl

Example​

import { lightenHslColor } from '@tolga1452/toolbox.js';

lightenHslColor([30, 100, 50]); // [30, 100, 80]
lightenHslColor([30, 100, 50], 50); // [30, 100, 100]

darkenHslColor()​

darkenHslColor(color: Hsl, amount: number = 25): Hsl

Makes the given HSL color darker. This function is HSL only. For other color types, use darkenColor.

ParameterTypeDescriptionRequiredDefault
colorHslThe HSL color to darken.✅-
amountnumberThe amount to darken the color by. The value must be between 0 and 100.❌25

Returns: Hsl

Example​

import { darkenHslColor } from '@tolga1452/toolbox.js';

darkenHslColor([30, 100, 50]); // [30, 100, 20]
darkenHslColor([30, 100, 50], 50); // [30, 100, 0]

lightenColor()​

lightenColor(color: Color, amount: number = 25): typeof color

Makes the given color lighter. For HSL colors, use lightenHslColor.

ParameterTypeDescriptionRequiredDefault
colorColorThe color to lighten.✅-
amountnumberThe amount to lighten the color by. The value must be between 0 and 100.❌25

Returns: typeof color

Example​

import { lightenColor } from '@tolga1452/toolbox.js';

lightenColor(39219); // 1769318
lightenColor("#009933"); // "#1aff66"
lightenColor("#009933", 50); // "#99ffbb"
lightenColor([0, 153, 51]); // [26, 255, 102]

darkenColor()​

darkenColor(color: Color, amount: number = 25): typeof color

Makes the given color darker. For HSL colors, use darkenHslColor.

ParameterTypeDescriptionRequiredDefault
colorColorThe color to darken.✅-
amountnumberThe amount to darken the color by. The value must be between 0 and 100.❌25

Returns: typeof color

Example​

import { darkenColor } from '@tolga1452/toolbox.js';

darkenColor(6750105); // 58957
darkenColor("#66ff99"); // "#00e64d"
darkenColor("#66ff99", 50); // "#006622"
darkenColor([102, 255, 153]); // [140, 100, 45]

Types​

Decimal​

A Decimal color code is a number between 0 and 16777215 (0xFFFFFF).

export type Decimal = number;

Rgb​

An RGB color code is an array of 3 numbers between 0 and 255.

export type Rgb = [number, number, number];

Hexadecimal​

A Hexadecimal color code is a string that starts with a '#' and is followed by 6 hexadecimal characters.

export type Hexadecimal = `#${string}`;

Hsl​

An HSL color code is an array of 3 numbers. The first number is the hue, the second number is the saturation, and the third number is the lightness.

export type Hsl = [number, number, number];

Color​

A color code can be a Decimal, RGB, or Hexadecimal color code.

export type Hsl = Decimal | Rgb | Hexadecimal;

TimeUnit​

The time units.

export enum TimeUnit {
Milliseconds,
Seconds,
Minutes,
Hours,
Days,
Weeks,
Months,
Years
};