-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.ts
31 lines (26 loc) · 1.15 KB
/
parse.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
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
// This module is browser compatible.
import { isNotEmpty, isToken, parseListFields } from "./deps.ts";
import { Msg } from "./constants.ts";
import type { AcceptRanges } from "./types.ts";
/** Parses string into {@link AcceptRanges}.
*
* @example
* ```ts
* import { parseAcceptRanges } from "https://deno.land/x/accept_ranges_parser@$VERSION/parse.ts";
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
*
* assertEquals(parseAcceptRanges(`none`), ["none"]);
* assertEquals(parseAcceptRanges(`bytes, unknown`), ["bytes", "unknown"]);
* ```
* @throws {SyntaxError} If the input is invalid [`<Accept-Ranges>`](https://www.rfc-editor.org/rfc/rfc9110.html#section-14.3-2) syntax.
*/
export function parseAcceptRanges(input: string): AcceptRanges {
const acceptableRanges = parseListFields(input);
const msg = `${Msg.InvalidSyntax} "${input}"`;
if (!isNotEmpty(acceptableRanges)) throw new SyntaxError(msg);
acceptableRanges.forEach((token) => {
if (!isToken(token)) throw new SyntaxError(msg);
});
return acceptableRanges as AcceptRanges;
}