diff --git a/lib/index.js b/lib/index.js index a13fd37..4b572a7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 }; diff --git a/test/lib/assert.js b/test/lib/assert.js index 8796d0e..9abf1bf 100644 --- a/test/lib/assert.js +++ b/test/lib/assert.js @@ -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); diff --git a/test/naming/naming.test.js b/test/naming/naming.test.js index f47d0fb..55cdd8d 100644 --- a/test/naming/naming.test.js +++ b/test/naming/naming.test.js @@ -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]+' } }; @@ -32,7 +24,7 @@ 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', @@ -40,7 +32,7 @@ describe('naming', function () { 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) { @@ -50,7 +42,7 @@ 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', @@ -58,7 +50,7 @@ describe('naming', function () { 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) { @@ -68,7 +60,7 @@ describe('naming', function () { } }); - var levels = ['blocks'], + var levels = { blocks: { naming: convention.custom } }, expected = [{ entity: { block: 'block', elem: 'elem', modName: 'boolMod', modVal: true }, tech: 'tech', @@ -76,7 +68,7 @@ describe('naming', function () { 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) { @@ -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 }, @@ -116,6 +100,6 @@ describe('naming', function () { } ]; - assert(levels, {}, expected, done); + assert({ levels: levels, defaults: defaults }, expected, done); }); }); diff --git a/test/schemes/flat.test.js b/test/schemes/flat.test.js index 0485420..d5de714 100644 --- a/test/schemes/flat.test.js +++ b/test/schemes/flat.test.js @@ -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 () { diff --git a/test/schemes/multi.test.js b/test/schemes/multi.test.js index 7f4b11e..54e198d 100644 --- a/test/schemes/multi.test.js +++ b/test/schemes/multi.test.js @@ -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 () { @@ -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' }, diff --git a/test/schemes/nested.test.js b/test/schemes/nested.test.js index d920aad..e669498 100644 --- a/test/schemes/nested.test.js +++ b/test/schemes/nested.test.js @@ -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 () {