npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tolga1452/toolbox.js

v1.2.0

Published

A collection of useful functions for JavaScript & Typescript.

Downloads

80

Readme

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.

| Parameter | Type | Description | | :--- | :--- | :--- | | color | Decimal | RGB | The 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.

| Parameter | Type | Description | | :--- | :--- | :--- | | color | Hexadecimal | Decimal | The 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.

| Parameter | Type | Description | | :--- | :--- | :--- | | color | Hexadecimal | RGB | The 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.

| Parameter | Type | Description | | :--- | :--- | :--- | | min | number | The minimum number. | | max | number | The 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.

| Parameter | Type | Description | | :--- | :--- | :--- | | str | string | The 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.

| Parameter | Type | Description | | :--- | :--- | :--- | | arr | any[] | 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.

| Parameter | Type | Description | | :--- | :--- | :--- | | time | number | The time to convert. | | unit | TimeUnit | The 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.

| 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

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.

| Parameter | Type | Description | | :--- | :--- | :--- | | arr | any[] | 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.

| Parameter | Type | Description | | :--- | :--- | :--- | | arr | any[] | The array to chunk. | | size | number | The 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
};