Skip to content

Commit

Permalink
refactor(types): add more utils types
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Jul 24, 2023
1 parent 58ae656 commit e1e7e23
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 46 deletions.
2 changes: 0 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
/**
* Lookup table for the lifecycle durations of a crop (in days).
* @typedef farmhand.cropTimetable
* @type {Object}
* @property {number} seed
* @property {number} growing
* @readonly
Expand All @@ -23,7 +22,6 @@
/**
* Reference object for an item.
* @typedef farmhand.item
* @type {Object}
* @property {string} id
* @property {string} name
* @property {string} type
Expand Down
69 changes: 25 additions & 44 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/** @typedef {import("../index").farmhand.recipe} farmhand.recipe */
/** @typedef {import("../index").farmhand.priceEvent} farmhand.priceEvent */
/** @typedef {import("../enums").cropLifeStage} farmhand.cropLifeStage */
/** @typedef {import("../components/Farmhand/Farmhand").farmhand.state} farmhand.state */

/**
* @module farmhand.utils
Expand Down Expand Up @@ -727,27 +728,6 @@ export const findInField = memoize(
}
)

// This is currently unused, but it could be useful later.
/**
* @param {Array.<Array.<?farmhand.plotContent>>} field
* @param {function(?farmhand.plotContent)} filterCondition
* @returns {Array.<Array.<?farmhand.plotContent>>}
*/
export const getCrops = memoize(
(field, filterCondition) =>
field.reduce((acc, row) => {
acc.push(...row.filter(filterCondition))

return acc
}, []),
{
serializer: memoizationSerializer,
}
)

/**
* @returns {boolean}
*/
export const doesMenuObstructStage = () => window.innerWidth < BREAKPOINTS.MD

const itemTypesToShowInReverse = new Set([itemType.MILK])
Expand All @@ -773,12 +753,12 @@ export const sortItems = items => {
return sortItemIdsByTypeAndValue(items.map(({ id }) => id)).map(id => map[id])
}

/**
* @param {Array.<farmhand.item>} inventory
* @returns {number}
*/
export const inventorySpaceConsumed = memoize(inventory =>
inventory.reduce((sum, { quantity }) => sum + quantity, 0)
export const inventorySpaceConsumed = memoize(
/**
* @param {farmhand.item[]} inventory
* @returns {number}
*/
inventory => inventory.reduce((sum, { quantity = 0 }) => sum + quantity, 0)
)

/**
Expand Down Expand Up @@ -816,25 +796,26 @@ export const nullArray = memoize(
}
)

/**
* @param {Array.<farmhand.cow>} cowInventory
* @param {string} id
* @returns {farmhand.cow|undefined}
*/
export const findCowById = memoize((cowInventory, id) =>
cowInventory.find(cow => id === cow.id)
export const findCowById = memoize(
/**
* @param {Array.<farmhand.cow>} cowInventory
* @param {string} id
* @returns {farmhand.cow|undefined}
*/
(cowInventory, id) => cowInventory.find(cow => id === cow.id)
)

/**
* @param {Object.<number>} itemsSold
* @returns {number}
*/
export const farmProductsSold = memoize(itemsSold =>
Object.entries(itemsSold).reduce(
(sum, [itemId, numberSold]) =>
sum + (isItemAFarmProduct(itemsMap[itemId]) ? numberSold : 0),
0
)
export const farmProductsSold = memoize(
/**
* @param {Record<string, number>} itemsSold
* @returns {number}
*/
itemsSold =>
Object.entries(itemsSold).reduce(
(sum, [itemId, numberSold]) =>
sum + (isItemAFarmProduct(itemsMap[itemId]) ? numberSold : 0),
0
)
)

/**
Expand Down

0 comments on commit e1e7e23

Please sign in to comment.