generated from skedwards88/template-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split out some functions, add some tests
- Loading branch information
1 parent
0964d5a
commit f8da270
Showing
13 changed files
with
159 additions
and
55 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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
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,26 @@ | ||
import "jest-localstorage-mock"; | ||
import {hasVisitedSince} from "./hasVisitedSince"; | ||
|
||
describe("hasVisitedSince", () => { | ||
const stateName = "testLastVisited"; | ||
|
||
test("returns false if lastVisited is not set", () => { | ||
localStorage.removeItem(stateName); | ||
expect(hasVisitedSince(stateName, "20240429")).toBe(false); | ||
}); | ||
|
||
test("returns false if lastVisited is set to a date before the reset date", () => { | ||
localStorage.setItem(stateName, JSON.stringify("20240428")); | ||
expect(hasVisitedSince(stateName, "20240429")).toBe(false); | ||
}); | ||
|
||
test("returns true if lastVisited is set to the reset date", () => { | ||
localStorage.setItem(stateName, JSON.stringify("20240429")); | ||
expect(hasVisitedSince(stateName, "20240429")).toBe(true); | ||
}); | ||
|
||
test("returns true if lastVisited is set to a date after the reset date", () => { | ||
localStorage.setItem(stateName, JSON.stringify("20240430")); | ||
expect(hasVisitedSince(stateName, "20240429")).toBe(true); | ||
}); | ||
}); |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,28 @@ | ||
import {getSurroundingIndexes} from "@skedwards88/word_logic"; | ||
|
||
// Given indexes and a grid size | ||
// Gets the number of neighboring (diagonals included) input indexes for each index | ||
export function getConnectivity(indexes, gridSize) { | ||
// error if grid size is 0 or negative | ||
if (gridSize <= 0) { | ||
throw new Error("Grid size must be greater than 0"); | ||
} | ||
|
||
// error if index exceeds the grid size | ||
if (indexes.some((index) => index >= gridSize * gridSize || index < 0)) { | ||
throw new Error("Index is not within grid size"); | ||
} | ||
|
||
let connectivity = 0; | ||
indexes.forEach((index) => { | ||
const surroundingIndexesInGrid = getSurroundingIndexes({ | ||
index, | ||
numColumns: gridSize, | ||
numRows: gridSize, | ||
}); | ||
const surroundingIndexesInInput = surroundingIndexesInGrid.filter((neighbor) => neighbor != index && indexes.includes(neighbor)); | ||
connectivity += surroundingIndexesInInput.length; | ||
}); | ||
|
||
return connectivity; | ||
} |
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,71 @@ | ||
import {getConnectivity} from "./getConnectivity"; | ||
|
||
describe("getConnectivity", () => { | ||
test("returns the number of connections (directionally matters) between the input indexes (corner row)", () => { | ||
const indexes = [0, 1, 2]; | ||
const gridSize = 4; | ||
expect(getConnectivity(indexes, gridSize)).toBe(4); | ||
}); | ||
|
||
test("returns the number of connections (directionally matters) between the input indexes (corner column)", () => { | ||
const indexes = [8, 4, 0]; | ||
const gridSize = 4; | ||
expect(getConnectivity(indexes, gridSize)).toBe(4); | ||
}); | ||
|
||
test("returns the number of connections (directionally matters) between the input indexes (square)", () => { | ||
const indexes = [5, 6, 10, 9]; | ||
const gridSize = 4; | ||
expect(getConnectivity(indexes, gridSize)).toBe(12); | ||
}); | ||
|
||
test("duplicate indexes are counted twice", () => { | ||
const indexes = [5, 6, 10, 9, 5]; | ||
const gridSize = 4; | ||
expect(getConnectivity(indexes, gridSize)).toBe(15); | ||
}); | ||
|
||
test("works with singleton indexes input", () => { | ||
const indexes = [5]; | ||
const gridSize = 4; | ||
expect(getConnectivity(indexes, gridSize)).toBe(0); | ||
}); | ||
|
||
test("works with empty indexes input", () => { | ||
const indexes = []; | ||
const gridSize = 4; | ||
expect(getConnectivity(indexes, gridSize)).toBe(0); | ||
}); | ||
|
||
test("throws an error when the indexes array has elements not in the grid", () => { | ||
const indexes = [0, 4, 16]; | ||
const gridSize = 4; | ||
expect(() => getConnectivity(indexes, gridSize)).toThrow( | ||
"Index is not within grid size", | ||
); | ||
}); | ||
|
||
test("throws an error when the indexes array has negative elements", () => { | ||
const indexes = [0, 4, -1]; | ||
const gridSize = 4; | ||
expect(() => getConnectivity(indexes, gridSize)).toThrow( | ||
"Index is not within grid size", | ||
); | ||
}); | ||
|
||
test("throws an error when the gridSize is 0", () => { | ||
const indexes = [0, 1, 2]; | ||
const gridSize = 0; | ||
expect(() => getConnectivity(indexes, gridSize)).toThrow( | ||
"Grid size must be greater than 0", | ||
); | ||
}); | ||
|
||
test("throws an error when the gridSize is negative", () => { | ||
const indexes = [0, 1, 2]; | ||
const gridSize = -4; | ||
expect(() => getConnectivity(indexes, gridSize)).toThrow( | ||
"Grid size must be greater than 0", | ||
); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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,14 @@ | ||
export function parseUrlQuery() { | ||
const searchParams = new URLSearchParams(document.location.search); | ||
const query = searchParams.get("id"); | ||
|
||
// The seed query consists of two parts: the seed and the difficulty level, separated by an underscore | ||
let difficultyLevel; | ||
let seed; | ||
if (query) { | ||
[seed, difficultyLevel] = query.split("_"); | ||
difficultyLevel = parseInt(difficultyLevel); | ||
} | ||
|
||
return [seed, difficultyLevel]; | ||
} |