From 3f895e386167355218e6326b28c8e0918d25c23f Mon Sep 17 00:00:00 2001 From: Lance Turri Date: Wed, 6 Sep 2017 23:36:56 -0500 Subject: [PATCH 1/3] Updated readme and fixed typo in the repository url. --- README.md | 2 +- package.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 709f9f9..372e65d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index ad37bf0..9bde0fe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "date-convert", - "version": "0.1.1", + "version": "0.1.2", "description": "Convert a Date into a human readable sentence.", "main": "./core/convert.js", "keywords": [ @@ -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", From 5177d0370b137d3f70cdd053b7a02e923d8762e2 Mon Sep 17 00:00:00 2001 From: Lance Turri Date: Wed, 6 Sep 2017 23:44:15 -0500 Subject: [PATCH 2/3] Fixed typo in readme, updated tests to match. --- README.md | 2 +- test/test.js | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 372e65d..682e6f9 100644 --- a/README.md +++ b/README.md @@ -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' ``` diff --git a/test/test.js b/test/test.js index 29d7537..d8af173 100644 --- a/test/test.js +++ b/test/test.js @@ -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" } @@ -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'); }); }); From 71cbeef189d2673ff64c5ed5ed3bbdaf7f39ee8f Mon Sep 17 00:00:00 2001 From: Lance Turri Date: Wed, 6 Sep 2017 23:59:00 -0500 Subject: [PATCH 3/3] Minor updates to files to make linter happy. --- core/convert.js | 16 +++++---- core/dateStrings.js | 80 ++++++++++++++++++++++++++++++++++++++++++--- lib/centuries.js | 14 ++++---- lib/decades.js | 12 +++---- lib/hundreds.js | 12 +++---- lib/parseDate.js | 1 + package.json | 2 +- 7 files changed, 105 insertions(+), 32 deletions(-) diff --git a/core/convert.js b/core/convert.js index f9325a6..9b9568e 100644 --- a/core/convert.js +++ b/core/convert.js @@ -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); } diff --git a/core/dateStrings.js b/core/dateStrings.js index 0d5c0b3..6cdd15c 100644 --- a/core/dateStrings.js +++ b/core/dateStrings.js @@ -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', + ], }; diff --git a/lib/centuries.js b/lib/centuries.js index 3bfd5a6..c17732b 100644 --- a/lib/centuries.js +++ b/lib/centuries.js @@ -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`; }; diff --git a/lib/decades.js b/lib/decades.js index ad87d18..b90df27 100644 --- a/lib/decades.js +++ b/lib/decades.js @@ -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]}`; }; diff --git a/lib/hundreds.js b/lib/hundreds.js index ebeb3d5..565fe5e 100644 --- a/lib/hundreds.js +++ b/lib/hundreds.js @@ -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 `; diff --git a/lib/parseDate.js b/lib/parseDate.js index 0d5bea0..2384654 100644 --- a/lib/parseDate.js +++ b/lib/parseDate.js @@ -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; diff --git a/package.json b/package.json index 9bde0fe..a881092 100644 --- a/package.json +++ b/package.json @@ -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": [