string-utils-lib
v1.1.0
Published
A comprehensive string manipulation utility library
Maintainers
Readme
String Utils Library
A comprehensive JavaScript utility library for string manipulation, providing a wide range of functions for text transformation, analysis, and generation.
Installation
npm i string-utils-libYou can also find the package on npm: string-utils-lib
GitHub Repository: js-string-lib
Quick Start
Basic Usage
// Import the functions you need
import { capitalize, camelCase, snakeCase } from 'string-utils-lib';
// Use the functions
const text = 'hello world';
console.log(capitalize(text)); // Output: 'Hello world'
console.log(camelCase(text)); // Output: 'helloWorld'
console.log(snakeCase(text)); // Output: 'hello_world'Running in Node.js
- Create a new file (e.g.,
example.js) - Add your code:
const { capitalize, camelCase } = require('string-utils-lib');
const result = capitalize('hello world');
console.log(result); // Output: 'Hello world'- Run the file:
node example.jsRunning in Browser
- Include the library in your HTML:
<script src="node_modules/string-utils-lib/dist/index.js"></script>- Use the functions:
const result = stringUtils.capitalize('hello world');
console.log(result); // Output: 'Hello world'Running in TypeScript
import { capitalize, camelCase } from 'string-utils-lib';
const text: string = 'hello world';
const result: string = capitalize(text);
console.log(result); // Output: 'Hello world'Development
Prerequisites
- Node.js (v14 or higher)
- npm (v6 or higher)
Setup
- Clone the repository:
git clone https://github.com/Joshkaki00/js-string-lib.git
cd js-string-lib- Install dependencies:
npm installRunning Tests
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run test:coverageBuilding
# Build the library
npm run build
# Build in watch mode
npm run build:watchLinting
# Run linter
npm run lint
# Fix linting issues
npm run lint:fixFeatures
- TypeScript Support: Full TypeScript definitions for better developer experience
- Case Manipulation: Convert between different text cases (camelCase, snake_case, etc.)
- Text Transformation: Truncate, reverse, strip HTML, etc.
- Text Analysis: Count words, check for emails or URLs, etc.
- String Generation: Generate random strings, pluralize text, mask characters
Case Manipulation
capitalize(str): Capitalizes the first letter of a stringcamelCase(str): Converts string to camelCasepascalCase(str): Converts string to PascalCasesnakeCase(str): Converts string to snake_casekebabCase(str): Converts string to kebab-casetitleCase(str): Converts string to Title Case
Text Transformation
truncate(str, length, suffix): Truncates text to specified length with optional suffixellipsis(str, length): Truncates text with ellipsis if it exceeds lengthreverse(str): Reverses a stringslugify(str): Converts string to URL-friendly slugstripHTML(str): Removes HTML tags from stringstripPunctuation(str): Removes all punctuation from string
Text Analysis
countWords(str): Counts words in a stringcountOccurrences(str, substr): Counts occurrences of a substringisEmail(str): Validates email addressesisURL(str): Validates URLscontainsSubstring(str, substr, caseSensitive): Checks for substring presencehasEmoji(str): Detects emoji presence in string
String Generation
randomString(length, options): Generates random strings with customizable optionspluralize(singular, plural, count): Returns singular or plural form based on countmaskString(str, visibleChars, options): Masks string with customizable options
Usage
// ES Module import
import { camelCase, truncate } from 'string-utils-lib';
// CommonJS require
const { camelCase, truncate } = require('string-utils-lib');
// TypeScript
import { camelCase, truncate, RandomStringOptions } from 'string-utils-lib';
camelCase('hello world'); // 'helloWorld'
truncate('hello world', 5); // 'hello'Examples
Case Manipulation
import { pascalCase, snakeCase, titleCase } from 'string-utils-lib';
pascalCase('hello world'); // 'HelloWorld'
snakeCase('helloWorld'); // 'hello_world'
titleCase('hello world'); // 'Hello World'Text Transformation
import { truncate, slugify, stripHTML } from 'string-utils-lib';
truncate('Hello world', 5, '...'); // 'Hello...'
slugify('Hello World!'); // 'hello-world'
stripHTML('<p>Hello <b>world</b></p>'); // 'Hello world'String Generation
import { randomString, maskString } from 'string-utils-lib';
// Generate random string
randomString(8, { uppercase: true, numbers: true }); // 'aB3cD4eF'
// Mask string
maskString('1234567890', 4); // '1234******'
maskString('1234567890', 4, { showFirst: false }); // '******7890'API Documentation
Each function is thoroughly documented with JSDoc comments, including:
- Parameter descriptions
- Return value descriptions
- Usage examples
- Edge case handling
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License - feel free to use this library in your projects.
