Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ordinal method #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 36 additions & 60 deletions lib/src/humanize.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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];
}
}

Expand All @@ -117,4 +94,3 @@ String naturalDay(DateTime value) {
String naturalTime(DateTime value) {
return "";
}

14 changes: 11 additions & 3 deletions test/humanize_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down