Skip to content

Commit

Permalink
reduce complexity, again
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonpecora committed Jan 17, 2018
1 parent 5dfc60b commit 8735c3b
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 74 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ add commas to numbers.<br /> _note:_ this overrides handlebars-helpers' `addCo
add ordinal after a number<br />e.g. `1``1st` , `2``2nd` , `3``3rd`

#### Params
* `i` _(number|string)_ number to add ordinal after
* `num` _(number|string)_ number to add ordinal after

**Returns** _(string)_

Expand Down
45 changes: 31 additions & 14 deletions helpers/numbers/addOrdinalSuffix.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,43 @@
'use strict';

function mod10(num) {
return num % 10;
}

function mod100(num) {
return num % 100;
}

function isFirst(num) {
return mod10(num) === 1 && mod100(num) !== 11;
}

function isSecond(num) {
return mod10(num) === 2 && mod100(num) !== 12;
}

function isFourth(num) {
return mod10(num) === 3 && mod100(num) !== 13;
}

/**
* add ordinal after a number
* e.g. `1` → `1st`, `2` → `2nd`, `3` → `3rd`
* @param {number|string} i number to add ordinal after
* @param {number|string} num number to add ordinal after
* @return {string}
*/
module.exports = function (i) {
const j = i % 10,
k = i % 100;

module.exports = function (num) {
// if no number is supplied, pass through string
if (i === '' || isNaN(i)) { // will check numbers and (numbers in) strings
return new String(i); // will convert to a string if it's a different type
} else if (j === 1 && k !== 11) {
return i + 'st';
} else if (j === 2 && k !== 12) {
return i + 'nd';
} else if (j === 3 && k !== 13) {
return i + 'rd';
if (num === '' || isNaN(num)) { // will check numbers and (numbers in) strings
return new String(num); // will convert to a string if it's a different type
} else if (isFirst(num)) {
return `${num}st`;
} else if (isSecond(num)) {
return `${num}nd`;
} else if (isFourth(num)) {
return `${num}rd`;
} else {
return i + 'th';
return `${num}th`;
}
};

Expand Down
118 changes: 60 additions & 58 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"coveralls": "^2.11.15",
"date-fns": "^1.28.2",
"documentation": "4.0.0-beta15",
"eslint": "^4.4.1",
"eslint": "^4.15.0",
"handlebars-template-loader": "^0.7.0",
"istanbul": "^0.4.5",
"lodash-webpack-plugin": "^0.11.2",
Expand Down

0 comments on commit 8735c3b

Please sign in to comment.