Test of using TypeScript. See post here: https://www.devbanana.me/static-typing/2021/09/23/typescript-for-statically-typed-javascript.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getWord(text: string, word: number) { | |
if (word < 1) throw new Error('word must be no less than 1'); | |
const wordIndex = word - 1; | |
// https://regex101.com/r/1zDG8a/2 | |
const words = text.toLowerCase().match(/\w+(?:'\w+)*/g); | |
if (words === null) { | |
throw new Error('Please provide some text'); | |
} | |
if (wordIndex >= words.length) { | |
throw new Error(`Text only has ${words.length} words`); | |
} | |
return words[wordIndex]; | |
} | |
console.log(getWord('This is a test.', 2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"compilerOptions": { | |
"target": "ES2015", | |
"noEmitOnError": true, | |
"strict": true | |
}, | |
"files": ["./index.ts"] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment