Created
February 28, 2017 11:05
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
@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