Giter Site home page Giter Site logo

httpland / etag-parser Goto Github PK

View Code? Open in Web Editor NEW
1.0 0.0 0.0 36 KB

HTTP ETag header field parser

Home Page: https://deno.land/x/etag_parser

License: MIT License

TypeScript 100.00%
deserialize entity-tag etag header http parse parser serialize stringify weak

etag-parser's Introduction

etag-parser

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

test NPM

HTTP ETag header field parser.

Compliant with RFC 9110, 8.8.3. ETag.

Deserialization

Parses string into ETag.

import { parseETag } from "https://deno.land/x/etag_parser@$VERSION/parse.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

assertEquals(parseETag(`W/"123456789"`), { tag: "123456789", weak: true });
assertEquals(parseETag(`"123456789"`), { tag: "123456789", weak: false });

Throwing error

Throws SyntaxError if the input is invalid <entity-tag>.

import { parseETag } from "https://deno.land/x/etag_parser@$VERSION/parse.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";

assertThrows(() => parseETag("<invalid>"));

Serialization

Serialize ETag into string.

import { stringifyETag } from "https://deno.land/x/etag_parser@$VERSION/stringify.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

assertEquals(stringifyETag({ weak: true, tag: "123456789" }), `W/"123456789"`);
assertEquals(stringifyETag({ weak: false, tag: "123456789" }), `"123456789"`);

Throwing error

Throws TypeError if ETag contains invalid value.

import { stringifyETag } from "https://deno.land/x/etag_parser@$VERSION/stringify.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";

assertThrows(() => stringifyETag({ tag: "aあ亜", weak: true }));

ETag

ETag is a structured object for ETag header.

Name Type Description
tag string Representation of <etagc>.
weak boolean Whether this is weak validator or not.

Utilities

Utility functions are provided.

compareWeak

Weak comparison. Two ETag are equivalent if ETag.tag match character-by-character, regardless of either or both being tagged as ETag.weak.

Compliant with RFC 9110, 8.8.3.2. Comparison.

import { compareWeak } from "https://deno.land/x/etag_parser@$VERSION/validate.ts";
import { assert } from "https://deno.land/std/testing/asserts.ts";

assert(
  compareWeak(
    { weak: true, tag: "123456789" },
    { weak: false, tag: "123456789" },
  ),
);

compareStrong

Strong comparison. Two ETag are equivalent if both are StrongETag and ETag.tag match character-by-character.

Compliant with RFC 9110, 8.8.3.2. Comparison.

import { compareWeak } from "https://deno.land/x/etag_parser@$VERSION/validate.ts";
import { assert } from "https://deno.land/std/testing/asserts.ts";

assert(
  compareWeak(
    { weak: false, tag: "123456789" },
    { weak: false, tag: "123456789" },
  ),
);

isWeakETag

Whether the ETag is WeakETag or not.

import { isWeakETag } from "https://deno.land/x/etag_parser@$VERSION/validate.ts";
import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts";

assert(isWeakETag({ weak: true, tag: "123456789" }));
assertFalse(isWeakETag({ weak: false, tag: "123456789" }));

isStrongETag

Whether the ETag is StrongETag or not.

import { isStrongETag } from "https://deno.land/x/etag_parser@$VERSION/validate.ts";
import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts";

assert(isStrongETag({ weak: false, tag: "123456789" }));
assertFalse(isStrongETag({ weak: true, tag: "123456789" }));

API

All APIs can be found in the deno doc.

License

Copyright © 2023-present httpland.

Released under the MIT license

etag-parser's People

Contributors

semantic-release-bot avatar tomokimiyauci avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.