<commit_before>import { CookieAttributes, Encoder } from '../types/index'

function stringifyAttributes (
  attributes: CookieAttributes & { expires?: any }
): string {
  // Copy incoming attributes as to not alter the original object..
  attributes = Object.assign({}, attributes)

  if (typeof attributes.expires === 'number') {
    attributes.expires = new Date(Date.now() + attributes.expires * 864e5)
  }
  if (attributes.expires != null) {
    attributes.expires = attributes.expires.toUTCString()
  }

  let stringifiedAttributes: string = ''
  for (const attributeName in attributes) {
    if (
      attributes[attributeName] == null ||
      attributes[attributeName] === false
    ) {
      continue
    }

    stringifiedAttributes += `; ${attributeName}`

    if (attributes[attributeName] === true) {
      continue
    }

    // Considers RFC 6265 section 5.2:
    // ...
    // 3.  If the remaining unparsed-attributes contains a %x3B (";")
    //     character:
    // Consume the characters of the unparsed-attributes up to,
    // not including, the first %x3B (";") character.
    // ...
    const attributeValue: string = attributes[attributeName].split(';')[0]
    stringifiedAttributes += `=${attributeValue}`
  }

  return stringifiedAttributes
}<commit_msg>Prepare attributes declaratively<commit_after>import { CookieAttributes, Encoder } from '../types/index'

function stringifyAttributes (
  attributes: CookieAttributes & { expires?: any }
): string {
  // Copy incoming attributes as to not alter the original object..
  attributes = Object.assign({}, attributes)

  if (typeof attributes.expires === 'number') {
    attributes.expires = new Date(Date.now() + attributes.expires * 864e5)
  }
  if (attributes.expires != null) {
    attributes.expires = attributes.expires.toUTCString()
  }

  return (
    Object.entries(attributes)
      .filter(([key, value]) => !!value)
      // Considers RFC 6265 section 5.2:
      // ...
      // 3.  If the remaining unparsed-attributes contains a %x3B (";")
      //     character:
      // Consume the characters of the unparsed-attributes up to,
      // not including, the first %x3B (";") character.
      // ...
      .map(([key, value]) =>
        value === true ? `; ${key}` : `; ${key}=${value.split(';')[0]}`
      )
      .join('')
  )
}
{'fraction_non_alphanumeric': 0.14525862068965517, 'fraction_numerical': 0.01206896551724138, 'mean_word_length': 2.9008403361344537, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1909771', 'n_tokens_mistral': 691, 'n_tokens_neox': 666, 'n_words': 176}