sfv-parser
Structured Field Values for HTTP parser and serializer.
Compliant with RFC 8941, Structured Field Values for HTTP.
Parsing
Specify field value and field type(list
, dictionary
, item
) for parser.
import { parseSfv } from "https://deno.land/x/sfv_parser@$VERSION/parse.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
const result = parseSfv("sugar, tea, rum", "list");
assertEquals(result, {
"kind": "list",
"value": [
{
"kind": "item",
"value": [
{ "kind": "token", "value": "sugar" },
{ "kind": "parameters", "value": [] },
],
},
{
"kind": "item",
"value": [
{ "kind": "token", "value": "tea" },
{ "kind": "parameters", "value": [] },
],
},
{
"kind": "item",
"value": [
{ "kind": "token", "value": "rum" },
{ "kind": "parameters", "value": [] },
],
},
],
});
Syntax error
If field value has an invalid syntax, it may throw a SyntaxError
.
import { parseSfv } from "https://deno.land/x/sfv_parser@$VERSION/parse.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";
assertThrows(() => parseSfv("this, is, list", "dictionary"));
Serialization
Serialize structured field values into string.
import { stringifySfv } from "https://deno.land/x/sfv_parser@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
const sfv = {
"kind": "list",
"value": [
{
"kind": "item",
"value": [
{ "kind": "token", "value": "sugar" },
{ "kind": "parameters", "value": [] },
],
},
{
"kind": "item",
"value": [
{ "kind": "token", "value": "tea" },
{ "kind": "parameters", "value": [] },
],
},
{
"kind": "item",
"value": [
{ "kind": "token", "value": "rum" },
{ "kind": "parameters", "value": [] },
],
},
],
} as const;
assertEquals(stringifySfv(sfv), "sugar, tea, rum");
License
Copyright © 2023-present httpland.
Released under the MIT license