<|start_filename|>package.json<|end_filename|>
{
  "name": "cookiejs",
  "version": "2.0.2",
  "description": "A simple, lightweight JavaScript API for handling browser cookies.",
  "homepage": "https://jaywcjlove.github.io/cookie.js/",
  "main": "dist/cookie.cjs.js",
  "module": "dist/cookie.esm.js",
  "browser": "dist/cookie.js",
  "types": "index.d.ts",
  "files": [
    "dist/*.js",
    "index.d.ts"
  ],
  "repository": {
    "type": "git",
    "url": "https://github.com/jaywcjlove/cookie.js.git"
  },
  "keywords": [
    "cookiejs",
    "cookie",
    "cookies"
  ],
  "author": "<NAME>",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/jaywcjlove/cookie.js/issues"
  },
  "scripts": {
    "prepare": "npm run build",
    "test": "tsbb test",
    "coverage": "tsbb test --coverage",
    "build": "node script/build.js",
    "watch": "node script/watch.js"
  },
  "markdown-to-html": {
    "reurls": {
      "./README-zh.md": "index.zh.html",
      "./README.md": "index.html"
    }
  },
  "devDependencies": {
    "@babel/cli": "7.15.7",
    "@babel/core": "7.15.5",
    "@babel/plugin-proposal-class-properties": "7.14.5",
    "@babel/plugin-proposal-object-rest-spread": "7.15.6",
    "@babel/plugin-transform-async-to-generator": "7.14.5",
    "@babel/preset-env": "7.15.6",
    "@rollup/plugin-babel": "5.3.0",
    "@rollup/plugin-commonjs": "20.0.0",
    "@rollup/plugin-node-resolve": "13.0.5",
    "bannerjs": "2.0.1",
    "colors-cli": "1.0.28",
    "rollup": "2.56.3",
    "rollup-plugin-terser": "7.0.2",
    "tsbb": "3.3.7",
    "zlib": "1.0.5"
  }
}


<|start_filename|>src/main.js<|end_filename|>
function getKeys(obj) {
  var names = [], name = '';
  for (name in obj) {
    names.push(name);
  }
  return names;
}

function isPlainObject(obj) {
  obj = JSON.stringify(obj);
  if (typeof obj !== 'string') {
    return false;
  }
  if (!/^\{[\s\S]*\}$/.test(obj)) {
    return false;
  }
  return true;
}

function isArray(value) { return value instanceof Array }
function toArray(value) {
  return Array.prototype.slice.call(value);
}
function Cookie(){
  if(!(this instanceof Cookie)) {
    return new Cookie()
  };
}
Cookie.prototype = {
  get: function(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');//把cookie分割成组    
    for(var i=0;i < ca.length;i++) {    
      var c = ca[i];//取得字符串    
      while (c.charAt(0)==' ') {//判断一下字符串有没有前导空格    
        c = c.substring(1,c.length);//有的话，从第二位开始取    
      }    
      //如果含有我们要的name
      if (c.indexOf(nameEQ) == 0) {
        return decodeURI(c.substring(nameEQ.length,c.length));//解码并截取我们要值    
      }    
    }
    return false;
  },
  set: function(name, value, options) {
    if (isPlainObject(name)) {
      for (var k in name) {
        this.set(k, name[k], value, options);
      }
    } else if (typeof name === 'string') {
      var opt = isPlainObject(options) ? options : { expires: options },
        expires = opt.expires !== undefined ? opt.expires : '',
          path = opt.path !== undefined ? ';path=' + opt.path : ';path=/',
          domain = opt.domain ? ';domain=' + opt.domain : '',
          secure = opt.secure ? ';secure' : '';

        //过期时间
      if (typeof expires === 'string' && expires !== '') {
        expires = new Date(expires);
      } else if (typeof expires === 'number') {
        expires = new Date(+new Date + 1000 * 60 * 60 * 24 * expires);
      } if (expires !== '' && 'toGMTString' in expires) {
        expires = ';expires=' + expires.toGMTString();
      }
      document.cookie = name+"="+encodeURI(value)+expires+path+domain+secure;   //转码并赋值    
    }
  },
  remove: function(names) {
    names = isArray(names) ? names : toArray(arguments);
    for (var i = 0, l = names.length; i < l; i++) {
      this.set(names[i], '', -1);
    }
    return names;  
  },
  clear: function(name) {
    return name ? this.remove(name) : this.remove(getKeys(this.all()));
  },
  all: function() {
    if (document.cookie === '') return {};
    var cookies = document.cookie.split('; '),result = {};
    for (var i = 0, l = cookies.length; i < l; i++) {
      var item = cookies[i].split('=');
      result[decodeURI(item[0])] = decodeURI(item[1]);
    }
    return result;
  }
};

let _Cookie = null;

const cookie = function(name, value, options) {
  const argm = arguments;
  if (!_Cookie) _Cookie = Cookie();
  if (argm.length === 0) return _Cookie.all();
  if (argm.length === 1 && name === null) return _Cookie.clear();
  if (argm.length === 2 && !value) return _Cookie.clear(name);
  if (typeof name == 'string' && !value) return _Cookie.get(name);
  if ((typeof name === 'string' && value) || isPlainObject(name)) {
    return _Cookie.set(name, value, options);
  }
};
for (const a in Cookie.prototype) cookie[a] = Cookie.prototype[a];

export default cookie;


