-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chapter 05: making fizzBuzz open for extension, step 1
- Loading branch information
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
chapter-05-the-dependency-inversion-principle/refactor/buzzRule.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function buzzRule() { | ||
function matches(number) { | ||
return number % 5 === 0; | ||
} | ||
|
||
function getReplacement() { | ||
return 'Buzz'; | ||
} | ||
|
||
return { | ||
matches, | ||
getReplacement, | ||
}; | ||
} | ||
|
||
module.exports = buzzRule; |
35 changes: 35 additions & 0 deletions
35
chapter-05-the-dependency-inversion-principle/refactor/fizzBuzz.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
function fizzBuzz() { | ||
function generateList(limit) { | ||
const list = []; | ||
|
||
for (let number = 1; number <= limit; number++) { | ||
list.push(generateElement(number)); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
function generateElement(number) { | ||
const fizzBuzzRule = fizzBuzzRule(); | ||
|
||
if (fizzBuzzRule.matches(number)) { | ||
return fizzBuzzRule.getReplacement(); | ||
} | ||
|
||
if (fizzRule.matches(number)) { | ||
return fizzRule.getReplacement(); | ||
} | ||
|
||
if (buzzRule.matches(number)) { | ||
return buzzRule.getReplacement(); | ||
} | ||
|
||
return number; | ||
} | ||
|
||
return { | ||
generateList, | ||
}; | ||
} | ||
|
||
module.exports = fizzBuzz; |
16 changes: 16 additions & 0 deletions
16
chapter-05-the-dependency-inversion-principle/refactor/fizzBuzzRule.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function fizzBuzzRule() { | ||
function matches(number) { | ||
return number % 3 && number && 5 === 0; | ||
} | ||
|
||
function getReplacement() { | ||
return 'FizzBuzz'; | ||
} | ||
|
||
return { | ||
matches, | ||
getReplacement, | ||
}; | ||
} | ||
|
||
module.exports = fizzBuzzRule; |
16 changes: 16 additions & 0 deletions
16
chapter-05-the-dependency-inversion-principle/refactor/fizzRule.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function fizzRule() { | ||
function matches(number) { | ||
return number % 3 === 0; | ||
} | ||
|
||
function getReplacement() { | ||
return 'Fizz'; | ||
} | ||
|
||
return { | ||
matches, | ||
getReplacement, | ||
}; | ||
} | ||
|
||
module.exports = fizzRule; |