@eugeou/string-utils
v1.0.0
Published
A collection of useful string utility functions and data type conversion utilities
Downloads
6
Maintainers
Readme
String Utils
A collection of useful string utility functions and data type conversion utilities for TypeScript/JavaScript projects.
Installation
npm install @eugeou/string-utilsUsage
ES6 Modules
import { capitalize, toCamelCase, isValidEmail, numToArray, stringToNum } from '@eugeou/string-utils';
// Capitalize first letter
const result = capitalize('hello world'); // 'Hello world'
// Convert to camelCase
const camelCase = toCamelCase('hello-world'); // 'helloWorld'
// Validate email
const isValid = isValidEmail('[email protected]'); // true
// Data type conversions
const digits = numToArray(123); // [1, 2, 3]
const number = stringToNum('456'); // 456CommonJS
const { capitalize, toCamelCase, isValidEmail } = require('@eugeou/string-utils');Default Import
import stringUtils from '@eugeou/string-utils';
const result = stringUtils.capitalize('hello world');API Reference
capitalize(str: string): string
Capitalizes the first letter of a string.
capitalize('hello world') // 'Hello world'
capitalize('') // ''toCamelCase(str: string): string
Converts a string to camelCase format.
toCamelCase('hello world') // 'helloWorld'
toCamelCase('hello-world') // 'helloWorld'
toCamelCase('hello_world') // 'helloWorld'toKebabCase(str: string): string
Converts a string to kebab-case format.
toKebabCase('helloWorld') // 'hello-world'
toKebabCase('Hello World') // 'hello-world'
toKebabCase('hello_world') // 'hello-world'truncate(str: string, length: number, suffix?: string): string
Truncates a string to a specified length and adds a suffix.
truncate('Hello world', 5) // 'Hello...'
truncate('Hello world', 5, '---') // 'Hello---'removeWhitespace(str: string): string
Removes all whitespace from a string.
removeWhitespace('hello world') // 'helloworld'
removeWhitespace(' hello world ') // 'helloworld'isValidEmail(email: string): boolean
Validates if a string is a valid email address.
isValidEmail('[email protected]') // true
isValidEmail('invalid-email') // falsegenerateRandomString(length: number, charset?: string): string
Generates a random string of specified length.
generateRandomString(8) // 'aB3dE9fG'
generateRandomString(5, '0123456789') // '12345'Data Type Conversion Utilities
numToArray(num: number): number[]
Converts a number to an array of its digits.
numToArray(123) // [1, 2, 3]
numToArray(0) // [0]
numToArray(-456) // [-4, 5, 6]arrayToNum(arr: number[]): number
Converts an array of digits to a number.
arrayToNum([1, 2, 3]) // 123
arrayToNum([0]) // 0
arrayToNum([]) // 0numToString(num: number): string
Converts a number to a string.
numToString(123) // '123'
numToString(0) // '0'
numToString(-456) // '-456'stringToNum(str: string): number
Converts a string to a number.
stringToNum('123') // 123
stringToNum('0') // 0
stringToNum('abc') // NaNstringToArray(str: string): string[]
Converts a string to an array of characters.
stringToArray('hello') // ['h', 'e', 'l', 'l', 'o']
stringToArray('') // []arrayToString(arr: string[]): string
Converts an array of characters to a string.
arrayToString(['h', 'e', 'l', 'l', 'o']) // 'hello'
arrayToString([]) // ''numToBool(num: number): boolean
Converts a number to a boolean (0 = false, non-zero = true).
numToBool(1) // true
numToBool(0) // false
numToBool(-5) // trueboolToNum(bool: boolean): number
Converts a boolean to a number (true = 1, false = 0).
boolToNum(true) // 1
boolToNum(false) // 0stringToBool(str: string): boolean
Converts a string to a boolean (empty string = false, non-empty = true).
stringToBool('hello') // true
stringToBool('') // false
stringToBool('false') // true (non-empty string)boolToString(bool: boolean): string
Converts a boolean to a string.
boolToString(true) // 'true'
boolToString(false) // 'false'arrayToBool<T>(arr: T[]): boolean
Converts an array to a boolean (empty array = false, non-empty = true).
arrayToBool([1, 2, 3]) // true
arrayToBool([]) // falseboolToArray(bool: boolean): boolean[]
Converts a boolean to an array.
boolToArray(true) // [true]
boolToArray(false) // [false]numToBoolArray(num: number): boolean[]
Converts a number to a boolean array (binary representation).
numToBoolArray(5) // [true, false, true] (binary: 101)
numToBoolArray(0) // [false]boolArrayToNum(arr: boolean[]): number
Converts a boolean array to a number (from binary representation).
boolArrayToNum([true, false, true]) // 5 (binary: 101)
boolArrayToNum([false]) // 0stringToNumArray(str: string): number[]
Converts a string to a number array (ASCII values).
stringToNumArray('hi') // [104, 105]
stringToNumArray('') // []numArrayToString(arr: number[]): string
Converts a number array to a string (from ASCII values).
numArrayToString([104, 105]) // 'hi'
numArrayToString([]) // ''Development
Prerequisites
- Node.js (v16 or higher)
- npm
Setup
# Clone the repository
git clone https://github.com/Eugeou/string-utils.git
cd string-utils
# Install dependencies
npm install
# Build the project
npm run build
# Run tests
npm testScripts
npm run build- Build the TypeScript codenpm test- Run testsnpm run dev- Watch mode for development
Contributing
- Fork the repository
- 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
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
1.1.0
- Added comprehensive data type conversion utilities
- Added 16 new conversion functions covering all major data type combinations
- Enhanced test coverage with 100+ new test cases
- Updated documentation with complete API reference
1.0.0
- Initial release
- Added basic string utility functions
- TypeScript support
- Full test coverage
