renamed prev util to parseQuality()
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent fe5efa5 · Browse files · 2 files·+25 added−33 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+25 added−33 removed
- tests:+13 added−13 removed
- other:+12 added−20 removed
Provenance
- Commit
29d4ffd- Tree
947b975- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
fe5efa5
2 files changed+25 added−33 removed
modified:lib/utils.js+12 added-20 removed
59 unmodified lines606162636465646566676869707172727374757675767778798081808182838485868788898890919092939495969796989910010110010110210310410510610710810910210310410510 unmodified lines59 unmodified lines .replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');};
/** * Parse accepts `str`, returning an * object with `.type` and `.quality`. * Parse quality `str`, returning an * object with `.value` and `.quality`. * * @param {Type} name * @return {Type} * @api public */
exports.parseAccepts = function(str){exports.parseQuality = function(str){ return str .split(/ *, */) .map(type) .sort(quality); .map(quality) .sort(function(a, b){ return b.quality - a.quality; });};
/** * Parse type `str` returning an * object with `.type` and `.quality`. * Parse quality `str` returning an * object with `.value` and `.quality`. * * @param {String} str * @return {Object} * @api private */
function type(str) {function quality(str) { var parts = str.split(/ *; */) , type = parts[0]; , val = parts[0];
var q = parts[1] ? parseFloat(parts[1].split(/ *= */)[1]) : 1;
return { type: type, quality: q }; return { value: val, quality: q };}
/** * Sort by quality. * * @api private */
function quality(a, b) { return b.quality - a.quality;}
/** * Escape special characters in the given string of html. * * @param {String} html * @return {String}10 unmodified linesmodified:test/utils.js+13 added-13 removed
28 unmodified lines293031323333343536353637383940414041424344434445464748494849505152535455565754555657585928 unmodified lines .should.equal('<script>foo & "bar"') })})
describe('utils.parseAccepts(str)', function(){describe('utils.parseQuality(str)', function(){ it('should default quality to 1', function(){ utils.parseAccepts('text/html') .should.eql([{ type: 'text/html', quality: 1 }]); utils.parseQuality('text/html') .should.eql([{ value: 'text/html', quality: 1 }]); }) it('should parse qvalues', function(){ utils.parseAccepts('text/html; q=0.5') .should.eql([{ type: 'text/html', quality: 0.5 }]); utils.parseQuality('text/html; q=0.5') .should.eql([{ value: 'text/html', quality: 0.5 }]);
utils.parseAccepts('text/html; q=.2') .should.eql([{ type: 'text/html', quality: 0.2 }]); utils.parseQuality('text/html; q=.2') .should.eql([{ value: 'text/html', quality: 0.2 }]); }) it('should work with messed up whitespace', function(){ utils.parseAccepts('text/html ; q = .2') .should.eql([{ type: 'text/html', quality: 0.2 }]); utils.parseQuality('text/html ; q = .2') .should.eql([{ value: 'text/html', quality: 0.2 }]); }) it('should sort by quality', function(){ var str = 'text/plain;q=.2, application/json, text/html;q=0.5'; var arr = utils.parseAccepts(str); arr[0].type.should.equal('application/json'); arr[1].type.should.equal('text/html'); arr[2].type.should.equal('text/plain'); var arr = utils.parseQuality(str); arr[0].value.should.equal('application/json'); arr[1].value.should.equal('text/html'); arr[2].value.should.equal('text/plain'); })})No newline at end of fileNo newline at end of file