CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-utility

A collection of useful TypeScript/JavaScript utilities for crypto, date, string, number, JSON operations and more.

Pending
Overview
Eval results
Files

array.mddocs/

Array Operations

Array manipulation utilities for random slicing and efficient element removal operations, optimized for performance in data processing and collection management.

Capabilities

Random Array Slicing

Extract random elements from an array with specified count, using circular iteration for consistent behavior.

/**
 * Array random slice with items count
 * @param arr - Source array to slice from
 * @param num - Number of items to extract (optional, defaults to entire array)
 * @returns New array with randomly selected items
 */
function randomSlice<T = any>(arr: T[], num?: number): T[];

Usage Examples:

import { randomSlice } from "utility";

// Random subset of array
const colors = ['red', 'blue', 'green', 'yellow', 'purple', 'orange'];
const randomColors = randomSlice(colors, 3);
// Result: 3 random colors, e.g., ['green', 'red', 'purple']

// All elements (copy array)
const allColors = randomSlice(colors);
// Result: copy of entire array

// More items than array length (returns copy)
const moreItems = randomSlice(['a', 'b'], 5);
// Result: ['a', 'b'] (returns copy when num >= length)

// Random sampling from large dataset
const dataset = Array.from({ length: 1000 }, (_, i) => i);
const sample = randomSlice(dataset, 10);
// Result: 10 random numbers from 0-999

// Empty array handling
const empty = randomSlice([], 5);
// Result: [] (empty array)

// Random user selection
const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' },
  { id: 4, name: 'Diana' }
];
const randomUsers = randomSlice(users, 2);
// Result: 2 random user objects

Efficient Element Removal

Remove single element from array by index with optimized performance (faster than splice).

/**
 * Remove one element from an array efficiently
 * @param arr - Input array to modify
 * @param index - Element index to remove (supports negative indices)
 * @returns Modified array (same reference)
 */
function spliceOne<T = any>(arr: T[], index: number): T[];

Usage Examples:

import { spliceOne } from "utility";

// Basic element removal
const numbers = [1, 2, 3, 4, 5];
spliceOne(numbers, 2); // Remove element at index 2
console.log(numbers); // [1, 2, 4, 5]

// Negative index (from end)
const letters = ['a', 'b', 'c', 'd'];
spliceOne(letters, -1); // Remove last element
console.log(letters); // ['a', 'b', 'c']

// Out of bounds index (no change)
const items = ['x', 'y', 'z'];
spliceOne(items, 10); // Index too large
console.log(items); // ['x', 'y', 'z'] (unchanged)

spliceOne(items, -10); // Index too negative
console.log(items); // ['x', 'y', 'z'] (unchanged)

// Performance-critical removal
function removeUser(users: User[], userId: number) {
  const index = users.findIndex(user => user.id === userId);
  if (index !== -1) {
    spliceOne(users, index); // Faster than users.splice(index, 1)
  }
  return users;
}

// Batch removals (remove from end to maintain indices)
function removeBatch(arr: any[], indices: number[]) {
  // Sort indices in descending order to maintain correct positions
  const sortedIndices = indices.sort((a, b) => b - a);
  
  for (const index of sortedIndices) {
    spliceOne(arr, index);
  }
  
  return arr;
}

// Event handler cleanup
const handlers = [handler1, handler2, handler3, handler4];
const indexToRemove = handlers.indexOf(handler2);
if (indexToRemove !== -1) {
  spliceOne(handlers, indexToRemove);
}

Install with Tessl CLI

npx tessl i tessl/npm-utility

docs

array.md

crypto.md

date.md

fs.md

function.md

index.md

json.md

number.md

object.md

optimize.md

string.md

timeout.md

web.md

tile.json