refactored utils.accepts()
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 fdf9692 · Browse files · 2 files·+26 added−51 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+26 added−51 removed
- tests:+24 added−28 removed
- other:+2 added−23 removed
Provenance
- Commit
86a9e08- Tree
e0d6db2- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
fdf9692
2 files changed+26 added−51 removed
modified:lib/utils.js+2 added-23 removed
113 unmodified lines114 * @api private115 */116 117 exports.accepts = function(type, str){118- if (Array.isArray(type)) return exports.acceptsArray(type, str);119-120- // accept anything when Accept is not present121- if (!str) return true;122-123- // resolve mime124- if (!~type.indexOf('/')) type = mime.lookup(type);125-126- // split type/subtype127- type = type.split('/');128-129- // parse130- var accepted = exports.parseAccept(str)131- , len = accepted.length132- , obj133- , ok;134-135- for (var i = 0; i < len; ++i) {136- obj = accepted[i];137- if (exports.accept(type, obj)) return true;138- }139-140- return false;118+ if (!Array.isArray(type)) type = [type];119+ return exports.acceptsArray(type, str);120 };121 122 /**123 * Check if `type` array is acceptable for `other`.92 unmodified linesmodified:test/utils.js+24 added-28 removed
83 unmodified lines848586878888899090919293949595969797989910010110210210310410410510610710810910911011111111211311411511611411511611711811912112012112312212312412612712812512612712812913013313113213513313413513813914013613713813914014114514214314714414514615015115214714814915015115215715315415516015615715815983 unmodified lines})
describe('utils.accepts(type, str)', function(){ describe('when a string is not given', function(){ it('should return true', function(){ it('should return the value', function(){ utils.accepts('text/html') .should.be.true; .should.equal('text/html'); }) }) describe('when a string is empty', function(){ it('should return true', function(){ it('should return the value', function(){ utils.accepts('text/html', '') .should.be.true; .should.equal('text/html'); }) }) describe('when */* is given', function(){ it('should return true', function(){ it('should return the value', function(){ utils.accepts('text/html', 'text/plain, */*') .should.be.true; .should.equal('text/html'); }) })
describe('when accepting type/subtype', function(){ it('should return true when present', function(){ it('should return the value when present', function(){ utils.accepts('text/html', 'text/plain, text/html') .should.be.true; .should.equal('text/html'); }) it('should return false otherwise', function(){ utils.accepts('text/html', 'text/plain, application/json') .should.be.false; it('should return undefined otherwise', function(){ assert(null == utils.accepts('text/html', 'text/plain, application/json')); }) }) describe('when accepting */subtype', function(){ it('should return true when present', function(){ it('should return the value when present', function(){ utils.accepts('text/html', 'text/*') .should.be.true; .should.equal('text/html'); }) it('should return false otherwise', function(){ utils.accepts('text/html', 'image/*') .should.be.false; it('should return undefined otherwise', function(){ assert(null == utils.accepts('text/html', 'image/*')); }) }) describe('when accepting type/*', function(){ it('should return true when present', function(){ it('should return the value when present', function(){ utils.accepts('text/html', '*/html') .should.be.true; .should.equal('text/html'); }) it('should return false otherwise', function(){ utils.accepts('text/html', '*/json') .should.be.false; it('should return undefined otherwise', function(){ assert(null == utils.accepts('text/html', '*/json')); }) }) describe('when an extension is given', function(){ it('should return true when present', function(){ it('should return the value when present', function(){ utils.accepts('html', 'text/html, application/json') .should.be.true; .should.equal('html'); }) it('should return false otherwise', function(){ utils.accepts('html', 'text/plain, application/json') .should.be.false; it('should return undefined otherwise', function(){ assert(null == utils.accepts('html', 'text/plain, application/json')); }) it('should support *', function(){ utils.accepts('html', 'text/*') .should.be.true; .should.equal('html');
utils.accepts('html', '*/html') .should.be.true; .should.equal('html'); }) })})No newline at end of fileNo newline at end of file