Giter VIP home page Giter VIP logo

set-cookie-parser's Introduction

@sec-ant/set-cookie-parser

Yet another Set-Cookie parser.

Install

npm i @sec-ant/set-cookie-parser

Usage

This package provides three main functions: parse, parseString, and splitCookiesString.

parse

The parse function takes a string or an array of strings representing cookies and an options object of type ParseOptions. It returns an array of objects representing the parsed cookies if the map property of the options object is false or not provided. If the map property is true, it returns an object with the cookie names as keys and the parsed cookies as values.

import { parse, ParseOptions } from "@sec-ant/set-cookie-parser";

const cookies = "name=value; expires=Wed, 21 Oct 2021 07:28:00 GMT; secure";
const options: ParseOptions = {
  decodeValues: true,
  map: true,
};
const parsedCookies = parse(cookies, options);
console.log(parsedCookies);

parseString

The parseString function takes a string representing a single cookie and an optional configuration object of type ParseStringOptions. It returns an object representing the parsed cookie.

import { parseString, ParseStringOptions } from "@sec-ant/set-cookie-parser";

const cookie = "name=value; expires=Wed, 21 Oct 2021 07:28:00 GMT; secure";
const options: ParseStringOptions = {
  decodeValues: true,
};
const parsedCookie = parseString(cookie, options);
console.log(parsedCookie);

splitCookiesString

The splitCookiesString function takes a string representing multiple cookies separated by commas. It returns an array of strings representing the individual cookies.

import { splitCookiesString } from "@sec-ant/set-cookie-parser";

const cookies =
  "name1=value1; expires=Wed, 21 Oct 2021 07:28:00 GMT; secure, name2=value2; expires=Wed, 21 Oct 2021 07:28:00 GMT; secure";
const splitCookies = splitCookiesString(cookies);
console.log(splitCookies);

Configuration

The parse and parseString functions accept an optional configuration object as the second parameter. The configuration object can have the following properties:

  • decodeValues: A boolean indicating whether to decode the cookie values using decodeURIComponent. Defaults to true.
  • map: A boolean indicating whether to return an object with the cookie names as keys and the parsed cookies as values. Only applicable to the parse function. Defaults to false.

License

MIT

set-cookie-parser's People

Contributors

dependabot[bot] avatar sec-ant avatar semantic-release-bot avatar

Stargazers

 avatar  avatar

Watchers

 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.