Skip to content

Instantly share code, notes, and snippets.

@automagisch
Created February 28, 2017 11:05
Show Gist options
  • Save automagisch/a083a0096f9e365f1b1f55af3a226ed2 to your computer and use it in GitHub Desktop.
Save automagisch/a083a0096f9e365f1b1f55af3a226ed2 to your computer and use it in GitHub Desktop.
Parse a header string to a key-value format wrapped in an Array
/*
@description A function that takes in some headers as a string containing newlines.
It will split the string on its newlines, and then will split it into
a key-value pair for easy deploying into HTML.
@param headerStr [String]
@returns headers [Array]
*/
function parseHeaderString(headerStr) {
var headerLines = headerStr.split(/\r?\n/);
var headers = headerLines.map(function(item) {
return item.split(':');
}).map(function(item) {
if(item.length == 1) {
return {
name: null,
value: item[0]
}
} else {
return {
name: item[0],
value: item[1]
}
}
});
return headers;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment