@oxog/string
Version:
Comprehensive string manipulation utilities with zero dependencies
304 lines (228 loc) โข 9.85 kB
Markdown
# @oxog/string
**๐ The Ultimate String Manipulation Library**
A comprehensive, high-performance string utility library with zero dependencies. Featuring 80+ functions, three API styles, advanced algorithms, and complete Unicode support.
[](https://badge.fury.io/js/@oxog/string)
[](https://npmjs.org/package/@oxog/string)
[](https://www.typescriptlang.org/)
[](https://github.com/ersinkoc/string)
[](https://github.com/ersinkoc/string)
[](https://opensource.org/licenses/MIT)
[](https://bundlephobia.com/package/@oxog/string)
## โจ Key Features
- ๐ **Zero Dependencies** - Pure JavaScript implementation, no external dependencies
- ๐ฆ **Three API Styles** - Functional, chainable, and static class interfaces
- ๐ง **Plugin Architecture** - Extensible system for custom functionality
- ๐ **Full Unicode Support** - Proper handling of emojis, grapheme clusters, and international text
- ๐ **Complete TypeScript Support** - Full type definitions and IntelliSense
- ๐ **Dual Module Format** - CommonJS and ESM support for all environments
- โ
**100% Test Coverage** - 434 comprehensive tests ensuring reliability
- ๐ฏ **Tree-Shakeable** - Import only what you need, optimized bundle size
- โก **High Performance** - Optimized algorithms including Boyer-Moore string search
- ๐ก๏ธ **Security Features** - Built-in masking, encoding, and sanitization utilities
- ๐ **Internationalization** - Locale-aware functions and accent normalization
- ๐ **Advanced Analytics** - String similarity, pattern detection, and fuzzy matching
## ๐ฆ Installation
```bash
# npm
npm install @oxog/string
# yarn
yarn add @oxog/string
# pnpm
pnpm add @oxog/string
```
## ๐ Quick Start
Choose your preferred API style and start using @oxog/string immediately:
### ๐ Chainable API (Recommended)
Perfect for multiple transformations in sequence:
```typescript
import { chain } from '@oxog/string';
const result = chain(' Hello WORLD ')
.trim()
.toLowerCase()
.toCamelCase()
.value(); // 'helloWorld'
// Complex text processing
const slug = chain('<p>Cafรฉ & Restaurant</p>')
.stripHtml()
.removeAccents()
.slugify({ separator: '_' })
.value(); // 'cafe_restaurant'
```
### โก Functional API
Import individual functions for optimal tree-shaking:
```typescript
import { toCamelCase, isEmail, reverse, similarity } from '@oxog/string';
toCamelCase('hello-world'); // 'helloWorld'
isEmail('test@example.com'); // true
reverse('hello'); // 'olleh'
similarity('hello', 'helo'); // 0.8
```
### ๐๏ธ Static Class API
Use the unified Str class for consistent API access:
```typescript
import { Str } from '@oxog/string';
Str.toCamelCase('hello-world'); // 'helloWorld'
Str.maskEmail('test@example.com'); // 't***@example.com'
Str.random(10); // 'aB3xY9mN2p'
Str.boxify('Hello World!'); // โโโโโโโโโโโโโโโ
// โ Hello World! โ
// โโโโโโโโโโโโโโโ
```
## API Reference
### Case Transformations
```typescript
import { toCamelCase, toPascalCase, toSnakeCase, toKebabCase, toConstantCase, toTitleCase, toSentenceCase } from '@oxog/string';
toCamelCase('hello-world'); // 'helloWorld'
toPascalCase('hello-world'); // 'HelloWorld'
toSnakeCase('helloWorld'); // 'hello_world'
toKebabCase('helloWorld'); // 'hello-world'
toConstantCase('helloWorld'); // 'HELLO_WORLD'
toTitleCase('hello world'); // 'Hello World'
toSentenceCase('HELLO WORLD'); // 'Hello world'
```
### Validation
```typescript
import { isEmail, isUrl, isUuid, isHexColor, isBase64, isJson, isNumeric, isAlpha, isAlphanumeric, isEmpty } from '@oxog/string';
isEmail('test@example.com'); // true
isUrl('https://example.com'); // true
isUuid('550e8400-e29b-41d4-a716-446655440000'); // true
isHexColor('#ff0000'); // true
isBase64('SGVsbG8gV29ybGQ='); // true
isJson('{"key": "value"}'); // true
isNumeric('123.45'); // true
isAlpha('hello'); // true
isAlphanumeric('hello123'); // true
isEmpty(''); // true
```
### String Manipulation
```typescript
import { reverse, shuffle, repeat, truncate, pad, wrap, slugify } from '@oxog/string';
reverse('hello'); // 'olleh'
shuffle('hello'); // 'hlelo' (random)
repeat('a', 3); // 'aaa'
repeat('a', 3, '-'); // 'a-a-a'
truncate('hello world', 8); // 'hello...'
pad('hello', 8); // 'hello '
wrap('hello world test', 10); // 'hello\nworld test'
slugify('Hello World!'); // 'hello-world'
```
### Encoding/Decoding
```typescript
import { encodeBase64, decodeBase64, encodeHex, decodeHex, encodeHtml, decodeHtml, encodeUri, decodeUri } from '@oxog/string';
encodeBase64('hello'); // 'aGVsbG8='
decodeBase64('aGVsbG8='); // 'hello'
encodeHex('hello'); // '68656c6c6f'
decodeHex('68656c6c6f'); // 'hello'
encodeHtml('<div>Hello</div>'); // '<div>Hello</div>'
decodeHtml('<div>Hello</div>'); // '<div>Hello</div>'
```
### Advanced Features
```typescript
import { similarity, fuzzyMatch, soundsLike, findPatterns, extractEmails, random, mask, toTable, boxify, progressBar } from '@oxog/string';
similarity('hello', 'helo'); // 0.8 (similarity score)
fuzzyMatch('hello', 'helo', 0.7); // true
soundsLike('hello', 'helo'); // true (using soundex)
findPatterns('abcabcabc'); // [{ pattern: 'abc', frequency: 3, ... }]
extractEmails('Contact test@example.com'); // ['test@example.com']
random(10); // 'aB3xY9mN2p' (random)
mask('secret', { unmaskedEnd: 2 }); // '****et'
// Visual formatting
toTable([['Name', 'Age'], ['John', '30']]); // ASCII table
boxify('Hello World'); // Boxed text
progressBar(75); // 'โโโโโโโโโโโโโโโโ 75.0%'
```
### Plugin System
```typescript
import { core, createPlugin } from '@oxog/string';
// Create a custom plugin
const myPlugin = createPlugin('my-plugin', '1.0.0', (core) => {
core.extend('customMethod', (str: string) => {
return str.toUpperCase() + '!';
});
});
// Use the plugin
core.use(myPlugin);
// Access extended functionality
const customMethod = core.getExtension('customMethod');
if (customMethod) {
console.log(customMethod('hello')); // 'HELLO!'
}
```
## Options and Configuration
Many functions accept options objects for customization:
```typescript
// URL validation options
isUrl('example.com', {
requireProtocol: false,
allowUnderscore: true
});
// Truncation options
truncate('hello world', 8, {
suffix: 'โฆ',
preserveWords: true
});
// Slugify options
slugify('Hello World!', {
separator: '_',
lowercase: true,
strict: false
});
// Random string options
random(10, {
uppercase: true,
lowercase: true,
numbers: true,
symbols: false,
excludeSimilar: true
});
```
## Unicode Support
All functions properly handle Unicode characters, emojis, and grapheme clusters:
```typescript
reverse('๐๐'); // '๐๐'
chars('๐จโ๐ฉโ๐งโ๐ฆ'); // ['๐จโ๐ฉโ๐งโ๐ฆ'] (single grapheme cluster)
slugify('Cafรฉ niรฑo'); // 'cafe-nino'
```
## TypeScript Support
Full TypeScript definitions are included:
```typescript
import type { TruncateOptions, SlugifyOptions, ChainableString } from '@oxog/string';
const options: TruncateOptions = {
suffix: '...',
preserveWords: true
};
const chainable: ChainableString = chain('hello');
```
## Browser Support
Works in all modern browsers and Node.js environments:
- Node.js 14+
- Chrome 70+
- Firefox 65+
- Safari 12+
- Edge 79+
## Performance
Optimized algorithms and implementations:
- Boyer-Moore string searching
- Efficient Unicode handling
- Memoization for expensive operations
- Tree-shakeable imports
## ๐ Examples & Documentation
Explore comprehensive examples and documentation:
### Example Files
- ๐ [Comprehensive Examples](./examples/comprehensive.js) - All 80+ functions demonstrated
- ๐๏ธ [Basic Usage](./examples/basic-usage.js) - Getting started guide
- ๐ [TypeScript Usage](./examples/typescript-usage.ts) - Type-safe implementations
- ๐ [Plugin Development](./examples/plugin-development.js) - Custom plugin creation
- โก [Performance Benchmarks](./examples/performance.js) - Performance comparisons
- ๐ [Real-World Usage](./examples/real-world-usage.js) - Practical applications
### Additional Documentation
- ๐ [Complete API Reference](./docs/API.md) - Detailed function documentation
- ๐ [Migration Guide](./docs/MIGRATION.md) - Upgrading from other libraries
- ๐ฏ [Best Practices](./docs/BEST_PRACTICES.md) - Recommended usage patterns
- ๐ [Troubleshooting](./docs/TROUBLESHOOTING.md) - Common issues and solutions
## License
MIT ยฉ Ersin KOร
## Contributing
Contributions are welcome! Please read our [Contributing Guide](./CONTRIBUTING.md) for details.
## Changelog
See [CHANGELOG.md](./CHANGELOG.md) for release history.