Skip to content

Commit 0e42fe0

Browse files
committed
refactor: reduce code duplication by adding checkLocation to FloodFill
1 parent 5f143d9 commit 0e42fe0

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

Diff for: Recursive/FloodFill.js

+13-16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ const neighbors = [
2020
[1, 1]
2121
]
2222

23+
function checkLocation(rgbData, location) {
24+
if (
25+
location[0] < 0 ||
26+
location[0] >= rgbData.length ||
27+
location[1] < 0 ||
28+
location[1] >= rgbData[0].length
29+
) {
30+
throw new Error('location should point to a pixel within the rgbData')
31+
}
32+
}
33+
2334
/**
2435
* Implements the flood fill algorithm through a breadth-first approach using a queue.
2536
*
@@ -34,14 +45,7 @@ export function breadthFirstSearch(
3445
targetColor,
3546
replacementColor
3647
) {
37-
if (
38-
location[0] < 0 ||
39-
location[0] >= rgbData.length ||
40-
location[1] < 0 ||
41-
location[1] >= rgbData[0].length
42-
) {
43-
throw new Error('location should point to a pixel within the rgbData')
44-
}
48+
checkLocation(rgbData, location)
4549

4650
const queue = []
4751
queue.push(location)
@@ -65,14 +69,7 @@ export function depthFirstSearch(
6569
targetColor,
6670
replacementColor
6771
) {
68-
if (
69-
location[0] < 0 ||
70-
location[0] >= rgbData.length ||
71-
location[1] < 0 ||
72-
location[1] >= rgbData[0].length
73-
) {
74-
throw new Error('location should point to a pixel within the rgbData')
75-
}
72+
checkLocation(rgbData, location)
7673

7774
depthFirstFill(rgbData, location, targetColor, replacementColor)
7875
}

0 commit comments

Comments
 (0)