diff --git a/src/config-import.js b/src/config-import.js index e42d61b21..afd51262e 100644 --- a/src/config-import.js +++ b/src/config-import.js @@ -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' @@ -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 diff --git a/src/utils.js b/src/utils.js index 3d0ba9823..484c0ceb3 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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 diff --git a/test-e2e/translation-api.js b/test-e2e/translation-api.js index d41b24e37..bb94b925a 100644 --- a/test-e2e/translation-api.js +++ b/test-e2e/translation-api.js @@ -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 { @@ -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 -} diff --git a/tests/utils.js b/tests/utils.js index 804ba1e2d..13f2132b5 100644 --- a/tests/utils.js +++ b/tests/utils.js @@ -3,6 +3,7 @@ import assert from 'node:assert/strict' import { assert as utilsAssert, ExhaustivenessError, + isDefined, setHas, } from '../src/utils.js' @@ -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))