npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@httpland/sfv-parser

v1.1.0

Published

Structured Field Values for HTTP parser and serializer

Downloads

19

Readme

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.

Documentation

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, {
  "type": "List",
  "value": [
    {
      "type": "Item",
      "value": [
        { "type": "Token", "value": "sugar" },
        { "type": "Parameters", "value": [] },
      ],
    },
    {
      "type": "Item",
      "value": [
        { "type": "Token", "value": "tea" },
        { "type": "Parameters", "value": [] },
      ],
    },
    {
      "type": "Item",
      "value": [
        { "type": "Token", "value": "rum" },
        { "type": "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 Sfv 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 = {
  "type": "List",
  "value": [
    {
      "type": "Item",
      "value": [
        { "type": "Token", "value": "sugar" },
        { "type": "Parameters", "value": [] },
      ],
    },
    {
      "type": "Item",
      "value": [
        { "type": "Token", "value": "tea" },
        { "type": "Parameters", "value": [] },
      ],
    },
    {
      "type": "Item",
      "value": [
        { "type": "Token", "value": "rum" },
        { "type": "Parameters", "value": [] },
      ],
    },
  ],
} as const;

assertEquals(stringifySfv(sfv), "sugar, tea, rum");

RangeError

The range of possible values for Integer and Decimal is specified. If this is violated, RangeError may be thrown.

For example, the possible values of type Integer range from -999,999,999,999,999 to 999,999,999,999,999.

import {
  Dictionary,
  Integer,
  Item,
  Parameters,
  stringifySfv,
} from "https://deno.land/x/sfv_parser@$VERSION/mod.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";

const sfv = new Dictionary({
  foo: new Item(
    [new Integer(10e14), new Parameters()],
  ),
});

assertThrows(() => stringifySfv(sfv));

TypeError

If Data type contains invalid characters, it may throw TypeError.

For example, Token must be compliant with <sf-token>.

import {
  Item,
  List,
  Parameters,
  stringifySfv,
  Token,
} from "https://deno.land/x/sfv_parser@$VERSION/mod.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";

const sfv = new List([
  new Item(
    [new Token("<invalid>"), new Parameters()],
  ),
]);

assertThrows(() => stringifySfv(sfv));

Type constructor

Provides a utility of type constructor for structured data types.

This saves you the trouble of typing the type field.

List

Representation of Rfc 8941, 3.1. Lists.

import {
  type InnerList,
  type Item,
  List,
} from "https://deno.land/x/sfv_parser@$VERSION/types.ts";

declare const input: (Item | InnerList)[];
const list = new List(input);

yield:

{
  "type": "List",
  "value": "<Item | InnerList>[]"
}

InnerList

Representation of Rfc 8941, 3.1.1. Inner Lists.

import {
  InnerList,
  type Item,
  type Parameters,
} from "https://deno.land/x/sfv_parser@$VERSION/types.ts";

declare const items: Item[];
declare const parameters: Parameters;
const innerList = new InnerList([items, parameters]);

or,

import {
  InnerList,
  type Item,
} from "https://deno.land/x/sfv_parser@$VERSION/types.ts";

declare const items: Item[];
const innerList = new InnerList(items);

yield:

{
  "type": "InnerList",
  "value": ["<Item>[]", "<Parameters>"]
}

Parameters

Representation of Rfc 8941, 3.1.2. Parameters.

import {
  type BareItem,
  Parameters,
} from "https://deno.land/x/sfv_parser@$VERSION/types.ts";

declare const bareItem: BareItem;
const parameters = new Parameters({
  "<key>": bareItem,
});

or,

import {
  type BareItem,
  Parameters,
} from "https://deno.land/x/sfv_parser@$VERSION/types.ts";

declare const bareItem: BareItem;
const parameters = new Parameters([
  ["<key>", bareItem],
]);

yield:

{
  "type": "Boolean",
  "value": "[string, <BareItem>][]"
}

Dictionary

Representation of Rfc 8941, 3.2. Dictionaries.

import {
  Dictionary,
  type InnerList,
  type Item,
} from "https://deno.land/x/sfv_parser@$VERSION/types.ts";

declare const input: Item | InnerList;
const dictionary = new Dictionary({ "<key>": input });

or,

import {
  Dictionary,
  type InnerList,
  type Item,
} from "https://deno.land/x/sfv_parser@$VERSION/types.ts";

declare const input: Item | InnerList;
const dictionary = new Dictionary([
  ["<key>", input],
]);

yield:

{
  "type": "Dictionary",
  "value": "[string, <Item | InnerList>][]"
}

Item

Representation of Rfc 8941, 3.3. Items.

import {
  type BareItem,
  Item,
  type Parameters,
} from "https://deno.land/x/sfv_parser@$VERSION/types.ts";

declare const bareItem: BareItem;
declare const parameters: Parameters;

const item = new Item([bareItem, parameters]);

or,

import {
  type BareItem,
  Item,
} from "https://deno.land/x/sfv_parser@$VERSION/types.ts";

declare const bareItem: BareItem;

const item = new Item(bareItem);

yield:

{
  "type": "Item",
  "value": ["<BareItem>", "<Parameters>"]
}

Integer

Representation of RFC 8941, 3.3.1. Integers.

import { Integer } from "https://deno.land/x/sfv_parser@$VERSION/types.ts";

declare const input: number;
const integer = new Integer(input);

yield:

{
  "type": "Integer",
  "value": "<number>"
}

Decimal

Representation of Rfc 8941, 3.3.2. Decimals.

import { Decimal } from "https://deno.land/x/sfv_parser@$VERSION/types.ts";

declare const input: number;
const decimal = new Decimal(input);

yield:

{
  "type": "Decimal",
  "value": "<number>"
}

String

Representation of Rfc 8941, 3.3.3. Strings.

import { String } from "https://deno.land/x/sfv_parser@$VERSION/types.ts";

declare const input: string;
const string = new String(input);

yield:

{
  "type": "String",
  "value": "<string>"
}

Token

Representation of RFC 8941, 3.3.4. Tokens.

import { Token } from "https://deno.land/x/sfv_parser@$VERSION/types.ts";

declare const input: string;
const token = new Token(input);

yield:

{
  "type": "Token",
  "value": "<string>"
}

Binary

Representation of Rfc 8941,3.3.5. Byte Sequences.

import { Binary } from "https://deno.land/x/sfv_parser@$VERSION/types.ts";

declare const input: Uint8Array;
const binary = new Binary(input);

yield:

{
  "type": "Binary",
  "value": "<Uint8Array>"
}

Boolean

Representation of Rfc 8941, 3.3.6. Booleans.

import { Boolean } from "https://deno.land/x/sfv_parser@$VERSION/types.ts";

declare const input: boolean;
const boolean = new Boolean(input);

yield:

{
  "type": "Boolean",
  "value": "<boolean>"
}

Coverage

All test cases in structured-field-tests have been passed.

All test case

API

All APIs can be found in the deno doc.

License

Copyright © 2023-present httpland.

Released under the MIT license