-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stringify): add serialization function for accept-ranges header …
…field
- Loading branch information
1 parent
0c8b01c
commit 6f22c49
Showing
8 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { isString } from "./deps.ts"; | ||
import { isTokenFormat } from "./utils.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 (!isTokenFormat(rangeUnit)) { | ||
throw TypeError(`${Msg.InvalidRangeUnit} "${rangeUnit}"`); | ||
} | ||
}); | ||
|
||
return targets.join(", "); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { stringifyAcceptRanges } from "./stringify.ts"; | ||
import { | ||
assertEquals, | ||
assertIsError, | ||
assertThrows, | ||
describe, | ||
it, | ||
} from "./_dev_deps.ts"; | ||
|
||
describe("stringifyAcceptRanges", () => { | ||
it("should return string if the input is valid string", () => { | ||
const table: string[] = ["bytes", "none"]; | ||
|
||
table.forEach((input) => { | ||
assertEquals(stringifyAcceptRanges(input), input); | ||
}); | ||
}); | ||
|
||
it("should return string if the input is valid string of array", () => { | ||
const table: [[string, ...string[]], string][] = [ | ||
[["bytes"], "bytes"], | ||
[["bytes", "unknown"], "bytes, unknown"], | ||
]; | ||
|
||
table.forEach(([input, expected]) => { | ||
assertEquals(stringifyAcceptRanges(input), expected); | ||
}); | ||
}); | ||
|
||
it("should throw error if the input is invalid string", () => { | ||
const table: string[] = [ | ||
"", | ||
" ", | ||
"bytes, none", | ||
"a ", | ||
" a", | ||
",", | ||
" ,", | ||
`"a"`, | ||
`"a, b"`, | ||
]; | ||
|
||
table.forEach((input) => { | ||
assertThrows(() => stringifyAcceptRanges(input)); | ||
}); | ||
}); | ||
|
||
it("should throw error if the array input has is invalid string", () => { | ||
const table: [string, ...string[]][] = [ | ||
[""], | ||
["", ""], | ||
["", "", "", ""], | ||
["", "ok"], | ||
["ng", ","], | ||
]; | ||
|
||
table.forEach((input) => { | ||
assertThrows(() => stringifyAcceptRanges(input)); | ||
}); | ||
}); | ||
|
||
it("should be error message", () => { | ||
let err; | ||
try { | ||
stringifyAcceptRanges(""); | ||
} catch (e) { | ||
err = e; | ||
} finally { | ||
assertIsError(err, TypeError, `invalid <range-unit> syntax. ""`); | ||
} | ||
}); | ||
}); |