Skip to Content
Menu

singularPlural()

A convenient JavaScript function for returning a singular or plural label string based on the value.

JavaScript February 27, 2023

Usage

JavaScript
nebula.singularPlural(value, singular, plural)

Parameters

value
(Required) (Integer or Float) The value to determine the label for
Default: None

singular
(Required) (String) The singular label
Default: None

plural
(Optional) (String) The plural label
Default: Adds an "s" to the singular label

Parameter Notes

If the plural form of the singular label is the same but with an “s” (like “book” and “books”), you do not need to pass the plural parameter.

Request or provide clarification »

Examples

When the plural simply adds an "s", no need for the third parameter.

JavaScript
nebula.singularPlural(number, 'hour');

When the plural is spelled differently, the third parameter is needed.

JavaScript
nebula.singularPlural(number, 'story', 'stories');
Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /assets/js/modules/utilities.js on line 870.

    No Hooks

    This function does not have any filters or actions available. Request one?
    JavaScript
    nebula.singularPlural = function(value, singular, plural=''){
        if ( value == 1 ){
            return singular;
        }
    
        if ( !plural ){
            plural = singular + 's'; //Append an "s" to the singular label to simplify calling the function most of the time
        }
    
        return plural;
    };
    

    Override

    To override or disable this JavaScript function, simply redeclare it with the exact same function name. Remember: Some functionality is conditionally loaded via dynamic imports, so if your function is not overriding properly, try listening for a DOM event (described below).

    JavaScript

    For non-module import functions:

    nebula.singularPlural = function(value, singular, plural){
        //Write your own code here, leave it blank, or return false.
    }


    For dynamically imported module function overrides:

    jQuery(window).on('load', function(){
        nebula.singularPlural = function(value, singular, plural){
            //Write your own code here, leave it blank, or return false.
        }
    });


    Custom Nebula DOM events do also exist, so you could also try the following if the Window Load listener does not work:

    jQuery(document).on('nebula_module_loaded', function(module){
        //Note that the module variable is also available to know which module specifically was imported
        if ( typeof nebula.singularPlural === 'function' ){
            nebula.singularPlural = function(value, singular, plural){
                //Write your own code here, leave it blank, or return false.
            }
    	}
    });