Skip to content

Commit

Permalink
add private language option: _hideCountIf2
Browse files Browse the repository at this point in the history
  • Loading branch information
6km authored and EvanHahn committed May 25, 2024
1 parent 415ff71 commit 42cf7ba
Showing 1 changed file with 24 additions and 33 deletions.
57 changes: 24 additions & 33 deletions humanize-duration.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* @prop {string} [delimiter]
* @prop {DigitReplacements} [_digitReplacements]
* @prop {boolean} [_numberFirst]
* @prop {boolean} [_hideCountIf2]
*/

/**
Expand Down Expand Up @@ -186,8 +187,8 @@
),
{
delimiter: " ﻭ ",
hideUnitInDualForm: true,
_digitReplacements: ["۰", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"],
_hideCountIf2: true,
_digitReplacements: ["۰", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"]
}
),
bg: language(
Expand Down Expand Up @@ -1656,21 +1657,10 @@
/** @type {undefined | DigitReplacements} */
var digitReplacements;
if ("digitReplacements" in options) {
digitReplacements = options.digitReplacements;
digitReplacements = options.digitReplacements;
} else if ("_digitReplacements" in language) {
digitReplacements = language._digitReplacements;
digitReplacements = language._digitReplacements;
}

/** @type {boolean} */
var hideUnitInDualForm = false;
if (options.language === "ar") {
if ("arabic_hideUnitInDualForm" in options) {
hideUnitInDualForm = options.arabic_hideUnitInDualForm;
} else if ("hideUnitInDualForm" in language) {
hideUnitInDualForm = language.hideUnitInDualForm;
}
}


/** @type {string} */
var formattedCount;
Expand All @@ -1680,24 +1670,25 @@
: Math.floor(unitCount * Math.pow(10, maxDecimalPoints)) /
Math.pow(10, maxDecimalPoints);
var countStr = normalizedUnitCount.toString();
if (!hideUnitInDualForm || unitCount !== 2) {
if (digitReplacements) {
formattedCount = "";
for (var i = 0; i < countStr.length; i++) {
var char = countStr[i];
if (char === ".") {
formattedCount += decimal;
} else {
// @ts-ignore because `char` should always be 0-9 at this point.
formattedCount += digitReplacements[char];
}
}
} else {
formattedCount = countStr.replace(".", decimal);
}
} else {
formattedCount = "";
}

if (!language._hideCountIf2 || unitCount !== 2) {
if (digitReplacements) {
formattedCount = "";
for (var i = 0; i < countStr.length; i++) {
var char = countStr[i];
if (char === ".") {
formattedCount += decimal;
} else {
// @ts-ignore because `char` should always be 0-9 at this point.
formattedCount += digitReplacements[char];
}
}
} else {
formattedCount = countStr.replace(".", decimal);
}
} else {
formattedCount = "";
}

var languageWord = language[unitName];
var word;
Expand Down

0 comments on commit 42cf7ba

Please sign in to comment.