diff --git a/package-lock.json b/package-lock.json index 48a7f16..607514a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "devDependencies": { "@types/chai": "^4.3.4", "@types/mocha": "^10.0.1", + "@types/node": "^22.7.5", "chai": "^4.3.7", "mocha": "^10.2.0", "ts-node": "^10.9.2", @@ -97,7 +98,6 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-22.7.5.tgz", "integrity": "sha512-jML7s2NAzMWc//QSJ1a3prpk78cOPchGvXJsC3C6R6PSMoooztvRVQEz89gmBTBY1SPMaqo5teB4uNHPdetShQ==", "dev": true, - "peer": true, "dependencies": { "undici-types": "~6.19.2" } @@ -1022,8 +1022,7 @@ "version": "6.19.8", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz", "integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==", - "dev": true, - "peer": true + "dev": true }, "node_modules/v8-compile-cache-lib": { "version": "3.0.1", @@ -1218,7 +1217,6 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-22.7.5.tgz", "integrity": "sha512-jML7s2NAzMWc//QSJ1a3prpk78cOPchGvXJsC3C6R6PSMoooztvRVQEz89gmBTBY1SPMaqo5teB4uNHPdetShQ==", "dev": true, - "peer": true, "requires": { "undici-types": "~6.19.2" } @@ -1877,8 +1875,7 @@ "version": "6.19.8", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz", "integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==", - "dev": true, - "peer": true + "dev": true }, "v8-compile-cache-lib": { "version": "3.0.1", diff --git a/package.json b/package.json index 402d5e1..9a99052 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "devDependencies": { "@types/chai": "^4.3.4", "@types/mocha": "^10.0.1", + "@types/node": "^22.7.5", "chai": "^4.3.7", "mocha": "^10.2.0", "ts-node": "^10.9.2", @@ -20,4 +21,4 @@ "description": "Simple coding challenge for Evie!", "main": "dist/index.js", "author": "Evie Networks" -} \ No newline at end of file +} diff --git a/src/index.ts b/src/index.ts index 946d794..a9b3e14 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,27 @@ // The CLI entry point should live here! +import { reverseSentence } from './reverse'; + +// This line is used to receive the arguments from the command line + skipping 'node' and the script path +const args = process.argv.slice(2); + +// The first argument is the sentence and any other arguments are options. +const sentence = args[0] || ''; + +// Check if the user wants to reverse words or letters +const reverseWords = args.includes('--word'); +const reverseLetters = args.includes('--letter'); + +// Try to reverse sentence based on the options and catch if theres any error +try { + const result = reverseSentence(sentence, reverseWords, reverseLetters); + console.log(result); +} catch (error) { + //Narrow down to handle errors as typescript error + if (error instanceof Error) { + console.error(error.message); + } else { + console.error("An unknown error occurred."); + } +} + + diff --git a/src/reverse.ts b/src/reverse.ts index 6d2dbb5..7e9d41a 100644 --- a/src/reverse.ts +++ b/src/reverse.ts @@ -3,5 +3,31 @@ export const reverseSentence = ( reverseWords: boolean, reverseLetters: boolean ): string => { - throw new Error("Not implemented yet!"); + try { + // Split the provided sentence into an array as soon as space found. + let words = sentence.split(' '); + + // This reverse the order of the words + if (reverseWords) { + words.reverse(); + } + + // This reverse the letters in each words + if (reverseLetters) { + words = words.map(word => word.split('').reverse().join('')); + } + + // Join all the words back together and return the result. + return words.join(' '); + } catch (error) { + //Narrow down to handle errors as typescript error + if (error instanceof Error) { + console.error('An error occurred:', error.message); + throw new Error(error.message || 'Failed to reverse sentence.'); + } else { + console.error('An unexpected error occurred:', error); + throw new Error('Failed to reverse sentence due to an unknown error.'); + } + } }; +