<|start_filename|>test/cookie.test.js<|end_filename|>
import cookie from '../src/main';

test('Set cookie', () => {
  expect(cookie()).toEqual({});
  expect(cookie("test1", "value1", 1800)).toBeUndefined();
  expect(cookie("test2", "value2")).toBeUndefined();
  expect(cookie.set({ "test3": "value3", "test4": "value4" })).toBeUndefined();
  expect(cookie("test5", "value5", { 'expires': 30 })).toBeUndefined();
  expect(cookie({ "test6": "value6", "test7": "value7" })).toBeUndefined();
});

test('Get cookie', () => {
  expect(cookie('test1')).toBe('value1');
  expect(cookie('test2')).toBe('value2');
  expect(cookie('test3')).toBe('value3');
  expect(cookie('test4')).toBe('value4');
  expect(cookie('test5')).toBe('value5');
  expect(cookie('test6')).toBe('value6');
  expect(cookie('test7')).toBe('value7');
});

test('Get all cookie', () => {
  const json = { "test1": "value1", "test2": "value2", "test3": "value3", "test4": "value4", "test5": "value5", "test6": "value6", "test7": "value7" }
  expect(cookie()).toEqual(json);
  expect(cookie.all()).toEqual(json);
});

test('Delete one cookie', () => {
  expect(cookie('test1', null)).toEqual(["test1"]);
  expect(cookie('test1')).toBe(false);
  expect(cookie.remove('test2')).toEqual(["test2"]);

  expect(cookie.set({ "test10": "value10" })).toBeUndefined();
  expect(cookie('test10', null)).toEqual(["test10"]);
});

test('Delete multiple cookies', () => {
  expect(cookie.remove('test3', 'test4')).toEqual(["test3", "test4"]);
  expect(cookie('test3')).toBe(false);
  expect(cookie('test4')).toBe(false);
});

test('Clean all cookie', () => {
  expect(cookie.clear()).toEqual(["test5", "test6", "test7"]);
  expect(cookie('test5')).toBe(false);
  expect(cookie('test6')).toBe(false);
  expect(cookie('test7')).toBe(false);

  expect(cookie.set({ 'test3': 'value3', 'test4': 'value4' })).toBeUndefined();
  expect(cookie(null)).toEqual(['test3', 'test4']);
  expect(cookie('test1')).toBe(false);
});

test('Set cookie expires', () => {
  expect(cookie('test5', 'value5', { 'expires': '2023-08-13', path: '/' })).toBeUndefined();
  expect(cookie('test6', 'value6', { path: '/', secure: false })).toBeUndefined();
  expect(cookie(null)).toEqual(['test5', 'test6']);
  expect(cookie('test6', 'value6', { secure: true })).toBeUndefined();
  expect(cookie.get('test6')).toEqual(false);
  expect(cookie('test7', 'value7', { secure: false })).toBeUndefined();
  expect(cookie('test8', 'value8', { expires: 0.1 , domain: 'subdomain.website.com' })).toBeUndefined();
  expect(cookie()).toEqual({ "test7": "value7" });
  expect(cookie.clear()).toEqual(["test7"]);
});



test('Other test case', () => {
  expect(cookie.set([])).toBeUndefined();
  var map = new Map();
  map.set('a', 1);
  expect(cookie.set(map)).toBeUndefined();
  expect(cookie()).toEqual({});
});


<|start_filename|>script/build.js<|end_filename|>
const rollup = require('rollup');
const babel = require('@rollup/plugin-babel');
const nodeResolve = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');
const { terser } = require('rollup-plugin-terser');
const banner = require('bannerjs');
const zlib = require('zlib');
require('colors-cli/toxic');

// see below for details on the options
const inputOptions = {
  input: 'src/main.js',
  plugins: [
    nodeResolve.default(), // so Rollup can find `ms`
    commonjs(), // so Rollup can convert `ms` to an ES module
    babel.default({
      babelHelpers: 'bundled'
      // exclude: 'node_modules/**', // 只编译我们的源代码
    }),
  ],
};

(async () => {
  const bundle = await rollup.rollup(inputOptions);
  const umd = await bundle.write({
    file: 'dist/cookie.js',
    format: 'umd',
    name: 'cookie',
    banner: banner.multibanner(),
  });
  report(umd, 'dist/store.js');

  const iife = await bundle.write({
    file: 'dist/cookie.min.js',
    format: 'umd',
    name: 'cookie',
    banner: banner.multibanner(),
    plugins: [terser()]
  });
  report(iife, 'dist/store.min.js');

  const esm = await bundle.write({
    file: 'dist/cookie.esm.js',
    format: 'esm',
    name: 'cookie',
    banner: banner.multibanner(),
  });
  report(esm, 'dist/store.esm.js');

  const cjs = await bundle.write({
    file: 'dist/cookie.cjs.js',
    format: 'cjs',
    name: 'cookie',
    exports: 'default',
    banner: banner.multibanner(),
  });
  report(cjs, 'dist/store.cjs.js');

})();

function report(result, outpath, extra) {
  const code = result.output[0].code;
  zlib.gzip(code, (_err, zipped) => {
    if (_err) return reject(_err);
    extra = `(gzipped: ${getSize(zipped).green_bt})`;
    console.log(`${(outpath).blue_bt} ${getSize(code).green_bt + (extra || '')}`);
  });
}

function getSize(code) {
  return `${(code.length / 1024).toFixed(2)}kb`;
}
