MCPcopy Create free account
hub / github.com/denoland/std / parseLanguage

Function parseLanguage

http/_negotiation/language.ts:42–71  ·  view source on GitHub ↗
(
  str: string,
  i: number,
)

Source from the content-addressed store, hash-verified

40const SIMPLE_LANGUAGE_REGEXP = /^\s*([^\s\-;]+)(?:-([^\s;]+))?\s*(?:;(.*))?$/;
41
42function parseLanguage(
43 str: string,
44 i: number,
45): LanguageSpecificity | undefined {
46 const match = SIMPLE_LANGUAGE_REGEXP.exec(str);
47 if (!match) {
48 return undefined;
49 }
50
51 const [, prefix, suffix] = match;
52 if (!prefix) {
53 return undefined;
54 }
55
56 const full = suffix !== undefined ? `${prefix}-${suffix}` : prefix;
57
58 let q = 1;
59 if (match[3]) {
60 const params = match[3].split(";");
61 for (const param of params) {
62 const [key, value] = param.trim().split("=");
63 if (key === "q" && value) {
64 q = parseFloat(value);
65 break;
66 }
67 }
68 }
69
70 return { prefix, suffix, full, i, o: undefined, q, s: undefined };
71}
72
73function parseAcceptLanguage(accept: string): LanguageSpecificity[] {
74 const accepts = accept.split(",");

Callers 2

parseAcceptLanguageFunction · 0.85
specifyFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected