deno.land / std@0.173.0 / media_types / format_media_type.ts

format_media_type.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.import { isIterator, isToken, needsEncoding } from "./_util.ts";
/** Serializes the media type and the optional parameters as a media type * conforming to RFC 2045 and RFC 2616. * * The type and parameter names are written in lower-case. * * When any of the arguments results in a standard violation then the return * value will be an empty string (`""`). * * @example * ```ts * import { formatMediaType } from "https://deno.land/std@$STD_VERSION/media_types/format_media_type.ts"; * * formatMediaType("text/plain", { charset: "UTF-8" }); // `text/plain; charset=UTF-8` * ``` */export function formatMediaType( type: string, param?: Record<string, string> | Iterable<[string, string]>,): string { let b = ""; const [major, sub] = type.split("/"); if (!sub) { if (!isToken(type)) { return ""; } b += type.toLowerCase(); } else { if (!isToken(major) || !isToken(sub)) { return ""; } b += `${major.toLowerCase()}/${sub.toLowerCase()}`; }
if (param) { param = isIterator(param) ? Object.fromEntries(param) : param; const attrs = Object.keys(param); attrs.sort();
for (const attribute of attrs) { if (!isToken(attribute)) { return ""; } const value = param[attribute]; b += `; ${attribute.toLowerCase()}`;
const needEnc = needsEncoding(value); if (needEnc) { b += "*"; } b += "=";
if (needEnc) { b += `utf-8''${encodeURIComponent(value)}`; continue; }
if (isToken(value)) { b += value; continue; } b += `"${value.replace(/["\\]/gi, (m) => `\\${m}`)}"`; } } return b;}
std

Version Info

Tagged at
a year ago