@@ -120,25 +120,45 @@ function specify(type, spec, index) {
120120
121121}
122122
123- function preferredMediaTypes ( accept , provided ) {
123+ /**
124+ * Returns a list of the acceptable media ranges in preferred order.
125+ *
126+ * @param {string } accept The raw contents of the `Accept` header
127+ * @param {string[] } provided The media ranges the server can produce
128+ * for the resource in question.
129+ * @param {Object } options Configuration options
130+ * @param {boolean } options.detailed If true, this function returns a
131+ * {type, parameters, q} object for each acceptable type, rather than a string.
132+ */
133+ function preferredMediaTypes ( accept , provided , options ) {
124134 // RFC 2616 sec 14.2: no header = */*
125135 var accepts = parseAccept ( accept === undefined ? '*/*' : accept || '' ) ;
136+ var detailed = options && options . detailed ;
137+
138+ var specDetails = function ( spec ) {
139+ return {
140+ type : "" + spec . type + '/' + spec . subtype ,
141+ parameters : spec . params ,
142+ q : spec . q
143+ } ;
144+ } ;
126145
146+ // return sorted list of all types if none are provided.
127147 if ( ! provided ) {
128- // sorted list of all types
129- return accepts . filter ( isQuality ) . sort ( compareSpecs ) . map ( function getType ( spec ) {
130- return spec . full ;
131- } ) ;
148+ var mapper = detailed ? specDetails : function ( spec ) { return spec . full ; } ;
149+ return accepts . filter ( isQuality ) . sort ( compareSpecs ) . map ( mapper ) ;
132150 }
133151
134152 var priorities = provided . map ( function getPriority ( type , index ) {
135153 return getMediaTypePriority ( type , accepts , index ) ;
136154 } ) ;
137155
138- // sorted list of accepted types
139- return priorities . filter ( isQuality ) . sort ( compareSpecs ) . map ( function getType ( priority ) {
156+ // sorted list of acceptable types
157+ var acceptables = priorities . filter ( isQuality ) . sort ( compareSpecs ) . map ( function getType ( priority ) {
140158 return provided [ priorities . indexOf ( priority ) ] ;
141159 } ) ;
160+
161+ return detailed ? acceptables . map ( parseMediaType ) . map ( specDetails ) : acceptables ;
142162}
143163
144164function compareSpecs ( a , b ) {
0 commit comments