Skip to content

Instantly share code, notes, and snippets.

@plugn
Last active August 4, 2019 12:31
Show Gist options
  • Save plugn/a43110a20f7e055cf88a7dc75e73206e to your computer and use it in GitHub Desktop.
Save plugn/a43110a20f7e055cf88a7dc75e73206e to your computer and use it in GitHub Desktop.
/**
*
* @description parses http headers string
* and returns them as Array of { key: value }
*
* @headers {string}
* var headers = `Pragma: no-cache
* Cache-Control: no-cache
* Content-Type: application/json; charset=UTF-8
* Accept-Encoding: gzip, deflate, br
* Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7`
*
* @returns {array} < [headerName, headerValue] >
*/
export const parseHttpHeaders = headers => (
String(headers)
.split(/[\r\n]+/)
.filter(Boolean)
.map(header => header.split(/:\s+/))
);
export const getCookie = cookieName => (
document.cookie.split(';')
.map(s => s.trim().split('='))
.find(v => v[0] === cookieName) || []
)[1];
export const tupleToObject = tuple => ({ [String(tuple[0])]: tuple[1] });
export const collectionToMap = collection => collection.reduce((acc, v) => ({...acc, ...v}), {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment