Skip to main content
Deno 2 is finally here 🎉️
Learn more

sfv-parser

deno land deno doc GitHub release (latest by date) codecov GitHub

test NPM

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"));

License

Copyright © 2023-present httpland.

Released under the MIT license