Skip to content

Commit

Permalink
refactor(media_types): prepare for noUncheckedIndexedAccess (#4043)
Browse files Browse the repository at this point in the history
refactor(media_types): handle noUncheckedIndexedAccess for media_types
  • Loading branch information
syhol authored Dec 30, 2023
1 parent c9f4020 commit 58e6675
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
12 changes: 7 additions & 5 deletions media_types/_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ export function consumeValue(v: string): [value: string, rest: string] {
if (r === `"`) {
return [value, v.slice(i + 1)];
}
if (r === "\\" && i + 1 < v.length && isTSpecial(v[i + 1])) {
value += v[i + 1];
const next = v[i + 1];
if (r === "\\" && typeof next === "string" && isTSpecial(next)) {
value += next;
i++;
continue;
}
Expand Down Expand Up @@ -83,14 +84,15 @@ export function decode2331Encoding(v: string): string | undefined {
if (sv.length !== 3) {
return undefined;
}
const charset = sv[0].toLowerCase();
const [sv0, , sv2] = sv as [string, string, string];
const charset = sv0.toLowerCase();
if (!charset) {
return undefined;
}
if (charset !== "us-ascii" && charset !== "utf-8") {
return undefined;
}
const encv = decodeURI(sv[2]);
const encv = decodeURI(sv2);
if (!encv) {
return undefined;
}
Expand Down Expand Up @@ -133,7 +135,7 @@ function isTokenChar(r: string): boolean {
}

function isTSpecial(r: string): boolean {
return `()<>@,;:\\"/[]?=`.includes(r[0]);
return r[0] ? `()<>@,;:\\"/[]?=`.includes(r[0]) : false;
}

const CHAR_CODE_SPACE = " ".charCodeAt(0);
Expand Down
4 changes: 2 additions & 2 deletions media_types/format_media_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function formatMediaType(
param?: Record<string, string> | Iterable<[string, string]>,
): string {
let b = "";
const [major, sub] = type.split("/");
const [major = "", sub] = type.split("/");
if (!sub) {
if (!isToken(type)) {
return "";
Expand All @@ -45,7 +45,7 @@ export function formatMediaType(
if (!isToken(attribute)) {
return "";
}
const value = param[attribute];
const value = param[attribute]!;
b += `; ${attribute.toLowerCase()}`;

const needEnc = needsEncoding(value);
Expand Down
2 changes: 1 addition & 1 deletion media_types/parse_media_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { consumeMediaParam, decode2331Encoding } from "./_util.ts";
export function parseMediaType(
v: string,
): [mediaType: string, params: Record<string, string> | undefined] {
const [base] = v.split(";");
const [base] = v.split(";") as [string];
const mediaType = base.toLowerCase().trim();

const params: Record<string, string> = {};
Expand Down

0 comments on commit 58e6675

Please sign in to comment.