Skip to content

Commit 6f13de9

Browse files
Merge pull request #10 from danilhendrasr/refactoring
Fix bad variable namings and hard coded values
2 parents cbeacff + 47f1608 commit 6f13de9

File tree

7 files changed

+95
-62
lines changed

7 files changed

+95
-62
lines changed

algorithms-helper/bubble-sort.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ACTIVE_BAR_COLOR, INACTIVE_BAR_COLOR } from "../constants"
1+
import { AnimateFunctionParams } from "../types"
22
import {
33
changeBarsColor,
44
getAllBars,
@@ -42,11 +42,8 @@ const bubbleSort = (array: number[]) => {
4242
return animaSeq
4343
}
4444

45-
export const animateBubbleSort = (
46-
barHeights: number[],
47-
sortingSpeed: number,
48-
callback?: () => void
49-
) => {
45+
export const animateBubbleSort = (params: AnimateFunctionParams) => {
46+
const { barHeights, palette, sortingSpeed, callback } = params
5047
const animationSequence = bubbleSort(barHeights)
5148
const bars = getAllBars()
5249

@@ -57,25 +54,25 @@ export const animateBubbleSort = (
5754
if (!isFirstIteration) {
5855
const [prevFirstBarIdx, prevSecondBarIdx] = animationSequence[iteration - 1]
5956

60-
changeBarsColor(
61-
[bars[prevFirstBarIdx], bars[prevSecondBarIdx]],
62-
INACTIVE_BAR_COLOR
63-
)
57+
changeBarsColor([bars[prevFirstBarIdx], bars[prevSecondBarIdx]], palette.inactive)
6458
}
6559

6660
const activeBarHeights = [
6761
getNumberFromHeightString(bars[firstBarIdx].style.height),
6862
getNumberFromHeightString(bars[secondBarIdx].style.height),
6963
]
7064

71-
makeBarsActive([
72-
{ element: bars[firstBarIdx], height: activeBarHeights[1] },
73-
{ element: bars[secondBarIdx], height: activeBarHeights[0] },
74-
])
65+
makeBarsActive(
66+
[
67+
{ element: bars[firstBarIdx], height: activeBarHeights[1] },
68+
{ element: bars[secondBarIdx], height: activeBarHeights[0] },
69+
],
70+
palette.active
71+
)
7572

7673
const isLastIteration = iteration === animationSequence.length - 1
7774
if (isLastIteration && callback) {
78-
postSortAnimation(bars, ACTIVE_BAR_COLOR)
75+
postSortAnimation(bars, palette.active)
7976
callback()
8077
}
8178
}, iteration * sortingSpeed)

algorithms-helper/insertion-sort.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { INACTIVE_BAR_COLOR, ACTIVE_BAR_COLOR } from "../constants"
1+
import { AnimateFunctionParams } from "../types"
22
import { getAllBars, changeBarsColor, makeBarsActive, postSortAnimation } from "../utils"
33

44
interface InsertionAnimationSequence {
@@ -52,11 +52,8 @@ const insertionSort = (arrayToSort: number[]): InsertionAnimationSequence[] => {
5252
return animaSeq
5353
}
5454

55-
export const animateInsertionSort = (
56-
barHeights: number[],
57-
sortingSpeed: number,
58-
callback?: () => void
59-
) => {
55+
export const animateInsertionSort = (params: AnimateFunctionParams) => {
56+
const { barHeights, palette, sortingSpeed, callback } = params
6057
const animationSequence = insertionSort(barHeights)
6158
const bars = getAllBars()
6259

@@ -73,11 +70,14 @@ export const animateInsertionSort = (
7370

7471
setTimeout(() => {
7572
if (!isFirstIteration) {
76-
changeBarsColor(bars[animationSequence[i - 1].idx], INACTIVE_BAR_COLOR)
73+
changeBarsColor(bars[animationSequence[i - 1].idx], palette.inactive)
7774
}
7875

7976
prevIdxBarHeights.push(bars[idxToInsertTo].style.height)
80-
makeBarsActive([{ element: bars[idxToInsertTo], height: barHeights[barToMove] }])
77+
makeBarsActive(
78+
[{ element: bars[idxToInsertTo], height: barHeights[barToMove] }],
79+
palette.active
80+
)
8181
}, i * sortingSpeed)
8282

8383
for (let x = 1; x <= rightShift; x++) {
@@ -87,8 +87,8 @@ export const animateInsertionSort = (
8787

8888
const isLastInnerIteration = x === rightShift
8989
if (isLastInnerIteration && isLastIteration) {
90-
changeBarsColor(bars[idxToInsertTo], INACTIVE_BAR_COLOR)
91-
postSortAnimation(bars, ACTIVE_BAR_COLOR)
90+
changeBarsColor(bars[idxToInsertTo], palette.inactive)
91+
postSortAnimation(bars, palette.active)
9292
if (callback) callback()
9393
}
9494
}, i * sortingSpeed)

algorithms-helper/selection-sort.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { INACTIVE_BAR_COLOR, ACTIVE_BAR_COLOR } from "../constants"
2-
import { ActiveBar } from "../types"
1+
import { ActiveBar, AnimateFunctionParams } from "../types"
32
import { changeBarsColor, getAllBars, makeBarsActive, postSortAnimation } from "../utils"
43

54
const selectionSort = (inputArray: number[]) => {
@@ -44,11 +43,8 @@ const selectionSort = (inputArray: number[]) => {
4443
return [arrayState, animationSequence]
4544
}
4645

47-
export const animateSelectionSort = (
48-
barHeights: number[],
49-
sortingSpeed: number,
50-
callback?: () => void
51-
) => {
46+
export const animateSelectionSort = (params: AnimateFunctionParams) => {
47+
const { barHeights, palette, sortingSpeed, callback } = params
5248
const [arrayStates, animationSequence] = selectionSort(barHeights)
5349
const bars = getAllBars()
5450

@@ -81,23 +77,23 @@ export const animateSelectionSort = (
8177
let prevAnimationSequence = animationSequence[i - 1]
8278
let [prevBar1Idx, prevBar2Idx] = prevAnimationSequence
8379
let previousActiveBars = [bars[prevBar1Idx], bars[prevBar2Idx]]
84-
changeBarsColor(previousActiveBars, INACTIVE_BAR_COLOR)
80+
changeBarsColor(previousActiveBars, palette.inactive)
8581
}
8682

87-
makeBarsActive(barsToActiveBars)
83+
makeBarsActive(barsToActiveBars, palette.active)
8884

8985
if (lastIteration) {
9086
// Revert the last bar to an inactive bar
9187
setTimeout(() => {
9288
let lastBar = activeBar1
93-
changeBarsColor(lastBar, INACTIVE_BAR_COLOR)
89+
changeBarsColor(lastBar, palette.inactive)
9490

9591
// Because this animate function executes asynchronous function (setTimeout)
9692
// if we wanted to do something right after this function is done running,
9793
// we have to put the code inside the last setTimeout.
9894
// Otherwise, the code will get executed without waiting for the setTimeouts
9995
// to get executed.
100-
postSortAnimation(bars, ACTIVE_BAR_COLOR)
96+
postSortAnimation(bars, palette.active)
10197
if (callback) callback()
10298
}, sortingSpeed)
10399
}

constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { SortingAlgorithms, SortingSpeeds } from "./types"
22

3-
export const ACTIVE_BAR_COLOR = "#111"
4-
export const INACTIVE_BAR_COLOR = "#ff1a1a"
53
export const ALGORITHMS_LIST: SortingAlgorithms[] = ["Selection", "Insertion", "Bubble"]
64
export const sortingSpeedTable: SortingSpeeds = {
75
slow: 160,

pages/index.tsx

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
import styles from "../styles/Home.module.scss"
33

44
import React, { useEffect, useState } from "react"
5-
import { changeBarsColor, generateBarHeights, getAllBars, startAnimation } from "../utils"
5+
import {
6+
changeBarsColor,
7+
generateBarHeights,
8+
getAllBars,
9+
hexToRgb,
10+
startAnimation,
11+
} from "../utils"
612
import {
713
AppTitle,
814
AlgorithmSelector,
@@ -14,8 +20,8 @@ import {
1420
SortButton,
1521
SpeedControl,
1622
} from "../components"
17-
import { Spacer } from "@geist-ui/react"
18-
import { INACTIVE_BAR_COLOR, sortingSpeedTable } from "../constants"
23+
import { Spacer, useTheme } from "@geist-ui/react"
24+
import { sortingSpeedTable } from "../constants"
1925
import { SortingAlgorithms, SortingSpeeds, SortingState } from "../types"
2026
import { default as Head } from "next/head"
2127
import { AppStateContext } from "../contexts/app-state"
@@ -33,36 +39,44 @@ const getBarElementsFromBarHeights = (barHeights: number[]) => {
3339
}
3440

3541
const Home: React.FC = () => {
42+
const { palette: geistUIPalette } = useTheme()
43+
const palette = {
44+
active: geistUIPalette.foreground,
45+
inactive: geistUIPalette.errorLight,
46+
}
47+
3648
const [barHeights, setBarHeights] = useState<number[]>([])
37-
const [sortState, setSortState] = useState<SortingState>("Sort")
38-
const [barLength, setBarLength] = useState(70)
49+
const [sortingState, setSortingState] = useState<SortingState>("Sort")
50+
const [arrayLength, setArrayLength] = useState(70)
3951
const [selectedAlgorithm, setSelectedAlgorithm] = useState<SortingAlgorithms>(
4052
"Selection"
4153
)
4254
const [sortingSpeed, setSortingSpeed] = useState<keyof SortingSpeeds>("normal")
4355

44-
useEffect(resetBars, [barLength])
56+
useEffect(resetBars, [arrayLength])
4557
useEffect(resetBars, [selectedAlgorithm])
4658

4759
function resetBars() {
48-
const newBarHeights = generateBarHeights(barLength)
60+
const newBarHeights = generateBarHeights(arrayLength)
4961
setBarHeights(newBarHeights)
50-
setSortState("Sort")
62+
setSortingState("Sort")
5163
requestAnimationFrame(() => {
5264
const barsDomEl = getAllBars()
53-
if (barsDomEl[5].style.backgroundColor === "rgb(17, 17, 17)") {
54-
changeBarsColor(barsDomEl, INACTIVE_BAR_COLOR)
65+
const sampleBg = barsDomEl[5].style.backgroundColor
66+
if (sampleBg === hexToRgb(palette.active)) {
67+
changeBarsColor(barsDomEl, palette.inactive)
5568
}
5669
})
5770
}
5871

5972
function triggerAnimation() {
60-
setSortState("Sorting")
73+
setSortingState("Sorting")
6174
startAnimation({
62-
sortingAlgorithm: selectedAlgorithm,
6375
barHeights,
76+
palette,
77+
sortingAlgorithm: selectedAlgorithm,
6478
sortingSpeed: sortingSpeedTable[sortingSpeed],
65-
callback: () => setSortState("Sorted"),
79+
callback: () => setSortingState("Sorted"),
6680
})
6781
}
6882

@@ -73,7 +87,7 @@ const Home: React.FC = () => {
7387
</Head>
7488

7589
<div className={styles.container}>
76-
<AppStateContext.Provider value={{ sortingState: sortState }}>
90+
<AppStateContext.Provider value={{ sortingState }}>
7791
<div className={styles.sidebarContainer}>
7892
<Spacer y={1} />
7993
<AppTitle />
@@ -83,8 +97,8 @@ const Home: React.FC = () => {
8397
selectedAlgorithm={selectedAlgorithm}
8498
/>
8599
<ArrayLengthModifier
86-
onChange={(value) => setBarLength(value)}
87-
value={barLength}
100+
onChange={(value) => setArrayLength(value)}
101+
value={arrayLength}
88102
/>
89103
<Spacer y={0.5} />
90104
<SpeedControl

types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ export interface SortingSpeeds {
66
normal: number
77
fast: number
88
}
9+
export interface AnimateFunctionParams {
10+
barHeights: number[]
11+
palette: { active: string; inactive: string }
12+
sortingSpeed: number
13+
callback?: () => void
14+
}

utils/index.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { animateBubbleSort } from "../algorithms-helper/bubble-sort"
22
import { animateInsertionSort } from "../algorithms-helper/insertion-sort"
33
import { animateSelectionSort } from "../algorithms-helper/selection-sort"
4-
import { ACTIVE_BAR_COLOR } from "../constants"
5-
import { ActiveBar, SortingAlgorithms, SortingSpeeds } from "../types"
4+
import { ActiveBar, SortingAlgorithms } from "../types"
65

76
export const generateARandomNumber = (min: number, max: number) => {
87
min = Math.ceil(min)
@@ -47,9 +46,9 @@ export const changeBarsColor = (
4746
}
4847
}
4948

50-
export const makeBarsActive = (bars: ActiveBar[]) => {
49+
export const makeBarsActive = (bars: ActiveBar[], activeBarColor: string) => {
5150
for (const bar of bars) {
52-
changeBarsColor(bar.element, ACTIVE_BAR_COLOR)
51+
changeBarsColor(bar.element, activeBarColor)
5352
bar.element.style.height = `${bar.height}%`
5453
}
5554
}
@@ -82,23 +81,46 @@ export const getNumberFromHeightString = (height: string) => {
8281

8382
interface StartAnimationParams {
8483
barHeights: number[]
84+
palette: { active: string; inactive: string }
8585
sortingAlgorithm: SortingAlgorithms
8686
sortingSpeed: number
8787
callback?: () => void
8888
}
8989

9090
export const startAnimation = (params: StartAnimationParams) => {
91-
const { barHeights, sortingAlgorithm, sortingSpeed, callback } = params
91+
const { barHeights, palette, sortingAlgorithm, sortingSpeed, callback } = params
92+
const animationParams = {
93+
barHeights,
94+
palette,
95+
sortingSpeed,
96+
callback,
97+
}
9298

9399
switch (sortingAlgorithm) {
94100
case "Selection":
95-
animateSelectionSort(barHeights, sortingSpeed, callback)
101+
animateSelectionSort(animationParams)
96102
break
97103
case "Insertion":
98-
animateInsertionSort(barHeights, sortingSpeed, callback)
104+
animateInsertionSort(animationParams)
99105
break
100106
case "Bubble":
101-
animateBubbleSort(barHeights, sortingSpeed, callback)
107+
animateBubbleSort(animationParams)
102108
break
103109
}
104110
}
111+
112+
export const hexToRgb = (hex: string) => {
113+
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i
114+
hex = hex.replace(shorthandRegex, function (m, r, g, b) {
115+
return r + r + g + g + b + b
116+
})
117+
118+
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
119+
const rgb = {
120+
r: parseInt(result[1], 16),
121+
g: parseInt(result[2], 16),
122+
b: parseInt(result[3], 16),
123+
}
124+
125+
return result ? `rgb(${rgb.r}, ${rgb.g}, ${rgb.b})` : null
126+
}

0 commit comments

Comments
 (0)