MCPcopy Create free account
hub / github.com/tinyhttp/tinyhttp / mimeMatch

Function mimeMatch

packages/type-is/src/index.ts:22–47  ·  view source on GitHub ↗
(expected: string | boolean, actual: string | boolean)

Source from the content-addressed store, hash-verified

20}
21
22function mimeMatch(expected: string | boolean, actual: string | boolean) {
23 // invalid type
24 if (expected === false) return false
25
26 // split types
27 const actualParts = (actual as string).split('/')
28 const expectedParts = (expected as string).split('/')
29
30 // invalid format
31 if (actualParts.length !== 2 || expectedParts.length !== 2) return false
32
33 // validate type
34 if (expectedParts[0] !== '*' && expectedParts[0] !== actualParts[0]) return false
35
36 // validate suffix wildcard
37 if (expectedParts[1].substr(0, 2) === '*+')
38 return (
39 expectedParts[1].length <= actualParts[1].length + 1 &&
40 expectedParts[1].substr(1) === actualParts[1].substr(1 - expectedParts[1].length)
41 )
42
43 // validate subtype
44 if (expectedParts[1] !== '*' && expectedParts[1] !== actualParts[1]) return false
45
46 return true
47}
48
49function normalize(type: string | unknown) {
50 // invalid type

Callers 1

typeIsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected