@@ -68,9 +68,14 @@ Comb.prototype = {
6868 } ,
6969
7070 /**
71- * Sets up the detection environment.
71+ * Detects the options in the given string
72+ *
73+ * @param {String } text Stylesheet
74+ * @param {Array } options List of options to detect
75+ * @returns {Object }
7276 */
73- detect : function ( options ) {
77+ detectInString : function ( text , options ) {
78+ var result ;
7479 this . _detect = true ;
7580 this . _detected = { } ;
7681 this . _handlers = [ ] ;
@@ -87,8 +92,21 @@ Comb.prototype = {
8792 }
8893 } , this ) ;
8994
90- this . processed = 0 ;
91- this . changed = 0 ;
95+ result = this . processString ( text ) ;
96+ this . _detect = false ;
97+ return result ;
98+ } ,
99+
100+ /**
101+ * Detects the options in the given file
102+ *
103+ * @param {String } path Path to the stylesheet
104+ * @param {Array } options List of options to detect
105+ * @returns {Object }
106+ */
107+ detectInFile : function ( path , options ) {
108+ var stylesheet = fs . readFileSync ( path , 'utf8' ) ;
109+ return this . detectInString ( stylesheet , options ) ;
92110 } ,
93111
94112 /**
@@ -98,7 +116,6 @@ Comb.prototype = {
98116 * @returns {Array }
99117 */
100118 processTree : function ( tree ) {
101-
102119 // We walk across complete tree for each handler,
103120 // because we need strictly maintain order in which handlers work,
104121 // despite fact that handlers work on different level of the tree.
@@ -170,10 +187,6 @@ Comb.prototype = {
170187 */
171188 processFile : function ( path ) {
172189 var _this = this ;
173- if ( _this . _detect ) {
174- var syntax = path . split ( '.' ) . pop ( ) ;
175- return _this . processString ( fs . readFileSync ( path , 'utf8' ) , syntax , path ) ;
176- }
177190 if ( this . _shouldProcessFile ( path ) ) {
178191 return vfs . read ( path , 'utf8' ) . then ( function ( data ) {
179192 var syntax = path . split ( '.' ) . pop ( ) ;
@@ -292,31 +305,38 @@ Comb.prototype = {
292305 _getDetectedOptions : function ( detected ) {
293306 var options = { } ;
294307 Object . keys ( detected ) . forEach ( function ( option ) {
308+ // List of all the detected variants from the stylesheet for the given option:
295309 var values = detected [ option ] ;
296310 var i ;
297311 if ( values . length ) {
298312 if ( values . length === 1 ) {
299313 options [ option ] = values [ 0 ] ;
300314 } else {
315+ // If there are more than one value for the option, find the most popular one;
316+ // `variants` would be populated with the popularity for different values.
301317 var variants = { } ;
302- var best_guess = null ;
318+ var bestGuess = null ;
303319 var maximum = 0 ;
304320 for ( i = values . length ; i -- ; ) {
305- if ( variants [ values [ i ] ] ) {
306- variants [ values [ i ] ] ++ ;
321+ var currentValue = values [ i ] ;
322+ // Count the current value:
323+ if ( variants [ currentValue ] ) {
324+ variants [ currentValue ] ++ ;
307325 } else {
308- variants [ values [ i ] ] = 1 ;
326+ variants [ currentValue ] = 1 ;
309327 }
310- if ( variants [ values [ i ] ] >= maximum ) {
311- maximum = variants [ values [ i ] ] ;
312- best_guess = values [ i ] ;
328+ // If the current variant is the most popular one, treat it as the best guess:
329+ if ( variants [ currentValue ] >= maximum ) {
330+ maximum = variants [ currentValue ] ;
331+ bestGuess = currentValue ;
313332 }
314333 }
315- if ( best_guess !== null ) {
316- options [ option ] = best_guess ;
334+ if ( bestGuess !== null ) {
335+ options [ option ] = bestGuess ;
317336 }
318337 }
319338 } else {
339+ // If there are no values for the option, check if there is a default one:
320340 for ( i = this . _handlers . length ; i -- ; ) {
321341 if ( this . _handlers [ i ] . _name === option && this . _handlers [ i ] . _detectDefault !== undefined ) {
322342 options [ option ] = this . _handlers [ i ] . _detectDefault ;
0 commit comments