@@ -204,34 +204,42 @@ export const baseName = (path: string): string => path.slice(path.replace(/\\/g,
204204export 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/**
0 commit comments