-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringify.ts
More file actions
28 lines (25 loc) · 898 Bytes
/
stringify.ts
File metadata and controls
28 lines (25 loc) · 898 Bytes
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
import { isString, isToken } from "./deps.ts";
import { Msg } from "./constants.ts";
/** Serialize string of array into string.
*
* @example
* ```ts
* import { stringifyAcceptRanges } from "https://deno.land/x/accept_ranges_parser@$VERSION/stringify.ts";
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
*
* assertEquals(stringifyAcceptRanges(["bytes"]), "bytes");
* ```
*
* @throws {TypeError} If the input is invalid [`<range-unit>`](https://www.rfc-editor.org/rfc/rfc9110#section-14.1-3) format.
*/
export function stringifyAcceptRanges(
acceptRanges: string | readonly [string, ...string[]],
): string {
const targets = isString(acceptRanges) ? [acceptRanges] : acceptRanges;
targets.forEach((rangeUnit) => {
if (!isToken(rangeUnit)) {
throw TypeError(`${Msg.InvalidRangeUnit} "${rangeUnit}"`);
}
});
return targets.join(", ");
}