From dc1db2f5c3bfbb9f845ecb516f265bff2706a54f Mon Sep 17 00:00:00 2001 From: astyrc Date: Fri, 25 Sep 2020 18:12:51 +0200 Subject: [PATCH] Fix ordinal method --- lib/src/humanize.dart | 96 ++++++++++++++++------------------------- test/humanize_test.dart | 14 ++++-- 2 files changed, 47 insertions(+), 63 deletions(-) diff --git a/lib/src/humanize.dart b/lib/src/humanize.dart index 67ddc43..f679c91 100644 --- a/lib/src/humanize.dart +++ b/lib/src/humanize.dart @@ -3,80 +3,58 @@ String ordinal(int value) { assert(value != null, '[value] must not be null'); - int tempValue; - dynamic templates; - String finalValue; - - List valueSpecial = [11, 12, 13]; - - if (valueSpecial.contains(value % 100)) { + var specialValue = [11, 12, 13].contains(value % 100); + if (specialValue) { return "${value}th"; } else if (value.toString().length == 1) { - templates = [ - "0", - "1st", - "2nd", - "3rd", - "4th", - "5th", - "6th", - "7th", - "8th", - "9th", - ]; - finalValue = templates[value]; + switch (value) { + case 0: + return "0"; + case 1: + return "${value}st"; + case 2: + return "${value}nd"; + case 3: + return "${value}rd"; + default: + return "${value}th"; + } } else { - tempValue = value % 10; - templates = { - // Ordinal format when value ends with 0, e.g. 80th - "$tempValue": "${value}th", - // Ordinal format when value ends with 1, e.g. 81st, except 11. - "$tempValue": "${value}st", - // Ordinal format when value ends with 2, e.g. 82nd, except 12. - "$tempValue": "${value}nd", - // Ordinal format when value ends with 3, e.g. 83rd, except 13. - "$tempValue": "${value}rd", - // Ordinal format when value ends with 4, e.g. 84th. - "$tempValue": "${value}th", - // Ordinal format when value ends with 5, e.g. 85th. - "$tempValue": "${value}th", - // Ordinal format when value ends with 6, e.g. 86th. - "$tempValue": "${value}th", - // Ordinal format when value ends with 7, e.g. 87th. - "$tempValue": "${value}th", - // Ordinal format when value ends with 8, e.g. 88th. - "$tempValue": "${value}th", - // Ordinal format when value ends with 9, e.g. 89th. - "$tempValue": "${value}th", - }; - finalValue = templates["$tempValue"]; + switch (value % 10) { + case 1: + return "${value}st"; + case 2: + return "${value}nd"; + case 3: + return "${value}rd"; + default: + return "${value}th"; + } } - return finalValue; } - /// Convert an integer to a string containing commas every three digits. /// For example, 3000 becomes '3,000' and 45000 becomes '45,000'. String intComma(int value) { - // Convert int value to a String one - String valueToString = '$value'; + // Convert int value to a String one + String valueToString = '$value'; - // Use RegExp function witch used to match strings or parts of strings - RegExp reg = new RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'); - Function mathFunc = (Match match) => '${match[1]},'; - - // Store the result in a new String value + // Use RegExp function witch used to match strings or parts of strings + RegExp reg = new RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'); + Function mathFunc = (Match match) => '${match[1]},'; - String newValue = valueToString.replaceAllMapped(reg, mathFunc); + // Store the result in a new String value - return '$newValue'; + String newValue = valueToString.replaceAllMapped(reg, mathFunc); + + return '$newValue'; } /// Convert a large integer to a friendly text representation. Works best /// for numbers over 1 million. For example, 1000000 becomes '1.0 million', /// 1200000 becomes '1.2 million' and '1200000000' becomes '1.2 billion'. String intWord(int value) { - if (value < 1000000){ + if (value < 1000000) { return "$value"; } // TODO : Implement numbers Hight to 1M @@ -85,7 +63,6 @@ String intWord(int value) { /// For numbers 1-9, return the number spelled out. Otherwise, return the /// number. This follows Associated Press style. String appNumber(int value) { - List values = [ "one", "two", @@ -100,8 +77,8 @@ String appNumber(int value) { if (value < 0 || value > 10) { return "$value"; - }else{ - return values[value-1]; + } else { + return values[value - 1]; } } @@ -117,4 +94,3 @@ String naturalDay(DateTime value) { String naturalTime(DateTime value) { return ""; } - \ No newline at end of file diff --git a/test/humanize_test.dart b/test/humanize_test.dart index 46c4244..3e1efda 100644 --- a/test/humanize_test.dart +++ b/test/humanize_test.dart @@ -3,9 +3,17 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:humanize/humanize.dart' as humanize; void main() { - test('Humanize', () { - expect(humanize.ordinal(2), "two"); - humanize.ordinal(1); + test('Humanize ordinal', () { + expect(humanize.ordinal(1), "1st"); + expect(humanize.ordinal(2), "2nd"); + expect(humanize.ordinal(3), "3rd"); + + expect(humanize.ordinal(10), "10th"); + expect(humanize.ordinal(11), "11th"); + expect(humanize.ordinal(13), "13th"); + + expect(humanize.ordinal(22), "22nd"); + expect(humanize.ordinal(33), "33rd"); // final calculator = Calculator(); // expect(calculator.addOne(2), 3);