-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…431) * fix(utils): make findInField return correct plot * refactor(utils): move findInField to its own file * test(utils): validate findInField * refactor(utils): simplify findInField
- Loading branch information
1 parent
dec631f
commit f0faa0f
Showing
6 changed files
with
64 additions
and
23 deletions.
There are no files selected for viewing
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
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 @@ | ||
/** @typedef {import("../index").farmhand.plotContent} farmhand.plotContent */ | ||
import { memoize } from './memoize' | ||
|
||
import { memoizationSerializer } from './' | ||
|
||
export const findInField = memoize( | ||
/** | ||
* @param {(?farmhand.plotContent)[][]} field | ||
* @param {function(?farmhand.plotContent): boolean} condition | ||
* @returns {?farmhand.plotContent} | ||
*/ | ||
(field, condition) => { | ||
for (const row of field) { | ||
for (const plot of row) { | ||
if (condition(plot)) { | ||
return plot | ||
} | ||
} | ||
} | ||
|
||
return null | ||
}, | ||
{ | ||
serializer: memoizationSerializer, | ||
} | ||
) |
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,33 @@ | ||
/** | ||
* @typedef {import("../components/Farmhand/Farmhand").farmhand.state['field']} farmhand.state.field | ||
*/ | ||
|
||
import { carrot, pumpkin } from '../data/crops' | ||
|
||
import { findInField } from './findInField' | ||
|
||
const carrotPlot = { | ||
itemId: carrot.id, | ||
fertilizerType: 'NONE', | ||
} | ||
|
||
describe('findInField', () => { | ||
/** @type farmhand.state.field} */ | ||
const field = [[null, carrotPlot, null]] | ||
|
||
test('returns the desired plot from the field', () => { | ||
const foundPlot = findInField(field, plot => { | ||
return plot?.itemId === carrot.id | ||
}) | ||
|
||
expect(foundPlot).toEqual(carrotPlot) | ||
}) | ||
|
||
test('yields null if desired plot is not in the field', () => { | ||
const foundPlot = findInField(field, plot => { | ||
return plot?.itemId === pumpkin.id | ||
}) | ||
|
||
expect(foundPlot).toEqual(null) | ||
}) | ||
}) |
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