ez-string-toolkit
Version:
A simple and easy-to-use string utility library with essential string manipulation functions
136 lines (97 loc) • 3.63 kB
Markdown
A simple and easy-to-use JavaScript string utility library with essential string manipulation functions.
```bash
npm install ez-string-toolkit
```
```javascript
const stringUtils = require('ez-string-toolkit');
// Capitalize first letter
console.log(stringUtils.capitalize('hello')); // Output: "Hello"
// Convert to camelCase
console.log(stringUtils.toCamelCase('hello world')); // Output: "helloWorld"
// Reverse string
console.log(stringUtils.reverse('hello')); // Output: "olleh"
// Count words
console.log(stringUtils.wordCount('hello world')); // Output: 2
// Truncate string
console.log(stringUtils.truncate('hello world', 5)); // Output: "he..."
```
Capitalizes the first letter of a string.
- **Parameter**: `str` (string) - The input string
- **Returns**: (string) - String with first letter capitalized
**Example:**
```javascript
capitalize('hello world'); // "Hello world"
capitalize('JAVASCRIPT'); // "Javascript"
```
Converts a string to camelCase format.
- **Parameter**: `str` (string) - The input string
- **Returns**: (string) - The camelCase formatted string
**Example:**
```javascript
toCamelCase('hello world'); // "helloWorld"
toCamelCase('user-name-field'); // "userNameField"
toCamelCase('my_variable_name'); // "myVariableName"
```
Reverses a string.
- **Parameter**: `str` (string) - The input string
- **Returns**: (string) - The reversed string
**Example:**
```javascript
reverse('hello'); // "olleh"
reverse('JavaScript'); // "tpircSavaJ"
```
Counts the number of words in a string.
- **Parameter**: `str` (string) - The input string
- **Returns**: (number) - The number of words
**Example:**
```javascript
wordCount('hello world'); // 2
wordCount('JavaScript is awesome!'); // 3
wordCount(' spaced out words '); // 3
```
Truncates a string to a specified length.
- **Parameters**:
- `str` (string) - The input string
- `maxLength` (number) - The maximum length
- `suffix` (string, optional) - The suffix to add (default: '...')
- **Returns**: (string) - The truncated string
**Example:**
```javascript
truncate('This is a long sentence', 10); // "This is..."
truncate('Short text', 20); // "Short text"
truncate('Custom suffix', 10, ' →'); // "Custom →"
```
- ✅ **Lightweight**: Only 2.7 kB gzipped
- ✅ **Zero dependencies**: No external dependencies
- ✅ **Well tested**: Comprehensive test coverage
- ✅ **TypeScript friendly**: Works great with TypeScript projects
- ✅ **Browser compatible**: Works in both Node.js and browsers
Run the test suite:
```bash
npm test
```
You can also use this library in the browser via CDN:
```html
<script src="https://unpkg.com/ez-string-toolkit@latest/index.js"></script>
<script>
// Library is available as global variable
console.log(capitalize('hello world')); // "Hello world"
</script>
```
MIT
Contributions are welcome! Please feel free to submit a Pull Request or open an Issue.