Socket
Socket
Sign inDemoInstall

mime-matcher

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mime-matcher - npm Package Compare versions

Comparing version 1.0.4 to 1.0.5

test/unit/specs/matcher.spec.js

5

index.d.ts
declare module "mime-matcher"
export function parse(a: string): ParsingResult
export function isValid(a: string): boolean
export function matcher(...expected: string[]): (actual: string) => boolean
export function parse(mimeType: string): ParsingResult
export function isValid(mimeType: string): boolean

@@ -6,0 +7,0 @@ interface ParsingResult {

2

package.json
{
"name": "mime-matcher",
"version": "1.0.4",
"version": "1.0.5",
"description": "Utility function checking if string is mime type from allowed range",

@@ -5,0 +5,0 @@ "main": "dist/mime-matcher.js",

[![Build Status](https://travis-ci.org/katlasik/mime-matcher.svg?branch=master)](https://travis-ci.org/katlasik/mime-matcher)
[![codecov](https://codecov.io/gh/katlasik/mime-matcher/branch/master/graph/badge.svg)](https://codecov.io/gh/katlasik/mime-matcher)
[![npm](https://img.shields.io/npm/v/mime-matcher)](https://www.npmjs.com/package/mime-matcher)

@@ -7,3 +8,3 @@ ### Motivation

MimeMatcher is very simple library for checking if mime type string is in allowed range.
You can also match mime types agains wildcards, like `*/*` or `'application/*'`
You can also match mime types against wildcards, like `*/*` or `'application/*'`

@@ -42,3 +43,3 @@ ### Instalation

You can also use multiple mime types to match agains:
You can also use multiple mime types to match against:

@@ -66,4 +67,15 @@ ````javascript

There is also function `parse` returning object containing data
You can also use higher-order function `matcher`, which accepts expected types and returns another function, which you can use for matching:
```javascript
import { matcher } from 'mime-matcher'
const m = matcher('text/*')
m('text/xml') //true
matcher('image/*', 'text/*')("image/gif") //true
```
There is also function `parse` which returns object containing data of parsed *mime-type*:
````javascript

@@ -70,0 +82,0 @@ import { parse as parseMimeType } from 'mime-matcher'

const MIME_TYPE_REGEX = /^(\*|[a-z0-9._\-]+)\/(\*|[a-z0-9._\-]+)(?:; ([a-zA-Z0-9._\-=]+))?$/
function createMatcher(expected) {
if (expected === '*') {
return () => true
} else {
return (actual) => actual === expected
}
if (expected === '*') {
return () => true
} else {
return (actual) => actual === expected
}
}
function parse(mimeType) {
if(!mimeType) {
return {
valid: false
if (!mimeType) {
return {
valid: false
}
}
}
const match = mimeType.match(MIME_TYPE_REGEX)
const valid = Boolean(match)
if(!valid) {
const match = mimeType.match(MIME_TYPE_REGEX)
const valid = Boolean(match)
if (!valid) {
return {
valid: false
}
}
const [, type, subType, parameter] = Array.from(match)
return {
valid: false
valid,
type,
subType,
parameter
}
}
const [,type, subType, parameter] = Array.from(match)
return {
valid,
type,
subType,
parameter
}
}
function isValid(mimeType) {
return parse(mimeType).valid
return parse(mimeType).valid
}
function matcher(...expected) {
const m = new MimeMatcher(...expected)
return actual => m.match(actual)
}
class MimeMatcher {
expected = []
constructor(...expected) {
expected.forEach(mimeType => {
const {valid, type, subType} = parse(mimeType)
if (valid) {
this.expected.push({
typeMatcher: createMatcher(type),
subTypeMatcher: createMatcher(subType)
expected = []
constructor(...expected) {
this.expected = expected.map(mimeType => {
const {valid, type, subType} = parse(mimeType)
if (valid) {
return ({
typeMatcher: createMatcher(type),
subTypeMatcher: createMatcher(subType)
})
} else {
const msg = `Value "${mimeType}" is not valid mime type.It should have format "type/subtype".`
throw new TypeError(msg)
}
})
} else {
const msg = `Value "${mimeType}" is not valid mime type.It should have format "type/subtype".`
throw new TypeError(msg)
}
})
}
match(actual) {
const {valid, type, subType} = parse(actual)
if (valid) {
return this.expected.some(({ typeMatcher, subTypeMatcher }) => {
return typeMatcher(type) && subTypeMatcher(subType)
})
} else {
return false
}
}
match(actual) {
const {valid, type, subType} = parse(actual)
if (valid) {
return this.expected.some(({typeMatcher, subTypeMatcher}) => {
return typeMatcher(type) && subTypeMatcher(subType)
})
} else {
return false
}
}
}
export { isValid, parse }
export {isValid, parse, matcher}
export default MimeMatcher
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc