Open In App

Pluralize and Singularize any Word using the ‘pluralize’ NPM Module

The ‘pluralize‘ npm module is a very important and useful NPM module that can convert singular English words to their plural form and vice versa also. This module uses a pre-defined list of rules, applied in order, to singularize or pluralize a given word which can be used in node.js applications or projects.

Using the ‘pluralize’ module in Node.js:



Installation Steps: To use this module, we have to install it first using the following steps:

$ npm install pluralize

Installation

Use the module: To use the module, firstly we have to import the module into our project using the require function passing the name of the module.



const pluralize = require('pluralize');

Run the code: After successfully installing and using the module in our application or project we can run the code using the given command. Open the terminal, navigate to the project directory, and use this command to run the code.

$ node index.js

Where index.js is the javascript code file name.

Example 1: Singular to Plural: In this example, first, we have imported the ‘pluralize’ module. We have used pluralize() function in which words are given as an argument and it returns a plural form of them. And at last, we have displayed the output in the console.




// Import the module
var pluralize = require('pluralize');
  
var apple = pluralize("apple");
var goose = pluralize("goose");
var child = pluralize("child");
var person = pluralize("person");
  
// Outputs
console.log(apple);
console.log(goose);
console.log(child);
console.log(person);

 Output : 

apples
geese
children
people

Example 2: Plural to Singular: In this example, first, we have imported the ‘pluralize’ module. We have used the singular() function in which words are given as an argument and it returns a singular form of them. And at last, we have displayed the output in the console.




// Import the module
var pluralize = require('pluralize');
  
var boxes = pluralize.singular("boxes");
var books = pluralize.singular("books");
var schools = pluralize.singular("schools");
var teachers = pluralize.singular("teachers");
  
// Outputs
console.log(boxes);
console.log(books);
console.log(schools);
console.log(teachers);

Output:

box
book
school
teacher

Article Tags :