-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringify.ts
More file actions
106 lines (90 loc) · 2.6 KB
/
Copy pathstringify.ts
File metadata and controls
106 lines (90 loc) · 2.6 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
// This module is browser compatible.
import {
assertRangeUnitFormat,
isIntRange,
isOtherRange,
isValidIntRange,
isValidOtherRange,
isValidSuffixRange,
} from "./validate.ts";
import { isNumber } from "./deps.ts";
import type {
IntRange,
OtherRange,
Range,
RangeSet,
RangeSpec,
SuffixRange,
} from "./types.ts";
/** Serializes {@link Range} into string.
*
* @example
* ```ts
* import { stringifyRange } from "https://deno.land/x/range_parser@$VERSION/stringify.ts";
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
*
* assertEquals(
* stringifyRange({
* rangeUnit: "bytes",
* rangeSet: [{ firstPos: 0, lastPos: 100 }, { suffixLength: 200 }],
* }),
* "bytes=0-100, -200",
* );
* ```
*
* @throws {TypeError} If the {@link Range} is invalid.
*/
export function stringifyRange(range: Range): string {
assertRangeUnitFormat(range.rangeUnit);
const rangeUnit = range.rangeUnit;
const rangeSet = stringifyRangeSet(range.rangeSet);
return `${rangeUnit}=${rangeSet}`;
}
/**
* @deprecated Rename to {@link stringifyRange}
*/
export const stringify = stringifyRange;
/**
* @throws {TypeError} If the {@link RangeSet} is invalid.
*/
export function stringifyRangeSet(rangeSet: RangeSet): string {
const rangeSetValue = rangeSet.map(stringifyRangeSpec).join(", ");
return rangeSetValue;
}
/**
* @throws {TypeError} If the {@link RangeSpec} is invalid.
*/
export function stringifyRangeSpec(rangeSpec: RangeSpec): string {
if (isIntRange(rangeSpec)) return stringifyIntRange(rangeSpec);
if (isOtherRange(rangeSpec)) return stringifyOtherRange(rangeSpec);
return stringifySuffixRange(rangeSpec);
}
/**
* @throws {TypeError} If the {@link IntRange} is invalid.
*/
export function stringifyIntRange(intRange: IntRange): string {
if (!isValidIntRange(intRange)) {
throw TypeError("<int-range> is invalid.");
}
const lastPos = isNumber(intRange.lastPos) ? intRange.lastPos : "";
return `${intRange.firstPos}-${lastPos}`;
}
/**
* @throws {TypeError} If the {@link OtherRange} is invalid.
*/
export function stringifyOtherRange(otherRange: OtherRange): string {
if (!isValidOtherRange(otherRange)) {
throw TypeError("<other-range> is invalid.");
}
return otherRange;
}
/**
* @throws {TypeError} If the {@link SuffixRange} is invalid.
*/
export function stringifySuffixRange(suffixRange: SuffixRange): string {
if (!isValidSuffixRange(suffixRange)) {
throw TypeError("<suffix-range> is invalid.");
}
return `-${suffixRange.suffixLength}`;
}