Skip to content

Commit

Permalink
Ordinal, nits.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohler committed Sep 8, 2023
1 parent 96a574c commit f422bd7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
8 changes: 4 additions & 4 deletions scripts/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,10 +705,10 @@ function plural(n, what) {

/* exported ordinal */
function ordinal(n) {
if (n >= 1 && n <= 3)
return n + ["st", "nd", "rd"][Math.floor(n - 1)];
else
return n + "th";
let x = Math.abs(Math.round(n));
x > 100 && (x = x % 100);
x > 20 && (x = x % 10);
return n + ["th", "st", "nd", "rd"][x > 3 ? 0 : x];
}

function commajoin(a, joinword) {
Expand Down
12 changes: 4 additions & 8 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,14 +458,10 @@ function plural($n, $singular, $plural = null) {
/** @param int $n
* @return string */
function ordinal($n) {
$x = $n;
if ($x > 100) {
$x = $x % 100;
}
if ($x > 20) {
$x = $x % 10;
}
return $n . ($x < 1 || $x > 3 ? "th" : ($x == 1 ? "st" : ($x == 2 ? "nd" : "rd")));
$x = abs($n);
$x > 100 && ($x = $x % 100);
$x > 20 && ($x = $x % 10);
return $n . (["th", "st", "nd", "rd"])[$x > 3 ? 0 : $x];
}

/** @param int|float $n
Expand Down
22 changes: 11 additions & 11 deletions src/pages/p_bulkassign.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,17 @@ function print() {

echo '<div id="foldoptions" class="mb-5 foldc fold2c fold3c">',
'<label>Default action: ',
Ht::select("default_action", ["guess" => "guess from input",
"primary" => "assign primary reviews",
"secondary" => "assign secondary reviews",
"optionalreview" => "assign optional PC reviews",
"metareview" => "assign metareviews",
"review" => "assign external reviews",
"conflict" => "assign PC conflicts",
"lead" => "assign discussion leads",
"shepherd" => "assign shepherds",
"settag" => "set tags",
"preference" => "set reviewer preferences"],
Ht::select("default_action", ["guess" => "Guess from input",
"primary" => "Assign primary reviews",
"secondary" => "Assign secondary reviews",
"optionalreview" => "Assign optional PC reviews",
"metareview" => "Assign metareviews",
"review" => "Assign external reviews",
"conflict" => "Assign PC conflicts",
"lead" => "Assign discussion leads",
"shepherd" => "Assign shepherds",
"settag" => "Set tags",
"preference" => "Set reviewer preferences"],
$qreq->default_action ?? "guess",
["id" => "k-action", "class" => "ml-1"]),
'</label> <span class="barsep">·</span> ';
Expand Down
15 changes: 15 additions & 0 deletions test/t_unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,21 @@ function test_tagger_checks() {
xassert_eqq($tagger->check("~~hello#0", Tagger::NOCHAIR), false);
}

function test_ordinal() {
xassert_eqq(ordinal(0), "0th");
xassert_eqq(ordinal(1), "1st");
xassert_eqq(ordinal(2), "2nd");
xassert_eqq(ordinal(3), "3rd");
xassert_eqq(ordinal(4), "4th");
xassert_eqq(ordinal(10), "10th");
xassert_eqq(ordinal(11), "11th");
xassert_eqq(ordinal(20), "20th");
xassert_eqq(ordinal(21), "21st");
xassert_eqq(ordinal(100), "100th");
xassert_eqq(ordinal(101), "101st");
xassert_eqq(ordinal(-1), "-1st");
}

function test_review_ordinals() {
foreach ([1 => "A", 26 => "Z", 27 => "AA", 28 => "AB", 51 => "AY", 52 => "AZ",
53 => "BA", 54 => "BB", 702 => "ZZ", 703 => "AAA", 704 => "AAB",
Expand Down

0 comments on commit f422bd7

Please sign in to comment.