Skip to content

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 duplicatesconstnumbers=unique([1,2,2,3]);// [1, 2, 3]// Find common elementsconstcommon=intersection([1,2,3],[2,3,4]);// [2, 3]// Create SEO stringconstseo=toSeoString('Hello World!');// 'hello-world'// Get typed object keysconstobj={name: 'John',age: 30};constkeys=typedKeys(obj);// ('name' | 'age')[]// Memoize an expensive function (optional TTL in ms)constexpensiveAdd=(a: number,b: number)=>{// pretend this is slowreturna+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 supportconstscheduler=newTaskScheduler(2);// max 2 concurrent tasksconstcontroller=newAbortController();consttask=async(signal: AbortSignal)=>{awaitnewPromise((r)=>setTimeout(r,200));if(signal.aborted)thrownewError('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

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages