Documentation

Comprehensive guide and API reference for Node Toolkit functions.

Installation

Install via npm

Add the package to your project:

bash
npm install @neeraj-x0/nodetoolkit

Importing

Import specific functions as needed:

bash
const { getExchangeRate, slugify } = require('@neeraj-x0/nodetoolkit');

Network & API Utils

fetchJSON(url)

Fetch JSON with auto-retry logic.

example
const data = await fetchJSON('https://api.example.com/data');

getExchangeRate(from, to)

Get real-time currency rates.

example
const rate = await getExchangeRate('USD', 'EUR');

getPublicIP()

Get your public IP address.

example
const ip = await getPublicIP();

geoLocateIP(ip)

Get location info for an IP.

example
const loc = await geoLocateIP('8.8.8.8');

checkWebsiteStatus(url)

Check if a website is up.

example
const isUp = await checkWebsiteStatus('https://google.com');

downloadFile(url, path)

Download file to local disk.

example
await downloadFile('http://img.com/a.jpg', './a.jpg');

getRandomQuote()

Get a random quote.

example
const quote = await getRandomQuote();

shortenURL(url)

Shorten a long URL.

example
const short = await shortenURL('https://very-long-url.com');

String Utilities

capitalize(str)

Capitalize first letter.

example
capitalize('hello'); // 'Hello'

slugify(str)

Convert string to URL slug.

example
slugify('Hello World'); // 'hello-world'

camelCase(str)

Convert to camelCase.

example
camelCase('hello world'); // 'helloWorld'

truncate(str, len)

Truncate string with ellipses.

example
truncate('long string...', 5); // 'long...'

randomString(len)

Generate random alphanumeric string.

example
randomString(16);

maskString(str)

Mask sensitive data.

example
maskString('1234567890'); // '******7890'

escapeHTML(str)

Escape HTML special chars.

example
escapeHTML('<script>'); // '&lt;script&gt;'

Array & Object Utils

chunk(arr, size)

Split array into chunks.

example
chunk([1,2,3,4], 2); // [[1,2], [3,4]]

shuffle(arr)

Randomly shuffle array.

example
shuffle([1,2,3]);

unique(arr)

Remove duplicates.

example
unique([1,1,2]); // [1,2]

groupBy(arr, key)

Group objects by key.

example
groupBy(users, 'role');

deepClone(obj)

Deep copy an object.

example
const copy = deepClone(original);

Math Utilities

randomInt(min, max)

Random integer in range.

example
randomInt(1, 10);

formatCurrency(num)

Format number as currency.

example
formatCurrency(1000, 'USD'); // '$1,000.00'

clamp(num, min, max)

Constrain number range.

example
clamp(105, 0, 100); // 100

formatCompactNumber(num)

Format large numbers.

example
formatCompactNumber(1500); // '1.5K'

System Utilities

getSystemInfo()

Get OS/CPU/Memory info.

example
console.log(getSystemInfo());

getFileSize(path)

Get human readable file size.

example
getFileSize('./video.mp4'); // '15 MB'

ensureDir(path)

Ensure directory exists.

example
ensureDir('./downloads/images');

getHash(content)

Calculate SHA256 hash.

example
getHash('some content');

Validation

isEmail(str)

Check if valid email.

example
isEmail('test@test.com'); // true

isStrongPassword(str)

Check password strength.

example
isStrongPassword('Pass123!'); // true

isEmpty(val)

Check if null/empty.

example
isEmpty([]); // true

Function Wrappers

retry(fn, retries)

Retry async function on fail.

example
await retry(fetchData, 3);

once(fn)

Ensure function runs only once.

example
const init = once(() => setup());

sleep(ms)

Async delay.

example
await sleep(1000);

debounce(fn, ms)

Debounce execution.

example
const handler = debounce(() => save(), 500);

Audio/Video Conversion

toAudio(buffer, ext)

Converts an audio buffer to a playable audio file.

example
const audioBuffer = fs.readFileSync('input.wav');
const mp3Buffer = await toAudio(audioBuffer, 'mp3');

toVideo(buffer, ext)

Converts video buffer to playable MP4 file.

example
const videoBuffer = await toVideo(buffer, 'mp4');

webp2mp4(source)

Converts animated WebP to MP4 video buffer.

example
const mp4Buffer = await webp2mp4('sticker.webp');