Skip to content

mtinner/utility-functions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@mtinner/utility-functions

A collection of TypeScript utility functions for common operations on arrays, strings, objects, and more.

Installation

npm install @mtinner/utility-functions

Features

Array Utilities

  • equals - Check if two arrays are equal
  • isSuperSet - Check if first array contains all elements of second array
  • intersection - Get common elements between two arrays
  • uniqueById - Remove duplicates based on id property
  • unique - Remove duplicate values
  • moveElement - Move array element up or down
  • mapByKey - Convert array to object mapped by specified key
  • partition - Split array into two based on predicate function

String Utilities

  • slice - Slice string to maximum characters
  • shorten - Truncate string and add ellipsis
  • toSeoString - Convert string to SEO-friendly format

Object Utilities

  • typedKeys - Get object keys with proper typing
  • intersectKeysToObject - Create object with only overlapping keys

Other Utilities

  • times - Execute function multiple times
  • memoize - Cache function results with optional TTL and cache controls
  • TaskScheduler - Run async tasks with a concurrency limit and abort support

Usage Examples

import { unique, intersection } from '@mtinner/utility-functions/array';
import { toSeoString } from '@mtinner/utility-functions/to-seo-string';
import { typedKeys } from '@mtinner/utility-functions/object';

// Remove duplicates
const numbers = unique([1, 2, 2, 3]); // [1, 2, 3]

// Find common elements
const common = intersection([1, 2, 3], [2, 3, 4]); // [2, 3]

// Create SEO string
const seo = toSeoString('Hello World!'); // 'hello-world'

// Get typed object keys
const obj = { name: 'John', age: 30 };
const keys = typedKeys(obj); // ('name' | 'age')[]

// Memoize an expensive function (optional TTL in ms)
const expensiveAdd = (a: number, b: number) => {
    // pretend this is slow
    return a + b;
};

const { memoizedFunction: add, clearCache, size } = memoize(expensiveAdd, { ttl: 5000 });

add(2, 3); // 5 (computed)
add(2, 3); // 5 (cached within TTL)
size();    // 1 (number of non-expired cache entries)
clearCache();
size();    // 0

// Run tasks with limited concurrency and abort support
const scheduler = new TaskScheduler(2); // max 2 concurrent tasks
const controller = new AbortController();

const task = async (signal: AbortSignal) => {
    await new Promise((r) => setTimeout(r, 200));
    if (signal.aborted) throw new Error('aborted');
    return 'done';
};

scheduler.add(task, controller.signal).then(console.log).catch(console.error);
// controller.abort(); // optionally abort

API Reference

Array Functions

import { unique, intersection } from '@mtinner/utility-functions/array';

String Functions

import { slice, shorten } from '@mtinner/utility-functions/string';
import { toSeoString } from '@mtinner/utility-functions/to-seo-string';

Object Functions

import { typedKeys, intersectKeysToObject } from '@mtinner/utility-functions/object';

Utility Functions

import { times } from '@mtinner/utility-functions/times';

Memoize Function

import { memoize } from '@mtinner/utility-functions/memoize';

Task Scheduler

import { TaskScheduler } from '@mtinner/utility-functions/task-scheduler';

TypeScript Support

This library is written in TypeScript and includes full type definitions for better developer experience and type safety.

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published