File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import { extname } from "node:path" ;
22
3- const mimeTypes : { [ key : string ] : string [ ] } = {
4- "application/ json" : [ " json"] ,
5- "application/x-xpinstall" : [ "xpi" ] ,
3+ const mimeTypes : { [ key : string ] : string } = {
4+ json : "application/ json",
5+ xpi : "application/x-xpinstall" ,
66} ;
77
88export function getMimeTypeByFileName ( filename : string ) {
99 const ext = extname ( filename ) ;
1010
1111 for ( const type in mimeTypes ) {
12- if ( mimeTypes [ type ] . includes ( ext ) )
13- return type ;
12+ if ( ext === `. ${ type } ` )
13+ return mimeTypes [ type ] ;
1414 }
1515
1616 return undefined ;
Original file line number Diff line number Diff line change 1+ import { describe , expect , it } from "vitest" ;
2+ import { getMimeTypeByFileName } from "../../src/utils/mime.js" ;
3+
4+ describe ( "mime" , ( ) => {
5+ describe ( "getMimeTypeByFileName" , ( ) => {
6+ it ( "should return application/json for json files" , ( ) => {
7+ expect ( getMimeTypeByFileName ( "test.json" ) ) . toEqual ( "application/json" ) ;
8+ } ) ;
9+
10+ it ( "should return application/x-xpinstall for xpi files" , ( ) => {
11+ expect ( getMimeTypeByFileName ( "test.xpi" ) ) . toEqual ( "application/x-xpinstall" ) ;
12+ } ) ;
13+
14+ it ( "should return undefined for unknown file types" , ( ) => {
15+ expect ( getMimeTypeByFileName ( "test.unknown" ) ) . toBeUndefined ( ) ;
16+ } ) ;
17+ } ) ;
18+ } ) ;
You can’t perform that action at this time.
0 commit comments