Skip to content
This repository has been archived by the owner on Feb 6, 2018. It is now read-only.

Commit

Permalink
Refactored walk arguments:
Browse files Browse the repository at this point in the history
**1. The second argument now expects `config` instead of `options`.**

Options by default now need specify in `config.defaults` instead of `options`.

*Before:*

```js
walk(['blocks'], { sheme: 'flat' });
```

*After:*

```js
var config = {
    defaults: { sheme: 'flat' }
};

walk(['blocks'], config);
```

**2. Info about levels (scheme and naming) now need specify in `config.levels` instead of `levels` arguments.**

The `levels` argument as `Array` no longer supported.

*Before:*

```js
walk([{ path: 'blocks', sheme: 'flat' }]);
```

*After:*

```js
var config = {
    levels: {
        blocks: { sheme: 'flat' }
    }
};

walk(['blocks'], config);
```
  • Loading branch information
andrewblond committed Mar 28, 2015
1 parent 7f5c635 commit c5682ee
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 59 deletions.
21 changes: 14 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,30 @@ var fs = require('fs'),
Readable = require('stream').Readable,
schemes = require('./schemes');

module.exports = function (levels, options) {
options || (options = {});
module.exports = function (levels, config) {
config || (config = {});

var output = new Readable({ objectMode: true }),
defaultScheme = options.scheme || schemes.nested,
defaultNaming = options.naming ? bemNaming(options.naming) : bemNaming;
defaults = config.defaults || {},
levelHash = config.levels || {},
defaultScheme = defaults.scheme || schemes.nested,
defaultNaming = defaults.naming ? bemNaming(defaults.naming) : bemNaming;

if (typeof defaultScheme === 'string') {
defaultScheme = schemes[defaultScheme];
}

levels = levels.map(function (level) {
var scheme = level && level.scheme,
naming = level && level.naming;
var config = levelHash[level],
scheme, naming;

if (config) {
scheme = config.scheme;
naming = config.naming;
}

return {
path: typeof level === 'string' ? level : level.path,
path: level,
scheme: typeof scheme === 'string' ? schemes[scheme] : (scheme || defaultScheme),
naming: naming ? bemNaming(naming) : defaultNaming
};
Expand Down
5 changes: 3 additions & 2 deletions test/lib/assert.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var walk = require('../../lib/index');

module.exports = function (levels, opts, expected, cb) {
module.exports = function (config, expected, cb) {
var buffer = [],
walker = walk(levels, opts);
levels = Array.isArray(config.levels) ? config.levels : Object.keys(config.levels),
walker = walk(levels, config);

walker.on('data', function (obj) {
buffer.push(obj);
Expand Down
52 changes: 18 additions & 34 deletions test/naming/naming.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
var path = require('path'),
mock = require('mock-fs'),
assert = require('../lib/assert'),
defaults = { scheme: 'flat' },
convention = {
original: {
scheme: 'flat',
naming: { elem: '__', mod: '_' }
},
csswizardry: {
scheme: 'flat',
naming: { elem: '__', mod: '--' }
},
custom: {
scheme: 'flat',
naming: {
elem: '-',
mod: '--',
wordPattern: '[a-zA-Z0-9]+'
}
original: { elem: '__', mod: '_' },
csswizardry: { elem: '__', mod: '--' },
custom: {
elem: '-',
mod: '--',
wordPattern: '[a-zA-Z0-9]+'
}
};

Expand All @@ -32,15 +24,15 @@ describe('naming', function () {
}
});

var levels = ['blocks'],
var levels = { blocks: { naming: convention.original } },
expected = [{
entity: { block: 'block', elem: 'elem', modName: 'bool-mod', modVal: true },
tech: 'tech',
level: 'blocks',
path: path.join('blocks', 'block__elem_bool-mod.tech')
}];

assert(levels, convention.original, expected, done);
assert({ levels: levels, defaults: defaults }, expected, done);
});

it('must support Convention by Harry Roberts', function (done) {
Expand All @@ -50,15 +42,15 @@ describe('naming', function () {
}
});

var levels = ['blocks'],
var levels = { blocks: { naming: convention.csswizardry } },
expected = [{
entity: { block: 'block', elem: 'elem', modName: 'bool-mod', modVal: true },
tech: 'tech',
level: 'blocks',
path: path.join('blocks', 'block__elem--bool-mod.tech')
}];

assert(levels, convention.csswizardry, expected, done);
assert({ levels: levels, defaults: defaults }, expected, done);
});

it('must support custom naming', function (done) {
Expand All @@ -68,15 +60,15 @@ describe('naming', function () {
}
});

var levels = ['blocks'],
var levels = { blocks: { naming: convention.custom } },
expected = [{
entity: { block: 'block', elem: 'elem', modName: 'boolMod', modVal: true },
tech: 'tech',
level: 'blocks',
path: path.join('blocks', 'block-elem--boolMod.tech')
}];

assert(levels, convention.custom, expected, done);
assert({ levels: levels, defaults: defaults }, expected, done);
});

it('must support several naming', function (done) {
Expand All @@ -89,18 +81,10 @@ describe('naming', function () {
}
});

var levels = [
{
path: 'original.naming',
scheme: 'flat',
naming: convention.original
},
{
path: 'csswizardry.naming',
scheme: 'flat',
naming: convention.csswizardry.naming
}
],
var levels = {
'original.naming': { naming: convention.original },
'csswizardry.naming': { naming: convention.csswizardry }
},
expected = [
{
entity: { block: 'block', elem: 'elem', modName: 'bool-mod', modVal: true },
Expand All @@ -116,6 +100,6 @@ describe('naming', function () {
}
];

assert(levels, {}, expected, done);
assert({ levels: levels, defaults: defaults }, expected, done);
});
});
6 changes: 4 additions & 2 deletions test/schemes/flat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ var path = require('path'),
mock = require('mock-fs'),
walk = require('../../lib/index'),
verboseAssert = require('../lib/assert'),
opts = { scheme: 'flat' },
assert = function (levels, expected, done) {
verboseAssert(levels, opts, expected, done);
verboseAssert({
levels: levels,
defaults: { scheme: 'flat' }
}, expected, done);
};

describe('flat scheme', function () {
Expand Down
17 changes: 5 additions & 12 deletions test/schemes/multi.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
var path = require('path'),
mock = require('mock-fs'),
verboseAssert = require('../lib/assert'),
opts = {},
assert = function (levels, expected, done) {
verboseAssert(levels, opts, expected, done);
verboseAssert({ levels: levels }, expected, done);
};

describe('multi scheme', function () {
Expand All @@ -23,16 +22,10 @@ describe('multi scheme', function () {
}
});

var levels = [
{
path: 'flat.level',
scheme: 'flat'
},
{
path: 'nested.level',
scheme: 'nested'
}
],
var levels = {
'flat.level': { scheme: 'flat' },
'nested.level': { scheme: 'nested' }
},
expected = [
{
entity: { block: 'block' },
Expand Down
3 changes: 1 addition & 2 deletions test/schemes/nested.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ var path = require('path'),
mock = require('mock-fs'),
walk = require('../../lib/index'),
verboseAssert = require('../lib/assert'),
opts = { scheme: 'nested' },
assert = function (levels, expected, done) {
verboseAssert(levels, opts, expected, done);
verboseAssert({ levels: levels }, expected, done);
};

describe('nested scheme', function () {
Expand Down

0 comments on commit c5682ee

Please sign in to comment.