Bug 974259 - Add getReplace and getReplaceAll helper functions to devtools's PluralForm. r? draft
authorIan Moody <moz-ian@perix.co.uk>
Sun, 08 Oct 2017 23:47:53 +0100
changeset 677031 846b3a02f9c74b9f0d43887db2fb3d8342e3b0c5
parent 677030 4cb03c2e7d4087fcbb4f7a296dbf8762827463f3
child 677032 660a5d9a45b7449a7711cc731f9de212a8e85aca
push id83667
push usermoz-ian@perix.co.uk
push dateMon, 09 Oct 2017 22:03:09 +0000
bugs974259
milestone58.0a1
Bug 974259 - Add getReplace and getReplaceAll helper functions to devtools's PluralForm. r? MozReview-Commit-ID: JPuhZH4Py4P
devtools/shared/plural-form.js
--- a/devtools/shared/plural-form.js
+++ b/devtools/shared/plural-form.js
@@ -147,16 +147,52 @@ const PluralForm = {
         ret = words[0];
       }
 
       return ret;
     }, () => numForms];
   },
 
   /**
+   * Get the correct plural form of a string based on the number, with "#1" in
+   * the string replaced by said number
+   *
+   * @param aNum {int}
+   *        The number to decide which plural form to use
+   * @param aStrings {String}
+   *        A semi-colon (;) separated string of strings to pick the plural form
+   * @returns The appropriate plural form of the string with "#1" replaced with aNum
+   */
+  getReplace(aNum, aStrings)
+  {
+    return PluralForm.get(aNum, aStrings).replace("#1", aNum);
+  },
+
+  /**
+   * Get the correct plural form of a string based on the number, and with
+   * placeholders replaced
+   *
+   * @param aNum {int}
+   *        The number to decide which plural form to use
+   * @param aStrings {String}
+   *        A semi-colon (;) separated string of words to pick the plural form
+   * @param aParams {Array}
+   *        The replacements for the #1 etc. placeholders
+   * @returns The appropriate plural form of the string with substitutions performed
+   */
+  getReplaceAll(aNum, aStrings, aParams)
+  {
+    let string = PluralForm.get(aNum, aStrings);
+    return string.replace(/\#(\d+)/g, function(matchedId, matchedNumber) {
+      let param = aParams[parseInt(matchedNumber, 10) - 1];
+      return param !== undefined ? param : matchedId;
+    });
+  },
+
+  /**
    * Get the number of forms for the current plural rule
    *
    * @return The number of forms
    */
   get numForms()
   {
     // We lazily load numForms, so trigger the init logic with get()
     this.get();