xfunc
v0.7.0
Published
A lightweight JavaScript utility library with common functions
Maintainers
Readme
xfunc
██╗ ██╗███╗ ██╗███████╗██╗ ██╗███╗ ██╗████████╗
██║ ██║████╗ ██║██╔════╝██║ ██║████╗ ██║╚══██╔══╝
██║ ██║██╔██╗ ██║█████╗ ██║ ██║██╔██╗ ██║ ██║
██║ ██║██║╚██╗██║██╔══╝ ██║ ██║██║╚██╗██║ ██║
╚██████╔╝██║ ╚████║██║ ╚██████╔╝██║ ╚████║ ██║
╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═╝A modern collection of utility functions for TypeScript/JavaScript projects.
Features
- 🚀 TypeScript First - Built with TypeScript, providing excellent type safety
- 📦 Tree Shaking - Only import what you need
- 🔧 Zero Dependencies - Lightweight and self-contained
- ⚡ Well Tested - Comprehensive test coverage with Vitest
- 📚 Full Documentation - Complete API reference with examples
- 🎯 Modern Tooling - ESM, CJS, and UMD builds
Installation
# npm
npm install xfunc
# yarn
yarn add xfunc
# pnpm
pnpm add xfuncUsage
// Import specific functions (recommended for tree shaking)
import { debounce, isArray } from 'xfunc'
// Or import everything
import * as xfunc from 'xfunc'
// Usage examples
const debouncedHandler = debounce(() => {
console.log('Called after 300ms delay')
}, 300)
if (isArray(data)) {
console.log('data is an array')
}API Categories
Array Methods
toArray- Convert value to arrayunionBy- Merge arrays and remove duplicates by iterateerange- Generate numeric range arrayssortBy- Sort array by iteratee functionsorderBy- Sort array by multiple criteria with orderuniq- Create deduplicated arrayuniqWith- Create deduplicated array with custom comparatorcastArray- Convert value to array (wrap if needed)remove- Remove first occurrence of item from array
⚡ Function Methods
debounce- Create debounced functionthrottle- Create throttled functionmemoize- Create memoized function with cache
Number Methods
toNumber- Convert value to numberrandomInt- Generate random integer in range
Object Methods
omit/omitBy- Create object excluding propertiespick/pickBy- Create object with only specified propertiesmapEntries- Transform object key-value pairsforEachEntry- Iterate over object entriesforOwn- Iterate over own object propertieshasOwn- Check for own propertyclone- Create shallow clonecloneDeep- Create deep clone
String Methods
capitalize- Capitalize first character, lowercase restlowerFirst- Convert first character to lowercaseupperFirst- Convert first character to uppercasecamelize- Convert hyphenated string to camelCasehyphenate- Convert camelCase string to hyphenated
Type Check Methods
22 type checking utilities including:
isArray,isString,isNumber,isBooleanisEmpty,isNil,isFunction,isObjectisDate,isRegExp,isPromise,isErrorisMap,isSet,isBigInt,isSymbol- And more...
Structure Methods
QueueMap- Map-based queue with insertion order
Quick Examples
Debounce Function Calls
import { debounce } from 'xfunc'
const searchHandler = debounce((query: string) => {
// API call will only happen 300ms after user stops typing
searchAPI(query)
}, 300)
input.addEventListener('input', (e) => {
searchHandler(e.target.value)
})Type-Safe Object Manipulation
import { pick, omit, isObject } from 'xfunc'
const user = { id: 1, name: 'John', email: '[email protected]', password: 'secret' }
// Pick only safe properties for API response
const safeUser = pick(user, ['id', 'name', 'email'])
// { id: 1, name: 'John', email: '[email protected]' }
// Omit sensitive data
const publicUser = omit(user, ['password'])
// { id: 1, name: 'John', email: '[email protected]' }
// Type-safe checking
if (isObject(data) && !isEmpty(data)) {
// Process the object safely
}Array and Type Utilities
import { toArray, range, uniq, isNumber } from 'xfunc'
// Convert various types to arrays
toArray('hello') // ['h', 'e', 'l', 'l', 'o']
toArray(42) // [42]
toArray([1, 2, 3]) // [1, 2, 3]
toArray(null) // []
// Generate numeric ranges
range(5) // [0, 1, 2, 3, 4]
range(1, 5) // [1, 2, 3, 4]
range(0, 20, 5) // [0, 5, 10, 15]
// Remove duplicates
uniq([1, 2, 2, 3, 3, 3]) // [1, 2, 3]
// Safe type checking
isNumber('42') // false
isNumber(42) // trueDocumentation
Visit our documentation site for:
- 📖 Complete API reference
- 💡 Usage examples
- 🎯 Best practices
- 📝 Type definitions
Development
# Install dependencies
pnpm install
# Run tests in watch mode
pnpm test
# Run tests once
pnpm test:run
# Build the library
pnpm build
# Lint and fix code
pnpm lint
# Start documentation site
pnpm docs:dev
# Build documentation
pnpm docs:buildLicense
MIT
Contributing
Contributions are welcome! Please read our contributing guidelines and submit a pull request.
xfunc - Utility functions that just work ⚡
