Skip to content

Commit 47f69bd

Browse files
committed
feat(list): add parser for list-based fields
1 parent 35e0839 commit 47f69bd

File tree

6 files changed

+123
-1
lines changed

6 files changed

+123
-1
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,34 @@ assert(response.headers.get(header), value);
542542
assert(init !== response);
543543
```
544544

545+
## Lists
546+
547+
Compliant with
548+
[RFC 9110, 5.6.1. Lists](https://www.rfc-editor.org/rfc/rfc9110.html#section-5.6.1).
549+
550+
### parseListFields
551+
552+
Parse
553+
[list-based fields](https://www.rfc-editor.org/rfc/rfc9110.html#section-5.5-7)
554+
into array.
555+
556+
Strings enclosed in double quotes are safely handled.
557+
558+
```ts
559+
import { parseListFields } from "https://deno.land/x/http_utils@$VERSION/list.ts";
560+
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
561+
562+
assert(parseListFields("foo , ,bar,charlie"), [
563+
"foo",
564+
"bar",
565+
"charlie",
566+
]);
567+
assert(parseListFields(`"Sat, 04 May 1996", "Wed, 14 Sep 2005"`), [
568+
`"Sat, 04 May 1996"`,
569+
`"Wed, 14 Sep 2005"`,
570+
]);
571+
```
572+
545573
## Tokens
546574

547575
Compliant with

_tools/meta.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,10 @@ export const makeOptions = (version: string): BuildOptions => ({
107107
version: "1.1.1",
108108
subPath: "is_null",
109109
},
110+
"https://deno.land/x/[email protected]/trim.ts": {
111+
name: "@miyauci/prelude",
112+
version: "1.2.0",
113+
subPath: "trim.js",
114+
},
110115
},
111116
});

deno.lock

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
// This module is browser compatible.
33

44
export { isNull } from "https://deno.land/x/[email protected]/is_null.ts";
5+
export { trim } from "https://deno.land/x/[email protected]/trim.ts";

list.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
2+
// This module is browser compatible.
3+
4+
import { trim } from "./deps.ts";
5+
6+
/** Parse [list-based fields](https://www.rfc-editor.org/rfc/rfc9110.html#section-5.5-7) into array.
7+
* Strings enclosed in double quotes are safely handled.
8+
*
9+
* @example
10+
* ```ts
11+
* import { parseListFields } from "https://deno.land/x/http_utils@$VERSION/list.ts";
12+
* import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
13+
*
14+
* assert(parseListFields("foo , ,bar,charlie"), [
15+
* "foo",
16+
* "bar",
17+
* "charlie",
18+
* ]);
19+
* assert(parseListFields(`"Sat, 04 May 1996", "Wed, 14 Sep 2005"`), [
20+
* `"Sat, 04 May 1996"`,
21+
* `"Wed, 14 Sep 2005"`,
22+
* ]);
23+
* ```
24+
*/
25+
export function parseListFields(input: string): string[] {
26+
return input
27+
.split(/,(?=(?:(?:[^"]*?"){2})*?[^"]*?$)/)
28+
.map(trim)
29+
.filter(Boolean);
30+
}

list_test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { parseListFields } from "./list.ts";
2+
import { assertEquals, describe, it } from "./_dev_deps.ts";
3+
4+
describe("parseListFields", () => {
5+
it("should ", () => {
6+
const table: [string, string[]][] = [
7+
["", []],
8+
["a", ["a"]],
9+
["a, b, c", ["a", "b", "c"]],
10+
[" a, a , a ", ["a", "a", "a"]],
11+
[`"", `, [`""`]],
12+
[`","`, [`","`]],
13+
[`",", `, [`","`]],
14+
[`"," ,`, [`","`]],
15+
[`",,," ,`, [`",,,"`]],
16+
[`"," , " , "`, [`","`, `" , "`]],
17+
[`","abc,`, [`","abc`]],
18+
["a, b,,,,", ["a", "b"]],
19+
["a, b, c", ["a", "b", "c"]],
20+
["a,b,c", ["a", "b", "c"]],
21+
["a,,c", ["a", "c"]],
22+
[",,,", []],
23+
[`"a,b"`, [`"a,b"`]],
24+
[`"a,b",c, "d,e", f f , g `, [`"a,b"`, "c", `"d,e"`, "f f", "g"]],
25+
[` complex,pattern,"abc", "abc , def", ,,,, """, , ",""`, [
26+
"complex",
27+
"pattern",
28+
`"abc"`,
29+
`"abc , def"`,
30+
`""", , "`,
31+
`""`,
32+
]],
33+
[`",","`, [`"`, `","`]],
34+
[`",",","`, [`","`, `","`]],
35+
[`",",",",`, [`","`, `","`]],
36+
[`",",",","`, [`"`, `","`, `","`]],
37+
[`",",",",",`, [`"`, `","`, `","`]],
38+
[`",",",",","`, [`","`, `","`, `","`]],
39+
[`"`, [`"`]],
40+
[`""`, [`""`]],
41+
[`"""`, [`"""`]],
42+
[`""""`, [`""""`]],
43+
[`"",""`, [`""`, `""`]],
44+
[`",",`, [`","`]],
45+
[`"abc,def", "efg, hij", "lmn, opq"`, [
46+
'"abc,def"',
47+
'"efg, hij"',
48+
'"lmn, opq"',
49+
]],
50+
];
51+
52+
table.forEach(([input, expected]) => {
53+
assertEquals(parseListFields(input), expected);
54+
});
55+
});
56+
});

0 commit comments

Comments
 (0)