Created
May 15, 2020 22:04
-
-
Save ulisesantana/850b7c2e730c5a704b09fe1720db465b to your computer and use it in GitHub Desktop.
Fizz Buzz TDD with Deno
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
import fizzBuzz, { FIZZ, BUZZ, FIZZ_BUZZ } from "./fizzBuzz.ts"; | |
import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; | |
Deno.test( | |
"FizzBuzz should return the same number given if is not divisible by 3 or 5", | |
() => { | |
assertEquals(fizzBuzz(1), 1); | |
}, | |
); | |
Deno.test( | |
'FizzBuzz should return "Fizz" if the number given is divisible by 3', | |
() => { | |
assertEquals(fizzBuzz(6), FIZZ); | |
}, | |
); | |
Deno.test( | |
'FizzBuzz should return "Buzz" if the number given is divisible by 5', | |
() => { | |
assertEquals(fizzBuzz(25), BUZZ); | |
}, | |
); | |
Deno.test( | |
'FizzBuzz should return "FizzBuzz" if the number given is divisible by 5 and 3', | |
() => { | |
assertEquals(fizzBuzz(15), FIZZ_BUZZ); | |
}, | |
); |
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
import { | |
bgWhite, | |
magenta, | |
cyan, | |
bold, | |
} from "https://deno.land/std/fmt/colors.ts"; | |
const isDivisibleBy = (x: number) => (y: number) => y % x === 0; | |
const isDivisibleBy3 = isDivisibleBy(3); | |
const isDivisibleBy5 = isDivisibleBy(5); | |
const useBold = (color: Function, text: string): string => bold(color(text)); | |
export const FIZZ = useBold(magenta, "Fizz"); | |
export const BUZZ = useBold(cyan, "Buzz"); | |
export const FIZZ_BUZZ = useBold(bgWhite, FIZZ + BUZZ); | |
export default function fizzBuzz(num: number): string | number { | |
if (isDivisibleBy3(num) && isDivisibleBy5(num)) { | |
return FIZZ_BUZZ; | |
} | |
if (isDivisibleBy3(num)) { | |
return FIZZ; | |
} | |
if (isDivisibleBy5(num)) { | |
return BUZZ; | |
} | |
return num; | |
} |
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
import fizzBuzz from "./fizzBuzz.ts"; | |
Array.from({ length: 100 }).forEach((irrelevant, i) => { | |
console.log(fizzBuzz(++i)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment