diff --git a/media_types/_db.ts b/media_types/_db.ts index cd362f847e4f..9959993582e3 100644 --- a/media_types/_db.ts +++ b/media_types/_db.ts @@ -21,7 +21,7 @@ for (const type of Object.keys(db) as KeyOfDb[]) { continue; } - // @ts-ignore work around denoland/dnt#148 + // @ts-ignore Work around https://github.com/denoland/dnt/issues/148 extensions.set(type, exts); for (const ext of exts) { @@ -33,7 +33,7 @@ for (const type of Object.keys(db) as KeyOfDb[]) { if ( current !== "application/octet-stream" && (from > to || - // @ts-ignore work around denoland/dnt#148 + // @ts-ignore work around https://github.com/denoland/dnt/issues/148 (from === to && current.startsWith("application/"))) ) { continue; diff --git a/media_types/_util.ts b/media_types/_util.ts index 360809c96eec..d53e1c3ed6d1 100644 --- a/media_types/_util.ts +++ b/media_types/_util.ts @@ -1,11 +1,5 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -/** Supporting functions for media_types that do not make part of the public - * API. - * - * @module - * @private - */ export interface DBEntry { source: string; compressible?: boolean; diff --git a/media_types/extension.ts b/media_types/extension.ts index 1abb6d8d63ea..d1a98019cfaf 100644 --- a/media_types/extension.ts +++ b/media_types/extension.ts @@ -26,9 +26,5 @@ import { extensionsByType } from "./extensions_by_type.ts"; * ``` */ export function extension(type: string): string | undefined { - const exts = extensionsByType(type); - if (exts) { - return exts[0]; - } - return undefined; + return extensionsByType(type)?.[0]; } diff --git a/media_types/format_media_type.ts b/media_types/format_media_type.ts index 681ad2eff7a4..5e9fa0a4976e 100644 --- a/media_types/format_media_type.ts +++ b/media_types/format_media_type.ts @@ -38,18 +38,18 @@ export function formatMediaType( type: string, param?: Record | Iterable<[string, string]>, ): string { - let b = ""; + let serializedMediaType = ""; const [major = "", sub] = type.split("/"); if (!sub) { if (!isToken(type)) { return ""; } - b += type.toLowerCase(); + serializedMediaType += type.toLowerCase(); } else { if (!isToken(major) || !isToken(sub)) { return ""; } - b += `${major.toLowerCase()}/${sub.toLowerCase()}`; + serializedMediaType += `${major.toLowerCase()}/${sub.toLowerCase()}`; } if (param) { @@ -62,25 +62,25 @@ export function formatMediaType( return ""; } const value = param[attribute]!; - b += `; ${attribute.toLowerCase()}`; + serializedMediaType += `; ${attribute.toLowerCase()}`; const needEnc = needsEncoding(value); if (needEnc) { - b += "*"; + serializedMediaType += "*"; } - b += "="; + serializedMediaType += "="; if (needEnc) { - b += `utf-8''${encodeURIComponent(value)}`; + serializedMediaType += `utf-8''${encodeURIComponent(value)}`; continue; } if (isToken(value)) { - b += value; + serializedMediaType += value; continue; } - b += `"${value.replace(/["\\]/gi, (m) => `\\${m}`)}"`; + serializedMediaType += `"${value.replace(/["\\]/gi, (m) => `\\${m}`)}"`; } } - return b; + return serializedMediaType; } diff --git a/media_types/get_charset.ts b/media_types/get_charset.ts index 3a1706e9bf0c..0bfdd7e41aab 100644 --- a/media_types/get_charset.ts +++ b/media_types/get_charset.ts @@ -28,11 +28,11 @@ import { db, type KeyOfDb } from "./_db.ts"; export function getCharset(type: string): string | undefined { try { const [mediaType, params] = parseMediaType(type); - if (params && params["charset"]) { - return params["charset"]; + if (params?.charset) { + return params.charset; } const entry = db[mediaType as KeyOfDb] as DBEntry; - if (entry && entry.charset) { + if (entry?.charset) { return entry.charset; } if (mediaType.startsWith("text/")) { diff --git a/media_types/parse_media_type.ts b/media_types/parse_media_type.ts index 89e3671e7242..6bd97e13965f 100644 --- a/media_types/parse_media_type.ts +++ b/media_types/parse_media_type.ts @@ -18,7 +18,7 @@ import { consumeMediaParam, decode2331Encoding } from "./_util.ts"; * params keys will be normalized to lower case, but preserves the casing of * the value. * - * @param v The media type to parse. + * @param type The media type to parse. * * @returns A tuple where the first element is the media type and the second * element is the optional parameters or `undefined` if there are none. @@ -33,9 +33,9 @@ import { consumeMediaParam, decode2331Encoding } from "./_util.ts"; * ``` */ export function parseMediaType( - v: string, + type: string, ): [mediaType: string, params: Record | undefined] { - const [base] = v.split(";") as [string]; + const [base] = type.split(";") as [string]; const mediaType = base.toLowerCase().trim(); const params: Record = {}; @@ -43,13 +43,13 @@ export function parseMediaType( // for parameters containing a '*' character. const continuation = new Map>(); - v = v.slice(base.length); - while (v.length) { - v = v.trimStart(); - if (v.length === 0) { + type = type.slice(base.length); + while (type.length) { + type = type.trimStart(); + if (type.length === 0) { break; } - const [key, value, rest] = consumeMediaParam(v); + const [key, value, rest] = consumeMediaParam(type); if (!key) { if (rest.trim() === ";") { // ignore trailing semicolons @@ -70,7 +70,7 @@ export function parseMediaType( throw new TypeError("Duplicate key parsed."); } pmap[key] = value; - v = rest; + type = rest; } // Stitch together any continuations or things with stars @@ -78,9 +78,9 @@ export function parseMediaType( let str = ""; for (const [key, pieceMap] of continuation) { const singlePartKey = `${key}*`; - const v = pieceMap[singlePartKey]; - if (v) { - const decv = decode2331Encoding(v); + const type = pieceMap[singlePartKey]; + if (type) { + const decv = decode2331Encoding(type); if (decv) { params[key] = decv; } @@ -91,25 +91,25 @@ export function parseMediaType( let valid = false; for (let n = 0;; n++) { const simplePart = `${key}*${n}`; - let v = pieceMap[simplePart]; - if (v) { + let type = pieceMap[simplePart]; + if (type) { valid = true; - str += v; + str += type; continue; } const encodedPart = `${simplePart}*`; - v = pieceMap[encodedPart]; - if (!v) { + type = pieceMap[encodedPart]; + if (!type) { break; } valid = true; if (n === 0) { - const decv = decode2331Encoding(v); + const decv = decode2331Encoding(type); if (decv) { str += decv; } } else { - const decv = decodeURI(v); + const decv = decodeURI(type); str += decv; } } @@ -118,7 +118,5 @@ export function parseMediaType( } } - return Object.keys(params).length - ? [mediaType, params] - : [mediaType, undefined]; + return [mediaType, Object.keys(params).length ? params : undefined]; } diff --git a/media_types/type_by_extension.ts b/media_types/type_by_extension.ts index 10577592b0d7..b91381f46dc5 100644 --- a/media_types/type_by_extension.ts +++ b/media_types/type_by_extension.ts @@ -28,6 +28,6 @@ import { types } from "./_db.ts"; */ export function typeByExtension(extension: string): string | undefined { extension = extension.startsWith(".") ? extension.slice(1) : extension; - // @ts-ignore Workaround around for https://github.com/denoland/dnt/issues/148 + // @ts-ignore Work around https://github.com/denoland/dnt/issues/148 return types.get(extension.toLowerCase()); }