Skip to content

Commit cea7a93

Browse files
committed
feat(guess-the-word): add util to remove children elements
1 parent 1fc4c92 commit cea7a93

File tree

5 files changed

+14
-7
lines changed

5 files changed

+14
-7
lines changed

vanilla/guess-the-word/src/events/handlers/difficulty-complete.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { difficulty } from "@/state/difficulty";
33
import { AvailableWords } from "@/state/words";
44
import { MasterDifficulty } from "@/utils/difficulty/master";
55
import { InsaneDifficulty } from "@/utils/difficulty/insane";
6+
import { clearChildren } from "@/utils/dom";
67
import { createWordLetters } from "@/ui/word";
78
import { $triesContainer } from "@/ui/tries";
89
import { $mistakesContainer } from "@/ui/mistakes";
@@ -33,7 +34,7 @@ export function handleDifficultyComplete() {
3334
import("@/ui/insane-countdown-bar").then(({ hideInsaneCountdown }) =>
3435
hideInsaneCountdown(),
3536
);
36-
while ($typing.firstChild != null) $typing.removeChild($typing.firstChild);
37+
clearChildren($typing);
3738
$typing.classList.add(CLASSES.HIDDEN);
3839

3940
showCompletedDifficultyMessage(difficulty, { allCompleted: gameCompleted });
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1+
import { clearChildren } from "@/utils/dom";
12
import { $allHintsList, $correctHintsList } from "./elements";
23

34
export const clearHints = () => {
4-
while ($allHintsList.firstChild != null)
5-
$allHintsList.removeChild($allHintsList.firstChild);
6-
while ($correctHintsList.firstChild != null)
7-
$correctHintsList.removeChild($correctHintsList.firstChild);
5+
clearChildren($allHintsList, $correctHintsList);
86
};

vanilla/guess-the-word/src/ui/typing/letter-fields.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getElementById, getElementBySelector } from "@lib/dom";
22
import { TypingLetterIndex } from "@/state/typing-letter";
3+
import { clearChildren } from "@/utils/dom";
34
import { createLetterLabel } from "./letter-label";
45
import { $typing } from "./elements";
56
import { CLASSES } from "@/consts/css-classes";
@@ -12,7 +13,7 @@ const $letterFields = [];
1213
/** @param {number} quantity */
1314
export function createLetterFields(quantity) {
1415
$letterFields.length = 0;
15-
while ($typing.firstChild != null) $typing.removeChild($typing.firstChild);
16+
clearChildren($typing);
1617

1718
for (let idx = 0; idx < quantity; idx++) {
1819
const $letterClone = /** @type {DocumentFragment} */ (

vanilla/guess-the-word/src/ui/word.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getElementById } from "@lib/dom";
22
import { currentWord } from "@/state/current-word";
3+
import { clearChildren } from "@/utils/dom";
34

45
const ATTRIBUTRES = Object.freeze({
56
LETTER: Object.freeze({
@@ -15,7 +16,7 @@ const lettersToUse = [""];
1516

1617
/** @param {string} word */
1718
export const createWordLetters = (word) => {
18-
while ($word.firstChild != null) $word.removeChild($word.firstChild);
19+
clearChildren($word);
1920
$wordLetters.length = 0;
2021
lettersToUse.length = 0;
2122

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/** @param {HTMLElement[]} $elements */
2+
export const clearChildren = (...$elements) => {
3+
for (const $element of $elements)
4+
while ($element.firstChild != null)
5+
$element.removeChild($element.firstChild);
6+
};

0 commit comments

Comments
 (0)