Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(media_types): prepare for noUncheckedIndexedAccess #4043

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
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;
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
}

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];
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
const mediaType = base.toLowerCase().trim();

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