Skip to content

Commit

Permalink
Make detect methods static
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyganch committed Jun 9, 2014
1 parent fa20869 commit 61d4c5c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 77 deletions.
140 changes: 64 additions & 76 deletions lib/csscomb.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ var OPTIONS = [
'unitless-zero',
'vendor-prefix-align'
];
// Function for parsing CSS using Gonzales:
var cssToAST;

/**
* Starts Code Style processing process.
*
* @param {String|Object} config
* @constructor
* @name Comb
*/
Expand Down Expand Up @@ -303,7 +306,7 @@ var Comb = function(config) {
*/
this.processString = function processString(text, syntax, filename) {
if (!text) return text;
var tree = this.cssToAST(text, syntax, filename);
var tree = cssToAST(text, syntax, filename);
tree = processTree(tree);
return gonzales.astToCSS({ syntax: syntax, ast: tree });
};
Expand All @@ -317,53 +320,84 @@ var Comb = function(config) {
}
};


/**
* STATIC METHODS
* Methods that can be called without creating an instance
* For example: `Comb.getConfig('zen')`
*/
(function() {
/**
* Gets one of configuration files from `config` directory.
*
* @param {String} name Config's name: 'csscomb', 'zen' or 'yandex'
* @returns {Object} Configuration object
*/
Comb.getConfig = function getConfig(name) {
// Names of predefined configs:
var CONFIGS = ['csscomb', 'zen', 'yandex'];
name = name || 'csscomb';

/**
* Gets one of configuration files from `config` directory.
*
* @param {String} name Config's name: 'csscomb', 'zen' or 'yandex'
* @returns {Object} Configuration object
*/
Comb.getConfig = function getConfig(name) {
// Names of predefined configs:
var CONFIGS = ['csscomb', 'zen', 'yandex'];
name = name || 'csscomb';
if (typeof name !== 'string') {
throw new Error('Config name must be a string.');
}

if (typeof name !== 'string') {
throw new Error('Config name must be a string.');
}
if (CONFIGS.indexOf(name) < 0) {
throw new Error('"' + name + '" is not a valid config name. Try one of ' +
'the following: \'csscomb\', \'zen\' or \'yandex\'.');
}

if (CONFIGS.indexOf(name) < 0) {
throw new Error('"' + name + '" is not a valid config name. Try one of ' +
'the following: \'csscomb\', \'zen\' or \'yandex\'.');
}
return require('../config/' + name + '.json');
};

return require('../config/' + name + '.json');
};
/**
* Detects the options in the given file
*
* @param {String} path Path to the stylesheet
* @param {Array} options List of options to detect
* @returns {Object} Detected options
*/
Comb.detectInFile = function detectInFile(path, options) {
var stylesheet = fs.readFileSync(path, 'utf8');
return Comb.detectInString(stylesheet, options);
};

/**
* Detects the options in the given string
*
* @param {String} text Stylesheet
* @param {Array} options List of options to detect
* @returns {Object} Detected options
*/
Comb.detectInString = function detectInString(text, options) {
var result;
var handlers = [];

/**
* COMMON PUBLIC METHODS
* Methods that are common for all instances and don't depend on any instance
* variables
*/
if (!text) return text;

OPTIONS.forEach(function(option) {
if (options && options.indexOf(option) < 0) return;
try {
handlers.push(getHandler(option));
} catch (e) {
console.warn('\nFailed to load "%s" option:\n%s', option, e.message);
}
});

var tree = cssToAST(text);
var detectedOptions = detectInTree(tree, handlers);
result = getDetectedOptions(detectedOptions, handlers);
return result;
};

Comb.prototype = (function() {
/**
/**
* Converts CSS string to AST.
*
* @param {String} text CSS string
* @param {String} [syntax] Syntax name (e.g., `scss`)
* @param {String} [filename]
* @returns {Array} AST
*/
function cssToAST(text, syntax, filename) {
cssToAST = function cssToAST(text, syntax, filename) {
var string = JSON.stringify;
var fileInfo = filename ? ' at ' + filename : '';
var tree;
Expand All @@ -380,7 +414,7 @@ Comb.prototype = (function() {
}

return tree;
}
};

/**
* Gets option's data needed for detection
Expand All @@ -400,46 +434,6 @@ Comb.prototype = (function() {
}

/**
* Detects the options in the given file
*
* @param {String} path Path to the stylesheet
* @param {Array} options List of options to detect
* @returns {Object} Detected options
*/
function detectInFile(path, options) {
var stylesheet = fs.readFileSync(path, 'utf8');
return detectInString(stylesheet, options);
}

/**
* Detects the options in the given string
*
* @param {String} text Stylesheet
* @param {Array} options List of options to detect
* @returns {Object} Detected options
*/
function detectInString(text, options) {
var result;
var handlers = [];

if (!text) return text;

OPTIONS.forEach(function(option) {
if (options && options.indexOf(option) < 0) return;
try {
handlers.push(getHandler(option));
} catch (e) {
console.warn('\nFailed to load "%s" option:\n%s', option, e.message);
}
});

var tree = cssToAST(text);
var detectedOptions = detectInTree(tree, handlers);
result = getDetectedOptions(detectedOptions, handlers);
return result;
}

/**
* Processes tree and detects options.
*
* @param {Array} tree
Expand Down Expand Up @@ -541,12 +535,6 @@ Comb.prototype = (function() {

return options;
}

return {
cssToAST: cssToAST,
detectInFile: detectInFile,
detectInString: detectInString
};
})();

module.exports = Comb;
2 changes: 1 addition & 1 deletion test/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ mocha.suite.beforeEach(function() {
return sorted;
}
assert.equal(
JSON.stringify(sortObject(this.comb.detectInString(input, options))),
JSON.stringify(sortObject(Comb.detectInString(input, options))),
JSON.stringify(sortObject(expected))
);
};
Expand Down

0 comments on commit 61d4c5c

Please sign in to comment.