Utility functions for all JavaScript/TypeScript environments.
To install the package, use npm:
npm install @andranik-arakelyan/js-utilities
Import the utilities you need in your project:
import {arraySubtract} from '@andranik-arakelyan/js-utilities';
const result = arraySubtract([1, 3, 5] , [ 1, 2, 3]);
console.log( 'result', result ); // [5]
A generic type-safe implementation of a Stack data structure with LIFO (Last-In-First-Out) operations.
import { Stack } from '@andranik-arakelyan/js-utilities';
// Create a new stack of numbers
const stack = new Stack<number>();
// Push elements onto the stack
stack.push(1);
stack.push(2);
stack.push(3);
console.log(stack.peek()); // 3 (returns the top element without removing it)
console.log(stack.pop()); // 3 (removes and returns the top element)
console.log(stack.size()); // 2 (returns the current number of elements)
console.log(stack.isEmpty()); // false
A generic type-safe implementation of a Queue data structure with FIFO (First-In-First-Out) operations.
import { Queue } from '@andranik-arakelyan/js-utilities';
// Create a new queue of strings
const queue = new Queue<string>();
// Enqueue elements
queue.enqueue("first");
queue.enqueue("second");
queue.enqueue("third");
console.log(queue.peek()); // "first" (returns the front element without removing it)
console.log(queue.dequeue()); // "first" (removes and returns the front element)
console.log(queue.size()); // 2 (returns the current number of elements)
console.log(queue.isEmpty()); // false
Subtracts elements of one array from another.
import { arraySubtract } from '@andranik-arakelyan/js-utilities';
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [1, 3, 5];
const result = arraySubtract(arr1, arr2);
console.log(result); // [2, 4]
Splits an array into chunks based on a separator.
import { arraySplit } from '@andranik-arakelyan/js-utilities';
// Using a value as separator
const result1 = arraySplit([1, 2, 3, 0, 4, 5, 0, 6], 0);
console.log(result1); // [[1, 2, 3], [4, 5], [6]]
// Using a function as separator
const result2 = arraySplit([1, 2, 3, 4, 5, 6], (item) => item % 2 === 0);
console.log(result2); // [[1], [3], [5]]
Randomly shuffles an array using the Fisher-Yates algorithm. This algorithm is superior to naive shuffling approaches (like sorting with a random comparator) because it guarantees a truly random permutation with equal probability for each possible outcome and has optimal O(n) time complexity.
import { shuffle } from '@andranik-arakelyan/js-utilities';
const array = [1, 2, 3, 4, 5];
const shuffled = shuffle(array);
console.log(shuffled); // Example output: [3, 1, 5, 2, 4]
console.log(array); // Original array remains unchanged: [1, 2, 3, 4, 5]
Generates a random integer in a specified range.
import { randomInt } from '@andranik-arakelyan/js-utilities';
// Random number between 1 and 10
const random = randomInt(10, 1);
console.log(random); // Example output: 7
Generates a random boolean value.
import { randomBoolean } from '@andranik-arakelyan/js-utilities';
const random = randomBoolean();
console.log(random); // Either true or false
Returns information about the current code execution context.
import { currentCodeInfo } from '@andranik-arakelyan/js-utilities';
function exampleFunction() {
const info = currentCodeInfo();
console.log(info);
// Output:
// {
// className: "",
// methodName: "exampleFunction",
// filepath: "/path/to/your/file.js",
// filename: "file.js",
// lineNumber: 4,
// columnNumber: 15
// }
}
exampleFunction();
Creates a promise that resolves after a specified delay.
import { wait } from '@andranik-arakelyan/js-utilities';
// Wait for 1 second
await wait(1000);
// Chain with other operations
wait(500).then(() => console.log('Half a second has passed'));
// Use in an async function
async function delayedOperation() {
console.log('Starting');
await wait(2000);
console.log('2 seconds have passed');
}
Retries an async function with configurable attempts and exponential backoff.
import { retry } from '@andranik-arakelyan/js-utilities';
// Basic usage with default options (3 attempts)
const data = await retry(() => fetchData());
// With custom retry configuration
const result = await retry(
() => riskyOperation(),
{
attempts: 5, // Maximum attempts including initial attempt
delay: 1000, // Initial delay in milliseconds
backoffFactor: 2, // Multiply delay by this factor after each attempt
retryIf: (err) => err instanceof NetworkError, // Only retry specific errors
onRetry: (err, attempt) => console.log(`Retry attempt ${attempt}`) // Track retries
}
);
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.