<commit_before>import cookie from 'cookie';
import objectAssign from 'object-assign';
import { isNode } from './utils';

export default class Cookies {
  constructor(cookies, hooks) {
    if (isNode()) {
      if (typeof cookies === 'string') {
        this.cookies = cookie.parse(cookies);
      } else if (typeof cookies === 'object') {
        this.cookies = cookies;
      } else {
        throw new Error('Missing the cookie header or object');
      }

      this.hooks = hooks;
    } else if (cookies) {
      throw new Error('The browser should not provide the cookies');
    }
  }

  get(name, options = {}) {
    const values = this.cookies || cookie.parse(document.cookie);
    return readCookie(values[name], options);
  }

  getAll(options = {}) {
    const values = this.cookies || cookie.parse(document.cookie);
    const result = {};

    for (let name in values) {
      result[name] = readCookie(values[name], options);
    }

    return result;
  }

  set(name, value, options) {
    if (typeof value === 'object') {
      value = JSON.stringify(value);
    }

    if (isNode()) {
      this.cookies[name] = value;

      if (this.hooks && this.hooks.onSet) {
        this.hooks.onSet(name, value, options);
      }
    } else {
      document.cookie = cookie.serialize(name, value, options);
    }
  }

  remove(name, options) {
    const finalOptions = (options = objectAssign({}, options, {
      expires: new Date(1970, 1, 1, 0, 0, 1),
      maxAge: 0
    }));

    if (isNode()) {
      delete this.cookies[name];

      if (this.hooks && this.hooks.onSet) {
        this.hooks.onRemove(name, finalOptions);
      }
    } else {
      document.cookie = cookie.serialize(name, '', finalOptions);
    }
  }
}

function isParsingCookie(value, doNotParse) {
  if (typeof doNotParse === 'undefined') {
    // We guess if the cookie start with { or [, it has been serialized
    doNotParse = !value || (value[0] !== '{' && value[0] !== '[');
  }

  return !doNotParse;
}

function readCookie(value, options) {
  if (isParsingCookie(value, options.doNotParse)) {
    try {
      return JSON.parse(value);
    } catch (e) {
      // At least we tried
    }
  }
<commit_msg>Always run onSet and onRemove regardless of environment<commit_after>import cookie from 'cookie';
import objectAssign from 'object-assign';
import { isNode } from './utils';

export default class Cookies {
  constructor(cookies, hooks) {
    if (isNode()) {
      if (typeof cookies === 'string') {
        this.cookies = cookie.parse(cookies);
      } else if (typeof cookies === 'object') {
        this.cookies = cookies;
      } else {
        throw new Error('Missing the cookie header or object');
      }
    } else if (cookies) {
      throw new Error('The browser should not provide the cookies');
    }

    this.hooks = hooks;
  }

  get(name, options = {}) {
    const values = this.cookies || cookie.parse(document.cookie);
    return readCookie(values[name], options);
  }

  getAll(options = {}) {
    const values = this.cookies || cookie.parse(document.cookie);
    const result = {};

    for (let name in values) {
      result[name] = readCookie(values[name], options);
    }

    return result;
  }

  set(name, value, options) {
    if (typeof value === 'object') {
      value = JSON.stringify(value);
    }

    if (this.hooks && this.hooks.onSet) {
      this.hooks.onSet(name, value, options);
    }

    if (isNode()) {
      this.cookies[name] = value;
    } else {
      document.cookie = cookie.serialize(name, value, options);
    }
  }

  remove(name, options) {
    const finalOptions = (options = objectAssign({}, options, {
      expires: new Date(1970, 1, 1, 0, 0, 1),
      maxAge: 0
    }));

    if (this.hooks && this.hooks.onRemove) {
      this.hooks.onRemove(name, finalOptions);
    }

    if (isNode()) {
      delete this.cookies[name];
    } else {
      document.cookie = cookie.serialize(name, '', finalOptions);
    }
  }
}

function isParsingCookie(value, doNotParse) {
  if (typeof doNotParse === 'undefined') {
    // We guess if the cookie start with { or [, it has been serialized
    doNotParse = !value || (value[0] !== '{' && value[0] !== '[');
  }

  return !doNotParse;
}

function readCookie(value, options) {
  if (isParsingCookie(value, options.doNotParse)) {
    try {
      return JSON.parse(value);
    } catch (e) {
      // At least we tried
    }
  }

{'fraction_non_alphanumeric': 0.14742572011794058, 'fraction_numerical': 0.005443411204354729, 'mean_word_length': 2.644628099173554, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5701701', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1383, 'n_words': 359}