1.2.0 • Published 1 month ago

@tolga1452/toolbox.js v1.2.0

Weekly downloads
-
License
CC-BY-NC-ND-4.0
Repository
github
Last release
1 month ago

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

convertToHex()

convertToHex(color: Decimal | RGB): Hexadecimal)

Converts a decimal or RGB color code to a hexadecimal color code.

ParameterTypeDescription
colorDecimal | RGBThe color code to convert.

Returns: Hexadecimal

Example

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

convertToHex(0x000000); // #000000
convertToHex([0, 0, 0]); // #000000

convertToRGB()

convertToRGB(color: Hexadecimal | Decimal): RGB

Converts a hexadecimal or decimal color code to an RGB color code.

ParameterTypeDescription
colorHexadecimal | DecimalThe color code to convert.

Returns: RGB

Example

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

convertToRGB(0x000000); // [0, 0, 0]
convertToRGB('#000000'); // [0, 0, 0]

convertToDecimal()

convertToDecimal(color: Hexadecimal | RGB): Decimal

Converts a hexadecimal or RGB color code to a decimal color code.

ParameterTypeDescription
colorHexadecimal | RGBThe color code to convert.

Returns: Decimal

Example

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

convertToDecimal([0, 0, 0]); // 0
convertToDecimal('#000000'); // 0

randomNumber()

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

Generates a random number between the given min and max.

ParameterTypeDescription
minnumberThe minimum number.
maxnumberThe maximum number.

Returns: number

Example

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

randomNumber(0, 10); // 5

links()

links(str: string): string[]

Returns the links of the given string.

ParameterTypeDescription
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.

ParameterTypeDescription
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.

ParameterTypeDescription
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.

ParameterTypeDescription
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.

ParameterTypeDescription
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.

ParameterTypeDescription
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"]]

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}`;

TimeUnit

The time units.

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