@vtmap/vtmap-sdk-js
Version:
JS SDK for accessing Viettelmaps APIs
44 lines (38 loc) • 955 B
JavaScript
;
function parseSingleHeader(raw) {
var boundary = raw.indexOf(':');
var name = raw
.substring(0, boundary)
.trim()
.toLowerCase();
var value = raw.substring(boundary + 1).trim();
return {
name: name,
value: value
};
}
/**
* Parse raw headers into an object with lowercase properties.
* Does not fully parse headings into more complete data structure,
* as larger libraries might do. Also does not deal with duplicate
* headers because Node doesn't seem to deal with those well, so
* we shouldn't let the browser either, for consistency.
*
* @param {string} raw
* @returns {Object}
*/
function parseHeaders(raw) {
var headers = {};
if (!raw) {
return headers;
}
raw
.trim()
.split(/[\r|\n]+/)
.forEach(function(rawHeader) {
var parsed = parseSingleHeader(rawHeader);
headers[parsed.name] = parsed.value;
});
return headers;
}
module.exports = parseHeaders;