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

cookie-operator

v1.2.4

Published

a javascript handler for browser cookie

Downloads

11

Readme

cookie-operator

Build Status codecov npm build

Used to manipulate cookies for JavaScript

Translations: 中文

Installation

npm install

$ npm i cookie-operator

Direct download

Download the script here and include it (unless you are packaging scripts somehow else):

<script src="/path/to/cookie_operator.min.js"></script>

Or include it via jsDelivr CDN:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cookieOperator.mini.js">

Basic Usage

Create a cookie, valid across the entire site:

cookieOperator.set('name', 'value');

Create a cookie that expires 30 days from now, valid across the entire site:

cookieOperator.set('name', 'value',{ expires:30, path:'', domain:document.domain});

Read cookie:

cookieOperator.set('name', 'value');
cookieOperator.set('name2', 'value2');

cookieOperator.get(); // => {name:"value";name2:'value2'}
cookieOperator.get('name'); // => 'value'
cookieOperator.get('otherName'); // => 'undefined'

Delete cookie:

cookieOperator.remove('name'); 

Delete cookies by keys:

cookieOperator.removeAll(['name1','name2','name3'...]); 

Delete a cookie valid to the path of the current page:

cookieOperator.set('name', 'value', { path: '' });
cookieOperator.remove('name'); // fail!
cookieOperator.remove('name', { path: '' }); // removed!

Get the current primary domain name:

// 'www.test.com'
cookieOperator.getTopDomain(); // 'test.com'
// 'name1.name2.test.com'
cookieOperator.getTopDomain(); // 'test.com'

Get the primary domain name based on the given domain name:

cookieOperator.getTopDomain('www.test2.com') // 'test2.com'

IMPORTANT! when deleting a cookie, you must pass the exact same path and domain attributes that was used to set the cookie, unless you're relying on the default attributes.

Create an instance that overrides the default value of the cookie setting :

let testAttr = {
        expires: 1,
        domain: 'test.com',
        path: '/a',
        secure: true
    };
let _cookieOperator = cookieOperator.create(testAttr)

API

API document

Cookie Attributes

The last parameter of a function cookieOperator.set(...) is an object, which has several properties that control the cookie. If you pass in these parameters, the default properties will be overwritten. For more information on the properties of cookies, please refer to Document.cookie

expires Define when the cookie will be removed.If the value is a Number, the deleted date will be the number of days after the creation time; if it is a date object, the deleted time will be the time represented by the date object.

Default: Cookie is removed when the user closes the browser.

Examples:

cookieOperator.set('name', 'value', { expires: 30 });
cookieOperator.get('name'); // => 'value'
cookieOperator.remove('name');

path

This cookie can only be obtained in the path where the cookie is set.

Default: /

Examples:

cookieOperator.set('name', 'value', { path: '' });
cookieOperator.get('name'); // => 'value'
cookieOperator.remove('name', { path: '' });

domain

A String indicating a valid domain where the cookie should be visible. The cookie will also be visible to all subdomains.

Default: document.domain

Examples:

cookieOperator.set('name', 'value', { domain: 'subdomain.site.com' });
cookieOperator.get('name'); // => undefined (need to read at 'subdomain.site.com')

secure

Either true or false, indicating if the cookie transmission requires a secure protocol (https).

Default: false

Examples:

Cookies.set('name', 'value', { secure: true });