[gnome-maps/wip/mlundblad/round-populations] utils: Add function to format population numbers
- From: Marcus Lundblad <mlundblad src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-maps/wip/mlundblad/round-populations] utils: Add function to format population numbers
- Date: Sun, 16 May 2021 20:54:09 +0000 (UTC)
commit a83bdb0fe6f585bc7b56d488d418df701fd90391
Author: Marcus Lundblad <ml update uu se>
Date: Sun May 16 22:52:05 2021 +0200
utils: Add function to format population numbers
Handles rounding even population numbers
(>= 1 M and divisable by 100k) in compact
abbreviated form (leaving out trailing zeros).
src/utils.js | 13 +++++++++++++
tests/utilsTest.js | 9 +++++++++
2 files changed, 22 insertions(+)
---
diff --git a/src/utils.js b/src/utils.js
index 64ff9dae..fe308472 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -396,6 +396,19 @@ function prettyDistance(distance, noRound) {
}
}
+/**
+ * Format a population number so that greater than or equal to a million and
+ * evenly divisiable by 100k are displayed a locale-specific compact form
+ * to handle estimated values without showing lots of zeros.
+ * Other values are formatted in full.
+ */
+function prettyPopulation(population) {
+ let notation = population >= 1000000 && population % 100000 === 0 ?
+ 'compact' : 'standard';
+
+ return population.toLocaleString(undefined, { notation: notation });
+}
+
function uriSchemeSupported(scheme) {
let apps = Gio.AppInfo.get_all();
let prefix = 'x-scheme-handler/';
diff --git a/tests/utilsTest.js b/tests/utilsTest.js
index 33801860..ddd010d3 100644
--- a/tests/utilsTest.js
+++ b/tests/utilsTest.js
@@ -36,6 +36,7 @@ function main() {
getAccuracyDescriptionTest();
prettyTimeTest();
prettyDistanceTest();
+ prettyPopulationTest();
normalizeStringTest();
validWebsiteTest();
validEmailTest();
@@ -98,6 +99,14 @@ function prettyDistanceTest() {
JsUnit.assertEquals('5282 ft', Utils.prettyDistance(1610, true));
}
+function prettyPopulationTest() {
+ JsUnit.assertEquals('123456', Utils.prettyPopulation(123456));
+ JsUnit.assertEquals('1234567', Utils.prettyPopulation(1234567));
+ JsUnit.assertEquals('200000', Utils.prettyPopulation(200000));
+ JsUnit.assertEquals('1M', Utils.prettyPopulation(1000000));
+ JsUnit.assertEquals('2.1M', Utils.prettyPopulation(2100000));
+}
+
function normalizeStringTest() {
JsUnit.assertEquals('foo', Utils.normalizeString('foo'));
JsUnit.assertEquals('fooBar', Utils.normalizeString('fooBar'));
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]