Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iconFilenames should be Set<string>, not Set<string | undefined> #790

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading