Added originalIndex to parseQuality to correctly sort long lists (v8 does unstable quicksort for length > 10)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by silentjohnny · committed by silentjohnny · parent 2e53cb7 · Browse files · 2 files·+39 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+39 added−11 removed
- tests:+31 added−8 removed
- other:+8 added−3 removed
Provenance
- Commit
88273a5- Tree
3ab35b1- Author
silentjohnny <joris@notifica.re>- Committer
silentjohnny <joris@notifica.re>- Parent
2e53cb7
2 files changed+39 added−11 removed
modified:lib/utils.js+8 added-3 removed
215 unmodified lines216 .filter(function(obj){217 return obj.quality;218 })219 .sort(function(a, b){220+ if (a.quality === b.quality) {221+ return a.originalIndex - b.originalIndex;222+ } else {220- return b.quality - a.quality;223+ return b.quality - a.quality;224+ }225 });226 };227 228 /**229 * Parse accept params `str` returning an230 * object with `.value`, `.quality` and `.params`.231+ * also includes `.originalIndex` for stable sorting232 *233 * @param {String} str234 * @return {Object}235 * @api private236 */237 233-function acceptParams(str) {238+function acceptParams(str, index) {239 var parts = str.split(/ *; */);235- var ret = { value: parts[0], quality: 1, params: {} };240+ var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };241 242 for (var i = 1; i < parts.length; ++i) {243 var pms = parts[i].split(/ *= */);244 if ('q' == pms[0]) {63 unmodified linesmodified:test/utils.js+31 added-8 removed
50 unmodified lines51 52 describe('utils.parseParams(str)', function(){53 it('should default quality to 1', function(){54 utils.parseParams('text/html')55- .should.eql([{ value: 'text/html', quality: 1, params: {}}]);55+ .should.eql([{ value: 'text/html', quality: 1, params: {}, originalIndex: 0 }]);56 })57 58 it('should parse qvalues', function(){59 utils.parseParams('text/html; q=0.5')60- .should.eql([{ value: 'text/html', quality: 0.5, params: {}}]);60+ .should.eql([{ value: 'text/html', quality: 0.5, params: {}, originalIndex: 0 }]);61 62 utils.parseParams('text/html; q=.2')63- .should.eql([{ value: 'text/html', quality: 0.2, params: {}}]);63+ .should.eql([{ value: 'text/html', quality: 0.2, params: {}, originalIndex: 0 }]);64 })65 66 it('should parse accept parameters', function(){67 utils.parseParams('application/json; ver=2.0')68- .should.eql([{ value: 'application/json', quality: 1, params: {ver: "2.0"}}]);68+ .should.eql([{ value: 'application/json', quality: 1, params: {ver: "2.0"}, originalIndex: 0 }]);69 70 utils.parseParams('text/html; q=0.5; level=2')71- .should.eql([{ value: 'text/html', quality: 0.5, params: {level: "2"}}]);71+ .should.eql([{ value: 'text/html', quality: 0.5, params: {level: "2"}, originalIndex: 0 }]);72 utils.parseParams('text/html;q=.2;ver=beta')73- .should.eql([{ value: 'text/html', quality: 0.2, params: {ver: "beta"}}]);73+ .should.eql([{ value: 'text/html', quality: 0.2, params: {ver: "beta"}, originalIndex: 0 }]);74 })75 76 it('should work with messed up whitespace', function(){77 utils.parseParams('text/html ; q = .2')78- .should.eql([{ value: 'text/html', quality: 0.2, params: {}}]);78+ .should.eql([{ value: 'text/html', quality: 0.2, params: {}, originalIndex: 0 }]);79 })80 81 it('should work with multiples', function(){82 var str = 'da, en;q=.5, en-gb;q=.8';1 unmodified line84 arr[0].value.should.equal('da');85 arr[1].value.should.equal('en-gb');86 arr[2].value.should.equal('en');87 })88+89+ it('should work with long lists', function(){90+ var str = 'en, nl, fr, de, ja, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl';91+ var arr = utils.parseParams(str);92+ arr[0].value.should.equal('en');93+ arr[1].value.should.equal('nl');94+ arr[2].value.should.equal('fr');95+ arr[4].value.should.equal('de');96+ arr[5].value.should.equal('ja');97+ arr[6].value.should.equal('it');98+ arr[7].value.should.equal('es');99+ arr[8].value.should.equal('pt');100+ arr[9].value.should.equal('pt-PT');101+ arr[10].value.should.equal('da');102+ arr[11].value.should.equal('fi');103+ arr[12].value.should.equal('nb');104+ arr[13].value.should.equal('sv');105+ arr[14].value.should.equal('ko');106+ arr[15].value.should.equal('zh-Hans');107+ arr[16].value.should.equal('zh-Hant');108+ arr[17].value.should.equal('ru');109+ arr[18].value.should.equal('pl');110+ })111 112 it('should sort by quality', function(){113 var str = 'text/plain;q=.2, application/json, text/html;q=0.5';114 var arr = utils.parseParams(str);119 unmodified lines234 utils.accepts('html', '*/html')235 .should.equal('html');236 })237 })215-})238+})