Created
December 13, 2012 09:03
-
-
Save azproduction/4275127 to your computer and use it in GitHub Desktop.
Accept-Language, Accept, Accept-Encoding header parser in JavaScript
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
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
/** | |
* Accept-* header parser in JavaScript - rfc2616 sec 14.4 | |
* | |
* @param {String} string Accept-* | |
* | |
* @return {Array} ordered by weight list of accepted values | |
* | |
* @example | |
* acceptLanguageParser('nl;q=0.6, en;q=0.8,el;q=0.4, en-US , de;q=0.2'); | |
* // ["en-US", "en", "nl", "el", "de"] | |
* acceptLanguageParser('text/html,application/xhtml+xml,application/xml;q=0.9,*\/*;q=0.8'); | |
* // ["text/html", "application/xhtml+xml", "application/xml", "*\/*"] | |
*/ | |
function acceptLanguageParser(string) { | |
var langs = string.split(','), i, c; | |
for (i = 0, c = langs.length; i < c; i++) { | |
langs[i] = langs[i].split(';q='); | |
langs[i][1] = +langs[i][1] || 1; | |
} | |
langs = langs.sort(function (a, b) { | |
return b[1] - a[1]; | |
}); | |
for (i = 0, c = langs.length; i < c; i++) { | |
langs[i] = langs[i][0].replace(/^\s+|\s+$/, ''); | |
} | |
return langs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great snippet, and funnily enough, after implementing it I found out express has a
request.acceptsLanguages(<supported languages>)
that does exactly this.