Skip to content
This repository has been archived by the owner on Aug 1, 2019. It is now read-only.

Commit

Permalink
Add support for @bem/cell
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeti-or committed Jan 24, 2017
1 parent 37a85da commit 1a4f7e3
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 18 deletions.
19 changes: 17 additions & 2 deletions lib/schemes/flat.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
var bemNaming = require('bem-naming');
var path = require('path'),
BemCell = require('@bem/cell'),
bemNaming = require('bem-naming');

module.exports = {
path: function(entity, tech, options) {
options || (options = {});

var layer = '';
var _tech = tech;

if (BemCell.isBemCell(entity)) {
entity.layer && (layer = entity.layer);
if (typeof tech === 'object') {
options = tech;
}
_tech = entity.tech;
entity = entity.entity;
}

var naming = bemNaming(options.naming);

return naming.stringify(entity) + '.' + tech;
return path.join(layer, naming.stringify(entity) + '.' + _tech);
}
};
30 changes: 23 additions & 7 deletions lib/schemes/nested.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
var path = require('path'),
BemCell = require('@bem/cell'),
bemNaming = require('bem-naming');

module.exports = {
path: function(entity, tech, options) {
options || (options = {});
var naming = bemNaming(options.naming),
elemFolder = naming.elemDelim + entity.elem,
modFolder = naming.modDelim + entity.modName,
folder = path.join(entity.block,
entity.elem ? elemFolder : '',
entity.modName ? modFolder : '');

var layer = '';
var modName = '';
var _tech = tech;

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;
}

var naming = bemNaming(options.naming);
var folder = path.join(layer, entity.block,
entity.elem ? (naming.elemDelim + entity.elem) : '',
modName ? (naming.modDelim + modName) : '');

return path.join(folder,
naming.stringify(entity) + '.' + tech);
naming.stringify(entity) + '.' + _tech);
}
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
},
"homepage": "https://github.com/bem-sdk/bem-fs-scheme#readme",
"dependencies": {
"@bem/cell": "^0.2.1",
"bem-naming": "^1.0.1"
},
"devDependencies": {
"@bem/entity-name": "^1.1.0",
"chai": "^3.5.0",
"eslint": "^3.1.1",
"eslint-config-pedant": "^0.7.0",
Expand Down
197 changes: 188 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
var expect = require('chai').expect,
BemCell = require('@bem/cell'),
BemEntityName = require('@bem/entity-name'),
scheme = require('..');

describe('default', function() {

it('should return path + tech', function() {
expect('a/a.js')
.eql(scheme().path({ block: 'a' }, 'js'));
.eql(scheme().path({block: 'a'}, 'js'));

expect('a/a.js')
.eql(scheme().path(
new BemCell({
entity: new BemEntityName({block: 'a'}),
tech: 'js'
})),
'bemCell - api'
);
});

it('should return nested scheme by default', function() {
expect(scheme().path({ block: 'a', elem: 'e1' }, 'js'))
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');
});

it('should return error', function() {
Expand All @@ -25,14 +43,34 @@ describe('default', function() {
elem: 'e1',
modName: 'mn',
modVal: 'mv'
}, 'js', { naming: { elem: '%%%', mod: '###' }})
}, 'js', {naming: {elem: '%%%', mod: '###'}})
).eql('a/%%%e1/###mn/a%%%e1###mn###mv.js');

expect(
scheme('nested').path(
new BemCell({
entity: new BemEntityName({
block: 'a',
elem: 'e1',
mod: {name: 'mn', val: 'mv'}
}),
tech: 'js'
}),
{naming: {elem: '%%%', mod: '###'}})
).eql('a/%%%e1/###mn/a%%%e1###mn###mv.js', 'bemCell - api');
});

