-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[wip] Add convert for v1 format #70
Conversation
module.exports = function (decl) { | ||
Array.isArray(decl) || (decl = [decl]); | ||
|
||
if (decl.length > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!decl.length) return [];
to avoid extra indentation
|
||
decl.forEach(function (dep) { | ||
const entity = dep.entity; | ||
let item = { name: entity.block }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let -> const
if (decl.length > 0) { | ||
const res = []; | ||
|
||
decl.forEach(function (dep) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return decl.reduce(fn, []);
to get rid of res
const entity = dep.entity; | ||
let item = { name: entity.block }; | ||
|
||
if (entity.elem) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
never tested but looks like all this stuff can be rewritten like this:
Array.isArray(decl) || (decl = [decl]);
return !decl.length ? [] : decl.reduce(function(acc, dep) {
const entity = dep.entity;
const item = { name: entity[entity.elem ? 'elem' : 'block'] };
entity.modName && (item.mods = [{ name: entity.modName }]);
entity.modVal && (item.mods[0].vals = [{ name: entity.modVal }]);
acc.push(entity.elem ? { name: entity.block, elems: [item] } : item);
return acc;
}, []);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
как-то тестов маловато
const output = [ | ||
{ name: 'block1' }, | ||
{ name: 'block1', mods: [{ name: 'mod1', vals: [{ name: true }] }] }, | ||
{ name: 'block1', mods: [{ name: 'mod1', vals: [{ name: 'val1' }] }] }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
может склеивать такое сразу?
const output = [ | ||
{ name: 'block1' }, | ||
{ | ||
name: 'block1', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А разве 2 объекта должно быть?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Я по наитию ориентировался)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Надеялся, что кто-то про него что-то знает)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/bem-sdk/bem-decl/blob/master/test/normalize/elems.test.js#L6-L20 Судя по этому тесту — первый объект лишний.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@blond Интересно, а как таким образом описывать элементы без блока и модификаторы без элемента и блока? Никак штоле?
name: 'block1', | ||
elems: [ | ||
{ | ||
name: 'elem1', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
очень странно, что блок мы вынесли в отдельный объект, а элемент нет. Это реально такой хороший формат?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zxqfox не понял тебя. Формат реально не хороший :)
8150307
to
4504c7f
Compare
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не хватает тестов для разных скоупов: например, несколько разных блоков.
t.deepEqual(convert([], { format: 'v1' }), []); | ||
}); | ||
|
||
test('must split elems of one block', t => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
split -> group?
name: 'block1', | ||
elems: [ | ||
{ | ||
name: 'elem1', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zxqfox не понял тебя. Формат реально не хороший :)
|
||
const stringify = require('../../lib/stringify'); | ||
|
||
test('should stringify enb declaration', t => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enb -> v1
@@ -2,10 +2,6 @@ const test = require('ava'); | |||
|
|||
const stringify = require('../../lib/stringify'); | |||
|
|||
test('should throws error if no format given', t => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Почему тест исчез? Вроде годный
🆙 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Тесты 🆗 .
По коду не нравится много find
, лишних обходов и получение объектов через [0]
.
Написал немного замечаний.
const item = { name: entity[entity.elem ? 'elem' : 'block'] }; | ||
|
||
entity.modName && (item.mods = [{ name: entity.modName }]); | ||
entity.modVal && (item.mods[0].vals = [entity.modVal]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Может { name: entity.modName }
вынести в переменную modItem
, чтобы не обращаться к mods[0]
?
|
||
const result = entity.elem ? { name: entity.block, elems: [item] } : item; | ||
|
||
if (result.elems && entity.elem) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Зачем проверка на entity.elem
, если у result
выставится elems
, только если есть entity.elem
.
|
||
if (result.elems && entity.elem) { | ||
// If exists block with existing elems property | ||
const curBlock = acc.find( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Зачем переносы строк?
if (curBlock) { | ||
if (curBlock.elems) { | ||
// check if elem exists | ||
const curElem = curBlock.elems.find(elem => elem.name === entity.elem); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Так как мы уже проходились ранее по элементам, которые добавили, мы можем знать, какие были добавлены, а какие нет.
Чтобы не ходить каждый раз заново по наполняемому массиву можно хранить карту, где ключ это скоуп БЭМ-сущности, а значение ссылка на объект БЭМ-сущности внутри acc
.
@skad0 Tests were not passed. Check it please? |
{ elem: 'elem', mods: { m2:'v2' } } | ||
] | ||
} | ||
], { format: 'v2' })); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wtf?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this pr now is in wip status
@skad0 Heya! Would be nice to see this in master |
@zxqfox will be happy if you can give me some advices tomorrow to complete this |
There is a try to create universal transformer based on which all other formatter can work. |
@blond @zxqfox
for #78