Skip to content

Commit

Permalink
iconFilenames should be Set<string>, not Set<string | undefined>
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn committed Aug 28, 2024
1 parent 0e1a8ae commit 48ed32f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
19 changes: 10 additions & 9 deletions src/config-import.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import yauzl from 'yauzl-promise'
import { validate, valueSchemas } from '@mapeo/schema'
import { json, buffer } from 'node:stream/consumers'
import { assert } from './utils.js'
import { assert, isDefined } from './utils.js'
import path from 'node:path'
import { parse as parseBCP47 } from 'bcp-47'
import { SUPPORTED_CONFIG_VERSION } from './constants.js'
Expand Down Expand Up @@ -172,15 +172,16 @@ export async function readConfig(configPath) {
return sort - nextSort
})

// check that preset references existing icon
const iconFilenames = new Set(
iconEntries.map((icon) => {
const matches = path.basename(icon.filename).match(ICON_NAME_REGEX)
if (matches) {
const [_, name] = matches
return name
}
})
iconEntries
.map((icon) => {
const matches = path.basename(icon.filename).match(ICON_NAME_REGEX)
if (matches) {
const [_, name] = matches
return name
}
})
.filter(isDefined)
)

// 5. for each preset get the corresponding fieldId and iconId, add them to the db
Expand Down
9 changes: 9 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ export function setHas(set) {
return (value) => set.has(/** @type {*} */ (value))
}

/**
* @template T
* @param {undefined | T} value
* @returns {value is T}
*/
export function isDefined(value) {
return value !== undefined
}

/**
* When reading from SQLite, any optional properties are set to `null`. This
* converts `null` back to `undefined` to match the input types (e.g. the types
Expand Down
10 changes: 1 addition & 9 deletions test-e2e/translation-api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { isDefined } from '../src/utils.js'
import { createManagers, ManagerCustodian } from './utils.js'
import { defaultConfigPath } from '../tests/helpers/default-config.js'
import {
Expand Down Expand Up @@ -387,12 +388,3 @@ test('translation api - re-loading from disk', async (t) => {

assert(hasExpectedNumberOfTranslations)
})

/**
* @template T
* @param {undefined | T} value
* @returns {value is T}
*/
function isDefined(value) {
return value !== undefined
}
7 changes: 7 additions & 0 deletions tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import assert from 'node:assert/strict'
import {
assert as utilsAssert,
ExhaustivenessError,
isDefined,
setHas,
} from '../src/utils.js'

Expand All @@ -26,6 +27,12 @@ test('ExhaustivenessError', () => {
})
})

test('isDefined()', () => {
assert(isDefined(123))
assert(isDefined(null))
assert(!isDefined(undefined))
})

test('setHas()', () => {
const set = new Set([1, 2, 3])
assert(setHas(set)(1))
Expand Down

0 comments on commit 48ed32f

Please sign in to comment.