Skip to content

Commit

Permalink
Merge pull request #3 from LanceTurri/develop
Browse files Browse the repository at this point in the history
Merging developer to master.
  • Loading branch information
LanceTurri committed Sep 7, 2017
2 parents eabe17d + 71cbeef commit db8f3ab
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 37 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Build Status](https://travis-ci.org/LanceTurri/date-convert.svg?branch=develop)](https://travis-ci.org/LanceTurri/date-convert)
[![Coverage Status](https://coveralls.io/repos/github/LanceTurri/date-convert/badge.svg?branch=develop)](https://coveralls.io/github/LanceTurri/date-convert?branch=develop)

This library is meant for applications to take dates in a standard format (`string` or `Date object`) and convert them into human readable, sentence format. It's especially useful for ADA optimizations since not all screen readers handle date shorthand very well.
This library is meant for applications to take dates in a standard format (`string` or `Date object`) and convert them into a human readable, sentence format.

## Installation

Expand Down Expand Up @@ -35,7 +35,7 @@ Any format that can be read by the Date constructor will be able to be converted
```javascript
convertDate('2010-03-25') // => 'March twenty fifth, two thousand ten'
convertDate('07/04/1776') // => 'July fourth, seventeen seventy six'
convertDate('August 29 2456') // => 'August twenty ninth, two thousand fifty six'
convertDate('August 29 2456') // => 'August twenty ninth, two thousand four hundred fifty six'
convertDate('01/01/2000') // => 'January first, two thousand'
convertDate('Tuesday March 15, 462') // => 'March fifteenth, four hundred sixty two'
```
Expand Down
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "date-convert",
"version": "0.1.1",
"version": "0.1.3",
"description": "Convert a Date into a human readable sentence.",
"main": "./core/convert.js",
"keywords": [
Expand All @@ -14,7 +14,7 @@
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/LanceTurri/date-translator"
"url": "https://github.com/LanceTurri/date-convert"
},
"scripts": {
"test": "nyc --reporter=html --reporter=text mocha",
Expand Down
9 changes: 7 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ describe('it also converts special case years', function() {
thirdZero: "03/03/2103",
century: "04/04/1900",
millenium: "05/05/2000",
random1: "06/06/2045",
nearFuture: "06/06/2045",
farFuture: "07/07/2456",
subThousand: "06/06/465"
}

Expand Down Expand Up @@ -104,6 +105,10 @@ describe('it also converts special case years', function() {
});

it('converts a short date string (06/06/2045)', function() {
assert.equal(translate(testValues.random1), 'June sixth, two thousand forty five');
assert.equal(translate(testValues.nearFuture), 'June sixth, two thousand forty five');
});

it('converts a short date string (07/07/2456)', function() {
assert.equal(translate(testValues.farFuture), 'July seventh, two thousand four hundred fifty six');
});
});

0 comments on commit db8f3ab

Please sign in to comment.