Pluralizing Strings in JavaScript

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will be exploring the concept of pluralizing strings in JavaScript. You will learn how to use a closure to define a function that can return the singular or plural form of a word based on an input number. Additionally, you will have the opportunity to practice using optional arguments and dictionaries to customize the behavior of the function. By the end of this lab, you will have a solid understanding of how to handle pluralization in your JavaScript projects.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/cond_stmts("`Conditional Statements`") javascript/BasicConceptsGroup -.-> javascript/str_manip("`String Manipulation`") javascript/BasicConceptsGroup -.-> javascript/array_methods("`Array Methods`") subgraph Lab Skills javascript/variables -.-> lab-28548{{"`Pluralizing Strings in JavaScript`"}} javascript/data_types -.-> lab-28548{{"`Pluralizing Strings in JavaScript`"}} javascript/arith_ops -.-> lab-28548{{"`Pluralizing Strings in JavaScript`"}} javascript/comp_ops -.-> lab-28548{{"`Pluralizing Strings in JavaScript`"}} javascript/cond_stmts -.-> lab-28548{{"`Pluralizing Strings in JavaScript`"}} javascript/str_manip -.-> lab-28548{{"`Pluralizing Strings in JavaScript`"}} javascript/array_methods -.-> lab-28548{{"`Pluralizing Strings in JavaScript`"}} end

Pluralize String

To pluralize a word based on a given number, use the pluralize function. Start by opening the Terminal/SSH and typing node. This function can return either the singular or plural form of the word, depending on the input number. You can also supply an optional dictionary to use custom plural forms.

To define the pluralize function, use a closure that takes in the word and an optional plural form. If the input num is either -1 or 1, return the singular form of the word. Otherwise, return the plural form. If no custom plural form is supplied, the function will use the default of the singular word + s.

If the first argument is an object, the pluralize function returns a new function that can use the supplied dictionary to resolve the correct plural form of the word.

Here is the pluralize function in action:

const pluralize = (val, word, plural = word + "s") => {
  const _pluralize = (num, word, plural = word + "s") =>
    [1, -1].includes(Number(num)) ? word : plural;
  if (typeof val === "object")
    return (num, word) => _pluralize(num, word, val[word]);
  return _pluralize(val, word, plural);
};

You can use the pluralize function like this:

pluralize(0, "apple"); // 'apples'
pluralize(1, "apple"); // 'apple'
pluralize(2, "apple"); // 'apples'
pluralize(2, "person", "people"); // 'people'

If you have a dictionary of custom plural forms, you can create an autoPluralize function that automatically uses the correct plural form for a given word:

const PLURALS = {
  person: "people",
  radius: "radii"
};
const autoPluralize = pluralize(PLURALS);
autoPluralize(2, "person"); // 'people'

Summary

Congratulations! You have completed the Pluralize String lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like