Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Voca: The Ultimate Javascript library for String Manipulation
Voca is a powerful JavaScript library designed specifically for string manipulation. In this tutorial, we will explore multiple examples demonstrating how to use the different functions available in Voca.
Features of Voca
Before we see all the examples, let's highlight some features that Voca brings to the table ?
It provides a multitude of functions that can be used to manipulate, query, escape, format strings.
It also provides detailed and searchable documentation.
It supports a wide range of environments like Node.js, Safari 7+, Chrome, Firefox etc.
It doesn't require any dependencies
Installation
To install Voca on your local machine, run the following command in the terminal ?
npm install voca
Once you run the above command, a "package.json" file along with a "package-lock.json" and a "node_modules" folder will be created. Now, we are ready to use Voca functions in our code.
String Case Transformation
The first category of examples we will explore involves changing the case of text.
camelCase() Function
The camelCase() function converts text to its camel case representation.
const v = require('voca');
let companyName = 'tutorials point';
console.log("Input Text -", companyName);
console.log('Camel Case -', v.camelCase(companyName));
Input Text - tutorials point Camel Case - tutorialsPoint
capitalize() Function
The capitalize() function capitalizes the first character of a string.
const v = require('voca');
let companyName = 'tutorials point';
console.log('Input Text -', companyName);
console.log('Capitalize -', v.capitalize(companyName));
Input Text - tutorials point Capitalize - Tutorials point
kebabCase() Function
The kebabCase() function converts text to kebab-case format (words separated by hyphens).
const v = require('voca');
let companyName = 'tutorials point';
console.log('Input -', companyName);
console.log('KebabCase -', v.kebabCase(companyName));
Input - tutorials point KebabCase - tutorials-point
snakeCase() Function
The snakeCase() function converts text to snake_case format (words separated by underscores).
const v = require('voca');
let companyName = 'tutorials point';
console.log('Input -', companyName);
console.log('snakeCase -', v.snakeCase(companyName));
Input - tutorials point snakeCase - tutorials_point
String Chaining
Voca supports method chaining, allowing multiple operations on strings in sequence.
const v = require('voca');
let str = 'Tutorials Point is Awesome!';
console.log('Creating a chain object -', v(str).lowerCase().words());
console.log('Chaining and Wrapping -', v.chain(str).lowerCase().words().value());
Creating a chain object - [ 'tutorials', 'point', 'is', 'awesome' ] Chaining and Wrapping - [ 'tutorials', 'point', 'is', 'awesome' ]
String Extraction Methods
charAt() Function
The charAt() function returns the character at a specific index.
const v = require('voca');
let thingsILove = 'Formula1-Football-Leetcode-Sleeping';
console.log('Input String -', thingsILove);
console.log('charAt 10th index -', v.charAt(thingsILove, 10));
console.log('charAt 7th index -', v.charAt(thingsILove, 7));
Input String - Formula1-Football-Leetcode-Sleeping charAt 10th index - o charAt 7th index - 1
first() and last() Functions
The first() function extracts characters from the beginning, while last() extracts from the end.
const v = require('voca');
let thingsILove = 'Formula1-Football-Leetcode-Sleeping';
console.log('Input -', thingsILove);
console.log('first -', v.first(thingsILove));
console.log('first 8 chars -', v.first(thingsILove, 8));
console.log('last -', v.last(thingsILove));
console.log('last 8 chars -', v.last(thingsILove, 8));
Input - Formula1-Football-Leetcode-Sleeping first - F first 8 chars - Formula1 last - g last 8 chars - Sleeping
String Manipulation Functions
Counting Functions
Voca provides several counting methods for analyzing strings.
const v = require('voca');
console.log('Character count:', v.count('Delhi'));
console.log('Substring count:', v.countSubstrings('India is beautiful. India is huge!', 'India'));
Character count: 5 Substring count: 2
String Modification
const v = require('voca');
console.log('Insert:', v.insert('cde', 'o', 1));
console.log('Repeat:', v.repeat('a', 3));
console.log('Reverse:', v.reverse('apple'));
console.log('Trim:', v.trim(' an apple falling too down under '));
Insert: code Repeat: aaa Reverse: elppa Trim: an apple falling too down under
String Validation Functions
Voca includes various functions for validating string properties.
const v = require('voca');
console.log('isEmpty:', v.isEmpty(''));
console.log('isNumeric (text):', v.isNumeric('Hey there'));
console.log('isNumeric (number):', v.isNumeric('123'));
console.log('isString (text):', v.isString('Hey there'));
console.log('isString (number):', v.isString(12345));
isEmpty: true isNumeric (text): false isNumeric (number): true isString (text): true isString (number): false
String Search Functions
These functions help check string patterns and find content within strings.
const v = require('voca');
let text = 'Hey there, join us?';
console.log('indexOf n:', v.indexOf('India', 'n'));
console.log('indexOf p (not found):', v.indexOf('India', 'p'));
console.log('startsWith:', v.startsWith(text, 'Hey'));
console.log('endsWith:', v.endsWith(text, 'us?'));
console.log('includes:', v.includes(text, 'oin'));
indexOf n: 1 indexOf p (not found): -1 startsWith: true endsWith: true includes: true
Comparison of Case Functions
| Function | Input: "hello world" | Output |
|---|---|---|
| camelCase() | "hello world" | "helloWorld" |
| kebabCase() | "hello world" | "hello-world" |
| snakeCase() | "hello world" | "hello_world" |
| titleCase() | "hello world" | "Hello World" |
Conclusion
Voca provides a comprehensive set of string manipulation functions that make working with text in JavaScript much easier. From case transformations to string validation and search operations, Voca offers reliable and well-documented solutions for common string processing tasks.
