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

cookies-ts

v1.0.5

Published

A simple tool for handling browser cookies

Downloads

9,475

Readme

vue-cookies-ts

A simple tool for handling browser cookies

Installation

npm install cookies-ts --save
import Cookies from "cookies-ts"

const cookies = new Cookies()

Api

syntax format: cookies.[method]

config

Set global config

(option: CookiesOption) => void

//example

cookies.config({
    expires?: string | number | Date,
    path?: string,
})  // default: expireTimes = 1d , path=/

set

Set a cookie

(key: string, value: any, option: CookiesOption) => Cookies

//example

cookies.set(keyName: string, {
    expires?: string | number | Date,
    path?: string,
    domain?: string,
    secure?: boolean
}) 

get

Get a cookie

(key: string) => string | null | object

//example

cookies.get(keyName: string)

remove

Remove a cookie

(key: string, option: CookiesOption) => Cookies | boolean

//example

cookies.remove(keyName: string, {path: string, domain: string})

isKey

If exist a cookie name

(key: string) => boolean

//example

cookies.isKey(keyName: string)

keys

Get All cookie name

() => string[]

//example

cookies.keys()

Example Usage

Set global config

// 30 day after, expire
cookies.config({ expires: "30d" })

cookies.config({ expires: new Date(2019,03,13).toUTCString() })

// 30 day after, expire, '' current path , browser default
cookies.config({ expires: 60 * 60 * 24 * 30, path: "" })

Support json object

var user = { id:1, name:'Journal', session:'25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX' }

cookies.set('user', user)

// print user name
console.log(cookies.get('user').name)

Set expire times

Suppose the current time is : Sat, 11 Mar 2017 12:25:57 GMT

Following equivalence: 1 day after, expire

Support chaining sets together

 // default expire time: 1 day
Cookies
        .set("user_session", "25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX")
        // number + d , ignore case
        .set("user_session", "25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX", { expires: "1d" })
        .set("user_session", "25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX", { expires: "1D" })
        // Base of second
        .set("user_session", "25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX", { expires: 60 * 60 * 24 })
        // input a Date, + 1day
        .set("user_session", "25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX", { expires: new Date(2017, 03, 12) })
        // input a date string, + 1day
        .set("user_session", "25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX", { expires: "Sat, 13 Mar 2017 12:25:57 GMT" })

Set expire times, input number type

// 1 second after, expire
cookies.set("default_unit_second", "input_value", { expires: 1 })

// 1 minute 30 second after, expire
cookies.set("default_unit_second", "input_value", { expires: 60 + 30 })

// 12 hour after, expire
cookies.set("default_unit_second", "input_value", { expires: 60 * 60 * 12 })

// 1 month after, expire
cookies.set("default_unit_second", "input_value", { expires: 60 * 60 * 24 * 30 })

Set expire times - end of browser session

// end of session - use string!
cookies.set("default_unit_second", "input_value", { expires: "0" })

Set expire times , input string type

| Unit | full name | | ----------- | ----------- | | y | year | | m | month | | d | day | | h | hour | | min | minute | | s | second |

✔ caseless for unit

❌ combination not supported

❌ double value not supported

// 60 second after, expire
cookies.set("token","GH1.1.1689020474.1484362313", { expires: "60s" })

// 30 minute after, expire, ignore case
cookies.set("token","GH1.1.1689020474.1484362313", { expires: "30min" })

// 24 day after, expire
cookies.set("token","GH1.1.1689020474.1484362313", { expires: "24d" })

// 4 month after, expire
cookies.set("token","GH1.1.1689020474.1484362313", { expires: "4m" })

// 16 hour after, expire
cookies.set("token","GH1.1.1689020474.1484362313", { expires: "16h" })

// 3 year after, expire
cookies.set("token","GH1.1.1689020474.1484362313", { expires: "3y" })

// input date string 
cookies.set('token',"GH1.1.1689020474.1484362313", { expires: new Date(2017,03,13).toUTCString() })

cookies.set("token","GH1.1.1689020474.1484362313", { expires: "Sat, 13 Mar 2017 12:25:57 GMT " })

Set expire support date

var date = new Date

date.setDate(date.getDate() + 1)

cookies.set("token","GH1.1.1689020474.1484362313", { expires: date })

Set never expire

// never expire
cookies.set("token","GH1.1.1689020474.1484362313", { expires: Infinity })

// never expire , only -1,Other negative Numbers are invalid
cookies.set("token","GH1.1.1689020474.1484362313", { expires: -1 }) 

Set other arguments

// set path
cookies.set("use_path_argument","value", { expires: "1d", path: "/app" })

// set domain, default 1 day after,expire
cookies.set("use_path_argument","value", { domain: "domain.com" })

// set secure
cookies.set("use_path_argument","value", { secure: true })

Other operation

// check a cookie exist
cookies.isKey("token")

// get a cookie
cookies.get("token")

// remove a cookie
cookies.remove("token")

// get all cookie key names, line shows
cookies.keys().join("\n") 

Warning

Cookies key names Cannot be set to ['expires','max-age','path','domain','secure']

Explaination

Cookies-ts is developed from vue-cookies without dependencies

License

MIT Copyright (c) 2016-present, ztytotoro