Open In App

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

Last Updated : 10 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • Open the terminal, and navigate to the project directory.
  • Use the following command to install this module.
  • When the module is installed, a JSON file will get added to your project directory which contains all the details of the installed modules.
$ 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.

  • There are two main functions in this module: pluralize() and singular().
  • pluralize() function takes the singular word as an argument and returns the plural form of it.
  • singular() function takes the plural word as an argument and returns the singular form of it.
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.

Javascript




// 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.

Javascript




// 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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads