A modular collection of useful TypeScript utility functions for common programming tasks. Each utility category is organized in separate files for better maintainability and tree-shaking optimization.
β¨ Modular Design - Each utility class in its own file
π³ Tree-shakable - Import only what you need
π Full TypeScript Support - Complete type definitions included
π§ͺ Well Tested - Comprehensive test coverage
π¦ Multiple Import Patterns - Flexible usage options
npm install typescript-util-npm-module// Import all utilities
import { StringUtils, ArrayUtils, NumberUtils, ObjectUtils } from 'typescript-util-npm-module';
// Import only specific utilities for better tree-shaking
import { StringUtils, ArrayUtils } from 'typescript-util-npm-module';
// Import types
import { ApiResponse, DeepPartial, Callback } from 'typescript-util-npm-module';
// Usage examples
const capitalized = StringUtils.capitalize('hello world'); // "Hello world"
const uniqueArray = ArrayUtils.unique([1, 2, 2, 3, 3, 4]); // [1, 2, 3, 4]// Import all utilities
const { StringUtils, ArrayUtils, NumberUtils, ObjectUtils } = require('typescript-util-npm-module');
// Import specific utilities
const { StringUtils } = require('typescript-util-npm-module');
const camelCase = StringUtils.toCamelCase('hello world'); // "helloWorld"capitalize(str: string): string- Capitalizes the first letter of a stringtoCamelCase(str: string): string- Converts a string to camelCaseisBlank(str: string): boolean- Checks if a string is empty or contains only whitespace
unique<T>(array: T[]): T[]- Removes duplicate values from an arraychunk<T>(array: T[], size: number): T[][]- Chunks an array into smaller arraysflatten<T>(array: T[][]): T[]- Flattens a nested array by one level
randomBetween(min: number, max: number): number- Generates a random number between min and maxroundTo(num: number, decimals: number): number- Rounds a number to specified decimal placesisEven(num: number): boolean- Checks if a number is evenisOdd(num: number): boolean- Checks if a number is odd
deepClone<T>(obj: T): T- Deep clones an objectisEmpty(obj: object): boolean- Checks if an object is emptygetNestedProperty(obj: any, path: string): any- Gets nested property value safely
import { StringUtils, ArrayUtils, NumberUtils, ObjectUtils } from 'typescript-util-npm-module';
// String utilities
console.log(StringUtils.capitalize('hello')); // "Hello"
console.log(StringUtils.toCamelCase('hello world')); // "helloWorld"
console.log(StringUtils.isBlank(' ')); // true
// Array utilities
console.log(ArrayUtils.unique([1, 2, 2, 3])); // [1, 2, 3]
console.log(ArrayUtils.chunk([1, 2, 3, 4, 5], 2)); // [[1, 2], [3, 4], [5]]
console.log(ArrayUtils.flatten([[1, 2], [3, 4]])); // [1, 2, 3, 4]
// Number utilities
console.log(NumberUtils.randomBetween(1, 10)); // Random number between 1 and 10
console.log(NumberUtils.roundTo(3.14159, 2)); // 3.14
console.log(NumberUtils.isEven(4)); // true
// Object utilities
const obj = { a: { b: { c: 'value' } } };
console.log(ObjectUtils.getNestedProperty(obj, 'a.b.c')); // "value"
console.log(ObjectUtils.isEmpty({})); // trueThis library is written in TypeScript with a modular architecture:
src/
βββ StringUtils.ts # String manipulation utilities
βββ ArrayUtils.ts # Array processing utilities
βββ NumberUtils.ts # Number utility functions
βββ ObjectUtils.ts # Object manipulation utilities
βββ types.ts # Shared type definitions
βββ index.ts # Main entry point (re-exports)
Benefits:
- π― Excellent IntelliSense - Full type checking and autocomplete
- π³ Better Tree-shaking - Bundlers can eliminate unused utilities
- π§ Easy Maintenance - Each utility category in separate files
- π Scalable - Easy to add new utilities or extend existing ones
import { ApiResponse, DeepPartial, Callback } from 'typescript-util-npm-module';
// Generic API response interface
const response: ApiResponse<string[]> = {
data: ['item1', 'item2'],
success: true,
timestamp: Date.now()
};
// Deep partial type for configuration objects
const partialConfig: DeepPartial<ComplexConfig> = {
database: {
host: 'localhost' // Other properties are optional
}
};
// Generic callback type
const onComplete: Callback<string> = (result) => console.log(result);# Install dependencies
npm install
# Build the library
npm run build
# Run TypeScript tests (recommended)
npm test
# Run all compatibility tests
npm run test:all
# Run individual format tests
npm run test:js # CommonJS compatibility
npm run test:esm # ES6 module compatibility
# Watch for changes during development
npm run dev
# Clean build directory
npm run cleanWe welcome contributions! The modular structure makes it easy to add new utilities or extend existing ones.
- Fork the repository
- Create a new utility file in
src/(e.g.,src/DateUtils.ts) - Export your utility class following the existing pattern
- Add the export to
src/index.ts - Add tests in
tests/ - Update this README
# Install dependencies
npm install
# Run tests in watch mode during development
npm run dev
# Run all tests
npm run test:all
# Build and verify
npm run buildtypescript_npm_module/
βββ src/
β βββ StringUtils.ts # String utilities
β βββ ArrayUtils.ts # Array utilities
β βββ NumberUtils.ts # Number utilities
β βββ ObjectUtils.ts # Object utilities
β βββ types.ts # Shared types
β βββ index.ts # Main entry point
βββ tests/
β βββ test.ts # TypeScript tests
βββ examples/
β βββ example.js # CommonJS example
β βββ example.mjs # ES6 module example
βββ dist/ # Compiled output
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.