Skip to content

Commit

Permalink
Arabic improvements
Browse files Browse the repository at this point in the history
Co-authored-by: Evan Hahn <[email protected]>

See [#198][0] and [#199][1].

[0]: #198
[1]: #199
  • Loading branch information
Misioka authored and EvanHahn committed Jun 6, 2021
1 parent c68f86c commit 77daebb
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 34 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased

- change: use Arabic numerals, delimiters, and updated words (see [#198](https://github.com/EvanHahn/HumanizeDuration.js/pull/198) and [#199](https://github.com/EvanHahn/HumanizeDuration.js/pull/199))

# 3.26.0 / 2021-05-03

- new: Afrikaans support (`af`)
Expand Down
68 changes: 55 additions & 13 deletions humanize-duration.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
decimal: ","
};

var ARABIC_DIGITS = ["۰", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"];

var LANGUAGES = {
af: {
y: "jaar",
Expand Down Expand Up @@ -65,30 +67,44 @@
},
ar: {
y: function (c) {
return c === 1 ? "سنة" : "سنوات";
return ["سنة", "سنتان", "سنوات"][getArabicForm(c)];
},
mo: function (c) {
return c === 1 ? "شهر" : "أشهر";
return ["شهر", "شهران", "أشهر"][getArabicForm(c)];
},
w: function (c) {
return c === 1 ? "أسبوع" : "أسابيع";
return ["أسبوع", "أسبوعين", "أسابيع"][getArabicForm(c)];
},
d: function (c) {
return c === 1 ? "يوم" : "أيام";
return ["يوم", "يومين", "أيام"][getArabicForm(c)];
},
h: function (c) {
return c === 1 ? "ساعة" : "ساعات";
return ["ساعة", "ساعتين", "ساعات"][getArabicForm(c)];
},
m: function (c) {
return c > 2 && c < 11 ? "دقائق" : "دقيقة";
return ["دقيقة", "دقيقتان", "دقائق"][getArabicForm(c)];
},
s: function (c) {
return c === 1 ? "ثانية" : "ثواني";
return ["ثانية", "ثانيتان", "ثواني"][getArabicForm(c)];
},
ms: function (c) {
return c === 1 ? "جزء من الثانية" : "أجزاء من الثانية";
return ["جزء من الثانية", "جزآن من الثانية", "أجزاء من الثانية"][
getArabicForm(c)
];
},
decimal: ","
decimal: ",",
delimiter: " و ",
_formatCount: function (count, decimal) {
var replacements = assign(ARABIC_DIGITS, { ".": decimal });
var characters = count.toString().split("");
for (var i = 0; i < characters.length; i++) {
var character = characters[i];
if (has(replacements, character)) {
characters[i] = replacements[character];
}
}
return characters.join("");
}
},
bg: {
y: function (c) {
Expand Down Expand Up @@ -1367,7 +1383,6 @@
result,
{
language: "en",
delimiter: ", ",
spacer: " ",
conjunction: "",
serialComma: true,
Expand Down Expand Up @@ -1509,13 +1524,22 @@
}

if (result.length) {
var delimiter;
if (has(options, "delimiter")) {
delimiter = options.delimiter;
} else if (has(dictionary, "delimiter")) {
delimiter = dictionary.delimiter;
} else {
delimiter = ", ";
}

if (!options.conjunction || result.length === 1) {
return result.join(options.delimiter);
return result.join(delimiter);
} else if (result.length === 2) {
return result.join(options.conjunction);
} else if (result.length > 2) {
return (
result.slice(0, -1).join(options.delimiter) +
result.slice(0, -1).join(delimiter) +
(options.serialComma ? "," : "") +
options.conjunction +
result.slice(-1)
Expand All @@ -1541,7 +1565,12 @@
decimal = ".";
}

var countStr = count.toString().replace(".", decimal);
var countStr;
if (typeof dictionary._formatCount === "function") {
countStr = dictionary._formatCount(count, decimal);
} else {
countStr = count.toString().replace(".", decimal);
}

var dictionaryValue = dictionary[type];
var word;
Expand All @@ -1567,6 +1596,19 @@
return destination;
}

function getArabicForm(c) {
if (c === 1) {
return 0;
}
if (c === 2) {
return 1;
}
if (c > 2 && c < 11) {
return 2;
}
return 0;
}

function getPolishForm(c) {
if (c === 1) {
return 0;
Expand Down
40 changes: 20 additions & 20 deletions test/definitions/ar.csv
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
0$0 أجزاء من الثانية
1$1 جزء من الثانية
1.2$1,2 أجزاء من الثانية
2$2 أجزاء من الثانية
1000$1 ثانية
2000$2 ثواني
60000$1 دقيقة
120000$2 دقيقة
1200000$20 دقيقة
300000$5 دقائق
3600000$1 ساعة
7200000$2 ساعات
86400000$1 يوم
172800000$2 أيام
604800000$1 أسبوع
1209600000$2 أسابيع
2629800000$1 شهر
5259600000$2 أشهر
31557600000$1 سنة
63115200000$2 سنوات
0$۰ جزء من الثانية
1$١ جزء من الثانية
1.2$١,٢ جزء من الثانية
2$٢ جزآن من الثانية
1000$١ ثانية
2000$٢ ثانيتان
60000$١ دقيقة
120000$٢ دقيقتان
1200000$٢۰ دقيقة
300000$٥ دقائق
3600000$١ ساعة
7200000$٢ ساعتين
86400000$١ يوم
172800000$٢ يومين
604800000$١ أسبوع
1209600000$٢ أسبوعين
2629800000$١ شهر
5259600000$٢ شهران
31557600000$١ سنة
63115200000$٢ سنتان
7 changes: 6 additions & 1 deletion test/humanizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,17 @@ describe("humanizer", function () {
w: () => "w",
d: () => "d",
h: () => "h",
m: () => "mi",
m: () => "m",
s: () => "s",
ms: () => "ms",
delimiter: "--",
};

assert.strictEqual(h(1000), "1 s");
assert.strictEqual(h(61000), "1 m--1 s");

assert.strictEqual(h(61000, { delimiter: "&&" }), "1 m&&1 s");

assert.strictEqual(
h(1000, {
language: "es",
Expand Down

0 comments on commit 77daebb

Please sign in to comment.