Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
* Have better typing on `getByDocId` result
* Avoid extra translation lookup
* remove ts-ignore

Co-authored-by: Evan Hahn <[email protected]>
  • Loading branch information
tomasciccola and EvanHahn authored May 3, 2024
1 parent 8c45d22 commit 333ba41
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/datatype/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ export class DataType extends TypedEmitter {
*/
async getByDocId(docId, { lang } = {}) {
await this.#dataStore.indexer.idle()
const result = /** @type {MapeoDoc} */ (this.#sql.getByDocId.get({ docId }))
const result = /** @type {undefined | MapeoDoc} */ (
this.#sql.getByDocId.get({ docId })
)
if (!result) throw new NotFoundError()
return this.#translate(deNullify(result), { lang })
}
Expand Down Expand Up @@ -215,10 +217,10 @@ export class DataType extends TypedEmitter {
let translations = await this.#translation.get(value)
// if passing a region code returns no matches,
// fallback to matching only languageCode
if (translations.length === 0) {
if (translations.length === 0 && value.regionCode) {
value.regionCode = undefined
translations = await this.#translation.get(value)
}
translations = await this.#translation.get(value)

for (let translation of translations) {
if (hasProperty(doc, translation.fieldRef)) {
Expand All @@ -238,8 +240,7 @@ export class DataType extends TypedEmitter {
rows.map(
async (doc) =>
await this.#translate(
// @ts-ignore - too complicated to type this
deNullify(doc),
deNullify(/** @type {MapeoDoc} */ (doc)),
{ lang }
)
)
Expand Down

0 comments on commit 333ba41

Please sign in to comment.