@amitind/util
v1.1.1
Published
Some useful Javascript functions that I use.
Downloads
97
Readme
@amitind/util
TypeScript-ready JavaScript utility functions to save time in your projects.
Installation
NPM
npm i @amitind/util
JSR (Deno)
deno add jsr:@impl/util
Usage
ES Modules
import { randomString, randomNumber } from '@amitind/util';
// or for JSR
import { randomString, randomNumber } from '@impl/util';
CommonJS
const { randomString, randomNumber } = require('@amitind/util');
// or for JSR
const { randomString, randomNumber } = require('@impl/util');
Available Functions
Array Utilities
shuffleArray<T>(array: T[]): T[]
- Randomly shuffles an arrayrandomArrayItem<T>(array: T[]): T
- Returns a random item from an arrayrandomItemsArray<T>(array: T[], count?: number): T[]
- Returns multiple random items from an arrayuniqueArrayItems<T>(array: T[]): T[]
- Returns array with unique items
Random Generators
randomString(length?: number): string
- Generates a random stringrandomNumber(min?: number, max?: number): number
- Generates a random number in rangebinary(): boolean
- Returns a random boolean value
DOM Utilities
querySelector<T>(selector: string, parent?: ParentNode): T | null
- Enhanced querySelectorquerySelectorAll<T>(selector: string, parent?: ParentNode): T[]
- Enhanced querySelectorAlltoggleClass(id: string, className?: string): void
- Toggle class on element
Examples
// Generate a random 8-character string
const str = randomString(8); // => "k3m1p9x2"
// Get a random number between 1 and 10
const num = randomNumber(1, 10); // => 7
// Shuffle an array
const arr = shuffleArray(['😏', '🤣', '🙅🏻♂️', '🎂']);
// => [ '🙅🏻♂️', '🤣', '😏', '🎂' ]
// Get random items from array
const items = randomItemsArray(['A', 'B', 'C', 'D'], 2);
// => ["C", "A"]
License
MIT © Amit Yadav