describe('lib/schemes/nested', function() {
it('should return path for a block', function() {
expect(scheme('nested').path({ block: 'a' }, 'js'))
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');
});

it('should return path for a block with modifier', function() {
Expand All @@ -41,6 +79,12 @@ describe('default', function() {
block: 'a', modName: 'mn', modVal: 'mv'
}, '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() {
Expand All @@ -49,6 +93,14 @@ describe('default', function() {
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');
});

it('should return path for a block with modifier without value', function() {
Expand All @@ -57,12 +109,30 @@ describe('default', function() {
block: 'a', modName: 'mn'
}, 'js')
).eql('a/_mn/a_mn.js');

expect(
scheme('nested').path(
new BemCell({
entity: new BemEntityName({block: 'a', mod: {name: 'mn'}}),
tech: 'js'
})
)
).eql('a/_mn/a_mn.js', 'bemCell - api');
});

it('should return path for elem', function() {
expect(
scheme('nested').path({ block: 'a', elem: 'e1' }, 'js')
scheme('nested').path({block: 'a', elem: 'e1'}, 'js')
).eql('a/__e1/a__e1.js');

expect(
scheme('nested').path(
new BemCell({
entity: new BemEntityName({block: 'a', elem: 'e1'}),
tech: 'js'
})
)
).eql('a/__e1/a__e1.js', 'bemCell - api');
});

it('should return path for modName elem', function() {
Expand All @@ -74,6 +144,19 @@ describe('default', function() {
modVal: 'mv'
}, 'js')
).eql('a/__e1/_mn/a__e1_mn_mv.js');

expect(
scheme('nested').path(
new BemCell({
entity: new BemEntityName({
block: 'a',
elem: 'e1',
mod: {name: 'mn', val: 'mv'}
}),
tech: 'js'
})
)
).eql('a/__e1/_mn/a__e1_mn_mv.js', 'bemCell - api');
});

it('should support optional naming style', function() {
Expand All @@ -83,15 +166,52 @@ describe('default', function() {
elem: 'e1',
modName: 'mn',
modVal: 'mv'
}, 'js', { naming: { elem: '%%%', mod: '###' }})
}, 'js', {naming: {elem: '%%%', mod: '###'}})
).eql('a/%%%e1/###mn/a%%%e1###mn###mv.js');

expect(
scheme('nested').path(
new BemCell({
entity: new BemEntityName({
block: 'a',
elem: 'e1',
mod: {name: 'mn', val: 'mv'}
}),
tech: 'js'
}),
{naming: {elem: '%%%', mod: '###'}}
)
).eql('a/%%%e1/###mn/a%%%e1###mn###mv.js', 'bemCell - api');
});

it('should support layer for BemCell', function() {
expect(
scheme('nested').path(
new BemCell({
entity: new BemEntityName({
block: 'a',
elem: 'e1',
mod: {name: 'mn', val: 'mv'}
}),
tech: 'js',
layer: 'common.blocks'
})
)
).eql('common.blocks/a/__e1/_mn/a__e1_mn_mv.js', 'bemCell - api');
});
});

describe('lib/schemes/flat', function() {
it('should return path for a block', function() {
expect(scheme('flat').path({ block: 'a' }, 'js'))
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');
});

it('should return path for a block with modifier', function() {
Expand All @@ -100,12 +220,28 @@ describe('default', function() {
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');
});

it('should return path for elem', function() {
expect(
scheme('flat').path({ block: 'a', elem: 'e1' }, 'js')
scheme('flat').path({block: 'a', elem: 'e1'}, 'js')
).eql('a__e1.js');

expect(
scheme('flat').path(
new BemCell({
entity: new BemEntityName({block: 'a', elem: 'e1'}),
tech: 'js'
})
)
).eql('a__e1.js', 'bemCell - api');
});

it('should return path for modName elem', function() {
Expand All @@ -117,6 +253,19 @@ describe('default', function() {
modVal: 'mv'
}, 'js')
).eql('a__e1_mn_mv.js');

expect(
scheme('flat').path(
new BemCell({
entity: new BemEntityName({
block: 'a',
elem: 'e1',
mod: {name: 'mn', val: 'mv'}
}),
tech: 'js'
})
)
).eql('a__e1_mn_mv.js', 'bemCell - api');
});

it('should support optional naming style', function() {
Expand All @@ -126,8 +275,38 @@ describe('default', function() {
elem: 'e1',
modName: 'mn',
modVal: 'mv'
}, 'js', { naming: { elem: '%%%', mod: '###' }})
}, 'js', {naming: {elem: '%%%', mod: '###'}})
).eql('a%%%e1###mn###mv.js');

expect(
scheme('flat').path(
new BemCell({
entity: new BemEntityName({
block: 'a',
elem: 'e1',
mod: {name: 'mn', val: 'mv'}
}),
tech: 'js'
}),
{naming: {elem: '%%%', mod: '###'}}
)
).eql('a%%%e1###mn###mv.js', 'bemCell - api');
});

it('should support layer for BemCell', function() {
expect(
scheme('flat').path(
new BemCell({
entity: new BemEntityName({
block: 'a',
elem: 'e1',
mod: {name: 'mn', val: 'mv'}
}),
tech: 'js',
layer: 'common.blocks'
})
)
).eql('common.blocks/a__e1_mn_mv.js', 'bemCell - api');
});
});
});

0 comments on commit 1a4f7e3

Please sign in to comment.