Skip to content

Commit

Permalink
Minor updates to files to make linter happy.
Browse files Browse the repository at this point in the history
  • Loading branch information
LanceTurri committed Sep 7, 2017
1 parent 5177d03 commit 71cbeef
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 32 deletions.
16 changes: 9 additions & 7 deletions core/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ const hundreds = require('../lib/hundreds');
const thousands = require('../lib/thousands');

module.exports = (date) => {
// We need to sanitize the date by first creating a new date and then getting the individual values for day, month, & year.
let dateMap = parseDate(date);
let stringifiedYear = dateMap.year.toString();
// We need to sanitize the date by first creating a new date and then
// getting the individual values for day, month, & year.
const dateMap = parseDate(date);
const stringifiedYear = dateMap.year.toString();

// 10/23/1945 => October twenty third nineteen fourty five
let month = dateStrings.months[dateMap.month];
let day = dateStrings.ordinals[dateMap.day];
const month = dateStrings.months[dateMap.month];
const day = dateStrings.ordinals[dateMap.day];
let year = '';

if (dateMap.year % 100 === 0) {
// Then this is a year to be suffixed with thousand (e.g. 2000) or with a hundred (e.g. 1900)
// Then this is a year to be suffixed with thousand
// (e.g. 2000) or with a hundred (e.g. 1900)
year = centuries(stringifiedYear);
} else {
if (dateMap.year >= 1000) {
year += thousands(stringifiedYear);
}

if (dateMap.year >= 99) {
year += hundreds(stringifiedYear);
}
Expand Down
80 changes: 75 additions & 5 deletions core/dateStrings.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,84 @@
module.exports = {
months: [
'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
],
ordinals: [
'', 'first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eigth', 'ninth', 'tenth', 'eleventh', 'twelvth', 'thirteenth', 'fourteenth', 'fifteenth', 'sixteenth', 'seventeenth', 'eighteenth', 'nineteenth', 'twentieth', 'twenty first', 'twenty second', 'twenty third', 'twenty fourth', 'twenty fifth', 'twenty sixth', 'twenty seventh', 'twenty eight', 'twenty nine', 'thirtieth', 'thirty first'
'',
'first',
'second',
'third',
'fourth',
'fifth',
'sixth',
'seventh',
'eigth',
'ninth',
'tenth',
'eleventh',
'twelvth',
'thirteenth',
'fourteenth',
'fifteenth',
'sixteenth',
'seventeenth',
'eighteenth',
'nineteenth',
'twentieth',
'twenty first',
'twenty second',
'twenty third',
'twenty fourth',
'twenty fifth',
'twenty sixth',
'twenty seventh',
'twenty eight',
'twenty nine',
'thirtieth',
'thirty first',
],
tens: [
'', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'
'',
'ten',
'twenty',
'thirty',
'forty',
'fifty',
'sixty',
'seventy',
'eighty',
'ninety',
],
ones: [
'', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
]
'',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten',
'eleven',
'twelve',
'thirteen',
'fourteen',
'fifteen',
'sixteen',
'seventeen',
'eighteen',
'nineteen',
],
};
14 changes: 7 additions & 7 deletions lib/centuries.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ const dateStrings = require('../core/dateStrings');
module.exports = (year) => {
if (year % 1000 === 0) {
// Then this is a year to be suffixed with thousand. EX: 2000, 5000, 17000, etc.
let firstDigit = parseInt(year.charAt(0));

return `${dateStrings.ones[firstDigit]} thousand`;
} else {
// Then this is a year that needs to be suffixed with hundred. EX: 1500, 1900, etc.
let firstTwoDigits = parseInt(year.slice(0, 2));
const firstDigit = parseInt(year.charAt(0), 10);

return `${dateStrings.ones[firstTwoDigits]} hundred`;
return `${dateStrings.ones[firstDigit]} thousand`;
}

// Then this is a year that needs to be suffixed with hundred. EX: 1500, 1900, etc.
const firstTwoDigits = parseInt(year.slice(0, 2), 10);

return `${dateStrings.ones[firstTwoDigits]} hundred`;
};
12 changes: 6 additions & 6 deletions lib/decades.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ module.exports = (year) => {
let decade = null;

if (year.length === 4) {
decade = parseInt(year.slice(2));
decade = parseInt(year.slice(2), 10);
} else {
decade = parseInt(year.slice(1));
decade = parseInt(year.slice(1), 10);
}

if (decade === 0) {
// 1900
return ``;
return '';
} else if (decade < 20) {
// 2019
return `${dateStrings.ones[decade]}`;
}

// 29 / 10 => parseInt(2.9) => 2
let tensDigit = parseInt(decade / 10);
let onesDigit = decade % 10;
const tensDigit = parseInt(decade / 10, 10);
const onesDigit = decade % 10;

return `${dateStrings.tens[tensDigit]} ${dateStrings.ones[onesDigit]}`;
};
12 changes: 6 additions & 6 deletions lib/hundreds.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ const dateStrings = require('../core/dateStrings');
module.exports = (year) => {
let frontPair = null;
let hundredDigit = null;

if (year.length === 4) {
frontPair = parseInt(year.slice(0, 2));
hundredDigit = parseInt(year.charAt(1));
frontPair = parseInt(year.slice(0, 2), 10);
hundredDigit = parseInt(year.charAt(1), 10);
} else {
// Three digit year - 465
frontPair = parseInt(year.slice(0, 1));
hundredDigit = parseInt(year.charAt(0));
frontPair = parseInt(year.slice(0, 1), 10);
hundredDigit = parseInt(year.charAt(0), 10);
}

if (hundredDigit === 0) {
// 1000
return ``;
return '';
} else if (frontPair < 20) {
// 1900
return `${dateStrings.ones[frontPair]} hundred `;
Expand Down
1 change: 1 addition & 0 deletions lib/parseDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = (dateInput) => {
// String will be a standard input, number is most likely a UTC date.
case 'string':
case 'number':
// eslint-disable-next-line no-param-reassign
dateInput = new Date(dateInput);
break;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "date-convert",
"version": "0.1.2",
"version": "0.1.3",
"description": "Convert a Date into a human readable sentence.",
"main": "./core/convert.js",
"keywords": [
Expand Down

0 comments on commit 71cbeef

Please sign in to comment.