Skip to content

Commit a5d8462

Browse files
committed
Merge pull request #129 from csscomb/tg/102-chainable-methods
Pass config to `new Comb()` and make it chainable (#102)
2 parents 903ee30 + 78f8e2a commit a5d8462

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

lib/csscomb.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var doNothing = function() {};
1111
* @constructor
1212
* @name Comb
1313
*/
14-
var Comb = function() {
14+
var Comb = function(config) {
1515
this.SUPPORTED_SYNTAXES = ['css', 'scss', 'less'];
1616
this._options = [
1717
'remove-empty-rulesets',
@@ -34,6 +34,17 @@ var Comb = function() {
3434
];
3535
this._exclude = null;
3636
this._detect = false;
37+
38+
// If config was passed, configure:
39+
if (typeof config === 'string' &&
40+
['csscomb', 'zen', 'yandex'].indexOf(config) > -1) {
41+
config = require('../config/' + config + '.json');
42+
}
43+
44+
if (typeof config === 'object') this.configure(config);
45+
46+
// Return Comb's object to make creating new instance chainable:
47+
return this;
3748
};
3849

3950
Comb.prototype = {
@@ -87,6 +98,9 @@ Comb.prototype = {
8798
this.changed = 0;
8899
this._verbose = config.verbose;
89100
this._lint = config.lint;
101+
102+
// Return Comb's object to make the method chainable:
103+
return this;
90104
},
91105

92106
/**

test/configure.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var Comb = require('../lib/csscomb');
2+
var assert = require('assert');
3+
4+
describe('csscomb methods', function() {
5+
var comb;
6+
var input;
7+
var output;
8+
var expected;
9+
10+
it('Passing no config to constructor should not configure anything', function() {
11+
comb = new Comb();
12+
assert.equal(undefined, comb._handlers);
13+
});
14+
15+
it('Passing valid config name to constructor should configure using correct config', function() {
16+
comb = new Comb('zen');
17+
input = 'a { color: tomato; top: 0; }';
18+
expected = 'a {top: 0; color: tomato; }';
19+
output = comb.processString(input);
20+
21+
assert.equal(expected, output);
22+
});
23+
24+
it('Passing config object to constructor should configure using that object', function() {
25+
comb = new Comb({ 'always-semicolon': true });
26+
input = 'a { color: tomato }';
27+
expected = 'a { color: tomato; }';
28+
output = comb.processString(input);
29+
30+
assert.equal(expected, output);
31+
});
32+
33+
it('new Comb() should be chainable', function() {
34+
input = 'a { color: tomato; top: 0; }';
35+
expected = 'a {top: 0; color: tomato; }';
36+
output = new Comb('zen').processString(input);
37+
38+
assert.equal(expected, output);
39+
});
40+
41+
it('configure() should be chainable', function() {
42+
input = 'a { color: tomato }';
43+
expected = 'a { color: tomato; }';
44+
output = new Comb().configure({ 'always-semicolon': true }).processString(input);
45+
46+
assert.equal(expected, output);
47+
});
48+
});

0 commit comments

Comments
 (0)