diff --git a/src/dateString.test.js b/src/dateString.test.js index 6667558..20bab2c 100644 --- a/src/dateString.test.js +++ b/src/dateString.test.js @@ -2,7 +2,7 @@ import test from 'ava'; import moment from 'moment'; import { dateString } from './index'; -if (dateString()) { +if (dateString('d') != null) { test('Nominal', t => { const actual = 1485470818000; const expected = 'January 26, 2017'; diff --git a/src/index.js b/src/index.js index 17daa32..776e2c9 100644 --- a/src/index.js +++ b/src/index.js @@ -1,12 +1,27 @@ - +import moment from "moment"; /** * CODING CHALLENGE 1 * @param {String} str the string to format * @see `codingChallenge.md` for instructions */ -export function titleCase (str) { +export function titleCase(str) { // CODE HERE - return false; + try { + if (str != null) { + const strReplace = str.replace(/[^a-zA-Z0-9]+/g, " "); + const lowCase = strReplace.toLowerCase().trim(); + const splitWords = lowCase.split(" "); + + for (let i = 0; i < splitWords.length; i++) { + splitWords[i] = + splitWords[i][0].toUpperCase() + splitWords[i].substr(1); + } + + return splitWords.join(" "); + } + } catch (error) { + throw new Error("Null Input"); + } } /** @@ -14,8 +29,20 @@ export function titleCase (str) { * @param {Number} timestamp the timestamp to format * @see `codingChallenge.md` for instructions */ -export function dateString (timestamp) { +export function dateString(timestamp) { // CODE HERE - return false; -} + try { + if (!timestamp && !null) { + const today = new moment().format("MMMM D, YYYY"); + return today; + } else if (timestamp != null) { + const toDate = new Date(timestamp); + let formattedDate = moment(toDate).format("MMMM D, YYYY"); + + return formattedDate; + } + } catch (error) { + throw new Error("Null Input"); + } +}