From 3755d5a4e89c26bc57ad4a78b8c733f24e34dd08 Mon Sep 17 00:00:00 2001 From: jmsbrett Date: Mon, 11 Oct 2021 13:48:29 -0500 Subject: [PATCH 1/2] adding tests for scenerio 1 titleCase minus null return error --- src/dateString.test.js | 2 +- src/index.js | 26 +++++++++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) 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..f7e1607 100644 --- a/src/index.js +++ b/src/index.js @@ -1,12 +1,29 @@ - /** * 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"); + // error = new Error("Null Input") + // console.log(error + " ERROR") + // return new Error("Null Input") + // } } /** @@ -14,8 +31,7 @@ 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; } - From 64659b75a0e826440ccd8b88fb1d8cb79d8dec63 Mon Sep 17 00:00:00 2001 From: jmsbrett Date: Mon, 11 Oct 2021 15:12:20 -0500 Subject: [PATCH 2/2] adding code for date minus null --- src/index.js | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index f7e1607..776e2c9 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,4 @@ +import moment from "moment"; /** * CODING CHALLENGE 1 * @param {String} str the string to format @@ -5,8 +6,8 @@ */ export function titleCase(str) { // CODE HERE - // try { - // if (str != null) { + try { + if (str != null) { const strReplace = str.replace(/[^a-zA-Z0-9]+/g, " "); const lowCase = strReplace.toLowerCase().trim(); const splitWords = lowCase.split(" "); @@ -17,13 +18,10 @@ export function titleCase(str) { } return splitWords.join(" "); - // } - // } catch (error) { - // throw new Error("Null Input"); - // error = new Error("Null Input") - // console.log(error + " ERROR") - // return new Error("Null Input") - // } + } + } catch (error) { + throw new Error("Null Input"); + } } /** @@ -33,5 +31,18 @@ export function titleCase(str) { */ 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"); + } }