Skip to content

Commit 7d39b3d

Browse files
committed
fix(play): an address is matched to its path by the rule files are matched by (#94)
The rule moved behind both as matchNamed: by name, then a single one of a kind whatever it is called. An address was still matched by the older, narrower one: only where one path and one address were left in all.
1 parent 5322749 commit 7d39b3d

2 files changed

Lines changed: 34 additions & 34 deletions

File tree

‎src/h5p/build/build-source.ts‎

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -204,34 +204,42 @@ export const baseName = (path: string): string => path.slice(path.replace(/\\/g,
204204
export const kindOf = (name: string): string => name.slice(name.lastIndexOf('.') + 1).toLowerCase()
205205

206206
/**
207-
* The files to build with: each path the source names, matched to a chosen file by its name,
208-
* keyed by the path as the source writes it. A chat writes a file's name its own way, and the
209-
* person's file is called what their camera or the chat called it: so where one path of a kind is
210-
* left without its file, and one chosen file of that kind without a path, that is its file. More of
211-
* a kind on either side are left, since nothing says which is which. `unused` are the chosen files
212-
* no path took.
207+
* Each path the source names, matched to one of the things given for it under a name: a file
208+
* chosen, or an address a chat holds. By name first. A chat writes a file's name its own way, and
209+
* the person's file is called what their camera or the chat called it: so where one path of a kind
210+
* is left without its match, and one given thing of that kind without a path, that is its match.
211+
* More of a kind on either side are left, since nothing says which is which. `unused` are the
212+
* given things no path took.
213213
*/
214-
export function matchFiles(paths: string[], chosen: File[]): { files: Record<string, File>, missing: string[], unused: File[] } {
215-
const byName = new Map(chosen.map((file) => [file.name, file]))
216-
const files: Record<string, File> = {}
214+
export function matchNamed<T>(paths: string[], given: (readonly [name: string, thing: T])[]): { matched: Record<string, T>, missing: string[], unused: T[] } {
215+
const byName = new Map(given)
216+
const matched: Record<string, T> = {}
217217
const unnamed: string[] = []
218+
const taken = new Set<string>()
218219
for (const path of paths) {
219-
const file = byName.get(baseName(path))
220-
if (file) files[path] = file
221-
else unnamed.push(path)
220+
const name = baseName(path)
221+
if (byName.has(name)) {
222+
matched[path] = byName.get(name)!
223+
taken.add(name)
224+
} else unnamed.push(path)
222225
}
223-
const taken = new Set(Object.values(files))
224-
const left = chosen.filter((file) => !taken.has(file))
226+
const left = [...byName.keys()].filter((name) => !taken.has(name))
225227
const missing: string[] = []
226228
for (const path of unnamed) {
227-
const fits = left.filter((file) => kindOf(file.name) === kindOf(path))
228-
const [file] = fits
229-
if (file && fits.length === 1 && unnamed.filter((other) => kindOf(other) === kindOf(path)).length === 1) {
230-
files[path] = file
231-
taken.add(file)
229+
const fits = left.filter((name) => kindOf(name) === kindOf(path))
230+
const [name] = fits
231+
if (name !== undefined && fits.length === 1 && unnamed.filter((other) => kindOf(other) === kindOf(path)).length === 1) {
232+
matched[path] = byName.get(name)!
233+
taken.add(name)
232234
} else missing.push(path)
233235
}
234-
return { files, missing, unused: chosen.filter((file) => !taken.has(file)) }
236+
return { matched, missing, unused: [...byName].filter(([name]) => !taken.has(name)).map(([, thing]) => thing) }
237+
}
238+
239+
/** The files to build with, keyed by the path as the source writes it: `matchNamed`, of the chosen files. */
240+
export function matchFiles(paths: string[], chosen: File[]): { files: Record<string, File>, missing: string[], unused: File[] } {
241+
const { matched, missing, unused } = matchNamed(paths, chosen.map((file) => [file.name, file] as const))
242+
return { files: matched, missing, unused }
235243
}
236244

237245
/**

‎src/h5p/play/play-page.ts‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import '@missing-elements/h5p-offline-player'
22
import { buildPackage, openPack, SourceError } from '@missing-elements/h5p-author/browser'
33
import { pack as installPack } from '@missing-elements/h5p-libraries/install'
44
import { runtime } from '@missing-elements/h5p-runtime'
5-
import { baseName, kindOf, matchFiles, namedFiles, packageName, parseSource, sized, sourceFromFragment, type Problem } from '../build/build-source'
5+
import { baseName, kindOf, matchFiles, matchNamed, namedFiles, packageName, parseSource, sized, sourceFromFragment, type Problem } from '../build/build-source'
66

77
/**
88
* The play page, `/h5p/play/`: the activity in a link's `#source=` built in the browser, as the
@@ -160,21 +160,13 @@ const filesFor = (paths: string[]): { files: Record<string, File>, missing: stri
160160
const playable = (name: string): boolean => called(name) === 'video' || called(name) === 'recording'
161161

162162
/**
163-
* The addresses for the paths still missing, matched as files are: by name, or where one path and
164-
* one address are left, by kind. A picture is never played from an address: its size is read.
163+
* The addresses for the paths still missing, matched as files are, by the same rule: by name, then
164+
* a single one of a kind whatever it is called. A picture is never played from an address: its
165+
* size is read.
165166
*/
166167
function linksFor(paths: string[]): { linked: Record<string, string>, missing: string[] } {
167-
const given = [...links]
168-
const linked: Record<string, string> = {}
169-
const missing: string[] = []
170-
for (const path of paths) {
171-
const [only] = given
172-
const address = !playable(path) ? undefined
173-
: links.get(baseName(path)) ?? (paths.length === 1 && given.length === 1 && only && kind(only[0]) === kind(path) ? only[1] : undefined)
174-
if (address) linked[path] = address
175-
else missing.push(path)
176-
}
177-
return { linked, missing }
168+
const { matched } = matchNamed(paths.filter(playable), [...links])
169+
return { linked: matched, missing: paths.filter((path) => !(path in matched)) }
178170
}
179171

180172
/** Something said to a screen reader once, beside the state's own words. */

0 commit comments

Comments
 (0)