A list of useful ES6 JS snippets for your toolkit

Check if item exists in an array:

export const exists = array => value => array.indexOf(value) > -1

Insert item at a certain position in an array:

export const insertAt = (position, array, item) => (
  array.reduce((acc, v, idx) => idx === position ? acc.concat([item, v]) : acc.concat([v]), [])
)

Removing an element from an array:

export const removeElementFromArray = (array, element) => (array || []).filter(el => el !== element)

Setting a cookie:

export const setCookie = (name, value, days) => {
  if (typeof document === 'undefined') return

  document.cookie = `${name}=${value || ''}${(days ? formatExpireDate(daysToDate(now(), days)) : '')}; path=/`
}

Delete a cookie:

export const deleteCookie = (name, value) => {
  if (typeof document === 'undefined') return

  document.cookie = `${name}=${value || ''};expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`
}