Voca JavaScript String Library
Voca is a comprehensive JavaScript library designed for efficient string manipulation, offering a wide array of functions for tasks such as changing case, trimming, padding, slugifying, latinising, formatting (like `sprintf`), truncating, and escaping strings. Currently stable at version 1.4.1, the library maintains a steady release cadence with minor versions introducing new utility functions and improvements. A key differentiator is its modular design, which allows developers to either load the entire library or import individual functions, optimizing application bundle sizes through tree-shaking. It boasts 100% test coverage, comprehensive documentation, and no external dependencies, making it a robust and lightweight solution for string operations across various environments, including Node.js (0.10+), modern browsers (Chrome, Firefox, Safari 7+, Edge 13+, IE 9+), and bundlers like Webpack and Rollup.
Common errors
-
TypeError: voca.slugify is not a function
cause Attempting to import `slugify` as a named export from the main `voca` package (`import { slugify } from 'voca';`) when it's a default export from a subpath.fixCorrect the import statement to `import slugify from 'voca/slugify';` -
ReferenceError: v is not defined
cause Trying to use `v.someFunction()` in a Node.js or browser environment without properly importing or loading the Voca library first.fixIn Node.js, add `const v = require('voca');`. In a browser, ensure `<script src="voca.js"></script>` is present before your script, or `import voca from 'voca'` if using a module bundler. -
ReferenceError: require is not defined
cause Using `require('voca')` or `require('voca/someFunction')` directly in a browser without a CommonJS compatible bundler (like Webpack or Browserify).fixFor browser-only usage, load `dist/voca.min.js` via a script tag which exposes a global `v` object. If using a bundler, ensure it's configured to handle CommonJS modules.
Warnings
- gotcha Voca functions are imported individually from subpaths (e.g., `import slugify from 'voca/slugify'`) rather than as named exports from the main package (`import { slugify } from 'voca'`). Attempting the latter will result in a runtime error.
- gotcha The global variable for Voca in browser environments is `v`, while the CommonJS require often uses `v`, but the ES2015 default import is typically aliased as `voca`. This inconsistency can lead to confusion if not mindful of the environment and import style.
Install
-
npm install voca -
yarn add voca -
pnpm add voca
Imports
- voca
import { voca } from 'voca';import voca from 'voca';
- v
const voca = require('voca');const v = require('voca'); - slugify
import { slugify } from 'voca';import slugify from 'voca/slugify';
- words
const { words } = require('voca');const words = require('voca/words');
Quickstart
import voca from 'voca';
import camelCase from 'voca/camelCase';
import sprintf from 'voca/sprintf';
import slugify from 'voca/slugify';
// Using the entire library default export
const sentence = ' Hello wonderful WORLD! ';
const trimmedSentence = voca.trim(sentence);
console.log(`Trimmed: '${trimmedSentence}'`);
// Using individual function imports for tree-shaking
const camelCased = camelCase('bird flight');
console.log(`Camel Case: '${camelCased}'`);
const formattedString = sprintf('%s costs $%.2f', 'Tea', 1.5);
console.log(`Formatted: '${formattedString}'`);
const urlSlug = slugify('What a wonderful world');
console.log(`Slug: '${urlSlug}'`);
// Example of another function from the main export
const capitalized = voca.capitalize('hello voca');
console.log(`Capitalized: '${capitalized}'`);