From 671b42030f621c5ec6f9b80afb243f8e1cfe334a Mon Sep 17 00:00:00 2001 From: Vasiliy Loginevskiy Date: Tue, 24 Jan 2017 17:07:44 +0300 Subject: [PATCH] MAJOR --- README.md | 25 +++++--- lib/schemes/flat.js | 17 ++---- lib/schemes/nested.js | 22 ++----- test/test.js | 132 ++++++++---------------------------------- 4 files changed, 50 insertions(+), 146 deletions(-) diff --git a/README.md b/README.md index 12a10f8..c718255 100644 --- a/README.md +++ b/README.md @@ -9,14 +9,19 @@ ## Usage ```js -var entity = { - block: 'b1', - elem: 'e1', - modName: 'm1', - modVal: 'v1' -}; - -var tech = 'js'; +var BemCell = require('@bem/cell'), + BemEntityName = require('@bem/entity-name'); + +var atom = new BemCell( + { + entity: new BemEntityName({ + block: 'b1', + elem: 'e1', + mod: {name: 'm1', val: 'v1'} + }), + tech: 'js' + } +); var options = { naming: { @@ -25,7 +30,9 @@ var options = { } }; // this is default value -require('bem-fs-scheme')('nested').path(entity, tech, options); // b1/__e1/_m1/b1__e1_m1_v1.js +var bemFs = require('bem-fs-scheme')('nested') + +bemFs.path(atom, options); // b1/__e1/_m1/b1__e1_m1_v1.js ``` License diff --git a/lib/schemes/flat.js b/lib/schemes/flat.js index a61c373..1132ddc 100644 --- a/lib/schemes/flat.js +++ b/lib/schemes/flat.js @@ -1,26 +1,19 @@ var path = require('path'), - BemCell = require('@bem/cell'), bemNaming = require('bem-naming'); module.exports = { - path: function(entity, tech, options) { + path: function(cell, options) { options || (options = {}); var layer = ''; - var _tech = tech; + var tech = cell.tech; + var entity = cell.entity; - if (BemCell.isBemCell(entity)) { - entity.layer && (layer = entity.layer); - if (typeof tech === 'object') { - options = tech; - } - _tech = entity.tech; - entity = entity.entity; - } + cell.layer && (layer = cell.layer); var naming = bemNaming(options.naming); return path.join(layer, - naming.stringify(entity) + (_tech ? '.' + _tech : '')); + naming.stringify(entity) + (tech ? '.' + tech : '')); } }; diff --git a/lib/schemes/nested.js b/lib/schemes/nested.js index fbde0f0..0801875 100644 --- a/lib/schemes/nested.js +++ b/lib/schemes/nested.js @@ -1,26 +1,16 @@ var path = require('path'), - BemCell = require('@bem/cell'), bemNaming = require('bem-naming'); module.exports = { - path: function(entity, tech, options) { + path: function(cell, options) { options || (options = {}); var layer = ''; - var modName = ''; - var _tech = tech; + var tech = cell.tech; + var entity = cell.entity; + var modName = Object(entity.mod).name; - if (BemCell.isBemCell(entity)) { - entity.layer && (layer = entity.layer); - if (typeof tech === 'object') { - options = tech; - } - _tech = entity.tech; - entity = entity.entity; - modName = Object(entity.mod).name; - } else { - modName = entity.modName; - } + cell.layer && (layer = cell.layer); var naming = bemNaming(options.naming); var folder = path.join(layer, entity.block, @@ -28,6 +18,6 @@ module.exports = { modName ? (naming.modDelim + modName) : ''); return path.join(folder, - naming.stringify(entity) + (_tech ? '.' + _tech : '')); + naming.stringify(entity) + (tech ? '.' + tech : '')); } }; diff --git a/test/test.js b/test/test.js index c5f2c0e..188dafd 100644 --- a/test/test.js +++ b/test/test.js @@ -6,9 +6,6 @@ var expect = require('chai').expect, describe('default', function() { it('should return path + tech', function() { - expect('a/a.js') - .eql(scheme().path({block: 'a'}, 'js')); - expect('a/a.js') .eql(scheme().path( new BemCell({ @@ -20,15 +17,12 @@ describe('default', function() { }); it('should return nested scheme by default', function() { - expect(scheme().path({block: 'a', elem: 'e1'}, 'js')) - .eql('a/__e1/a__e1.js'); - expect(scheme().path( new BemCell({ entity: new BemEntityName({block: 'a', elem: 'e1'}), tech: 'js' })) - ).eql('a/__e1/a__e1.js', 'bemCell - api'); + ).eql('a/__e1/a__e1.js'); }); it('should return error', function() { @@ -37,15 +31,6 @@ describe('default', function() { }); it('should support optional naming style', function() { - expect( - scheme('nested').path({ - block: 'a', - elem: 'e1', - modName: 'mn', - modVal: 'mv' - }, 'js', {naming: {elem: '%%%', mod: '###'}}) - ).eql('a/%%%e1/###mn/a%%%e1###mn###mv.js'); - expect( scheme('nested').path( new BemCell({ @@ -57,59 +42,41 @@ describe('default', function() { tech: 'js' }), {naming: {elem: '%%%', mod: '###'}}) - ).eql('a/%%%e1/###mn/a%%%e1###mn###mv.js', 'bemCell - api'); + ).eql('a/%%%e1/###mn/a%%%e1###mn###mv.js'); }); describe('lib/schemes/nested', function() { it('should return path for a block', function() { - expect(scheme('nested').path({block: 'a'}, 'js')) - .eql('a/a.js'); - expect(scheme('nested').path( new BemCell({ entity: new BemEntityName({block: 'a'}), tech: 'js' }) - )).eql('a/a.js', 'bemCell - api'); + )).eql('a/a.js'); }); it('should return path for a block with modifier', function() { expect( - scheme('nested').path({ - block: 'a', modName: 'mn', modVal: 'mv' - }, 'js') + scheme('nested').path( + new BemCell({ + entity: new BemEntityName({ block: 'a', modName: 'mn', modVal: 'mv' }), + tech: 'js' + }) + ) ).eql('a/_mn/a_mn_mv.js'); - - expect( - scheme('nested').path({ - block: 'a', modName: 'mn', modVal: 'mv' - }, 'js') - ).eql('a/_mn/a_mn_mv.js', 'bemCell - api'); }); it('should return path for a block with boolean modifier', function() { - expect( - scheme('nested').path({ - block: 'a', modName: 'mn', modVal: true - }, 'js') - ).eql('a/_mn/a_mn.js'); - expect( scheme('nested').path( new BemCell({ entity: new BemEntityName({block: 'a', mod: {name: 'mn', val: true }}), tech: 'js' })) - ).eql('a/_mn/a_mn.js', 'bemCell - api'); + ).eql('a/_mn/a_mn.js'); }); it('should return path for a block with modifier without value', function() { - expect( - scheme('nested').path({ - block: 'a', modName: 'mn' - }, 'js') - ).eql('a/_mn/a_mn.js'); - expect( scheme('nested').path( new BemCell({ @@ -117,14 +84,10 @@ describe('default', function() { tech: 'js' }) ) - ).eql('a/_mn/a_mn.js', 'bemCell - api'); + ).eql('a/_mn/a_mn.js'); }); it('should return path for elem', function() { - expect( - scheme('nested').path({block: 'a', elem: 'e1'}, 'js') - ).eql('a/__e1/a__e1.js'); - expect( scheme('nested').path( new BemCell({ @@ -132,19 +95,10 @@ describe('default', function() { tech: 'js' }) ) - ).eql('a/__e1/a__e1.js', 'bemCell - api'); + ).eql('a/__e1/a__e1.js'); }); it('should return path for modName elem', function() { - expect( - scheme('nested').path({ - block: 'a', - elem: 'e1', - modName: 'mn', - modVal: 'mv' - }, 'js') - ).eql('a/__e1/_mn/a__e1_mn_mv.js'); - expect( scheme('nested').path( new BemCell({ @@ -156,19 +110,10 @@ describe('default', function() { tech: 'js' }) ) - ).eql('a/__e1/_mn/a__e1_mn_mv.js', 'bemCell - api'); + ).eql('a/__e1/_mn/a__e1_mn_mv.js'); }); it('should support optional naming style', function() { - expect( - scheme('nested').path({ - block: 'a', - elem: 'e1', - modName: 'mn', - modVal: 'mv' - }, 'js', {naming: {elem: '%%%', mod: '###'}}) - ).eql('a/%%%e1/###mn/a%%%e1###mn###mv.js'); - expect( scheme('nested').path( new BemCell({ @@ -181,7 +126,7 @@ describe('default', function() { }), {naming: {elem: '%%%', mod: '###'}} ) - ).eql('a/%%%e1/###mn/a%%%e1###mn###mv.js', 'bemCell - api'); + ).eql('a/%%%e1/###mn/a%%%e1###mn###mv.js'); }); it('should support optional tech for BemCell', function() { @@ -195,7 +140,7 @@ describe('default', function() { }) }) ) - ).eql('a/__e1/_mn/a__e1_mn_mv', 'bemCell - api'); + ).eql('a/__e1/_mn/a__e1_mn_mv'); }); it('should support layer for BemCell', function() { @@ -211,43 +156,30 @@ describe('default', function() { layer: 'common.blocks' }) ) - ).eql('common.blocks/a/__e1/_mn/a__e1_mn_mv.js', 'bemCell - api'); + ).eql('common.blocks/a/__e1/_mn/a__e1_mn_mv.js'); }); }); describe('lib/schemes/flat', function() { it('should return path for a block', function() { - expect(scheme('flat').path({block: 'a'}, 'js')) - .eql('a.js'); - expect(scheme('flat').path( new BemCell({ entity: new BemEntityName({block: 'a'}), tech: 'js' }) - )).eql('a.js', 'bemCell - api'); + )).eql('a.js'); }); it('should return path for a block with modifier', function() { - expect( - scheme('flat').path({ - block: 'a', modName: 'mn', modVal: 'mv' - }, 'js') - ).eql('a_mn_mv.js'); - expect(scheme('flat').path( new BemCell({ entity: new BemEntityName({block: 'a', mod: {name: 'mn', val: 'mv'}}), tech: 'js' }) - )).eql('a_mn_mv.js', 'bemCell - api'); + )).eql('a_mn_mv.js'); }); it('should return path for elem', function() { - expect( - scheme('flat').path({block: 'a', elem: 'e1'}, 'js') - ).eql('a__e1.js'); - expect( scheme('flat').path( new BemCell({ @@ -255,19 +187,10 @@ describe('default', function() { tech: 'js' }) ) - ).eql('a__e1.js', 'bemCell - api'); + ).eql('a__e1.js'); }); it('should return path for modName elem', function() { - expect( - scheme('flat').path({ - block: 'a', - elem: 'e1', - modName: 'mn', - modVal: 'mv' - }, 'js') - ).eql('a__e1_mn_mv.js'); - expect( scheme('flat').path( new BemCell({ @@ -279,19 +202,10 @@ describe('default', function() { tech: 'js' }) ) - ).eql('a__e1_mn_mv.js', 'bemCell - api'); + ).eql('a__e1_mn_mv.js'); }); it('should support optional naming style', function() { - expect( - scheme('flat').path({ - block: 'a', - elem: 'e1', - modName: 'mn', - modVal: 'mv' - }, 'js', {naming: {elem: '%%%', mod: '###'}}) - ).eql('a%%%e1###mn###mv.js'); - expect( scheme('flat').path( new BemCell({ @@ -304,7 +218,7 @@ describe('default', function() { }), {naming: {elem: '%%%', mod: '###'}} ) - ).eql('a%%%e1###mn###mv.js', 'bemCell - api'); + ).eql('a%%%e1###mn###mv.js'); }); it('should support optional tech for BemCell', function() { @@ -318,7 +232,7 @@ describe('default', function() { }) }) ) - ).eql('a__e1_mn_mv', 'bemCell - api'); + ).eql('a__e1_mn_mv'); }); it('should support layer for BemCell', function() { @@ -334,7 +248,7 @@ describe('default', function() { layer: 'common.blocks' }) ) - ).eql('common.blocks/a__e1_mn_mv.js', 'bemCell - api'); + ).eql('common.blocks/a__e1_mn_mv.js'); }); }); });