Skip to content
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

Closed
wants to merge 8 commits into from
Closed

[wip] Add convert for v1 format #70

wants to merge 8 commits into from

Conversation

skad0
Copy link
Member

@skad0 skad0 commented Oct 3, 2016

module.exports = function (decl) {
Array.isArray(decl) || (decl = [decl]);

if (decl.length > 0) {
Copy link
Member

@tadatuta tadatuta Oct 3, 2016

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 };
Copy link
Member

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) {
Copy link
Member

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) {
Copy link
Member

@tadatuta tadatuta Oct 4, 2016

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;
}, []);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you.

Copy link
Contributor

@qfox qfox left a 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' }] }] },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

может склеивать такое сразу?

@skad0
Copy link
Member Author

skad0 commented Oct 10, 2016

up, review needed
@zxqfox @tadatuta @blond

@skad0
Copy link
Member Author

skad0 commented Oct 12, 2016

up
/cc @zxqfox @tadatuta @blond

const output = [
{ name: 'block1' },
{
name: 'block1',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А разве 2 объекта должно быть?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я по наитию ориентировался)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Надеялся, что кто-то про него что-то знает)

Copy link
Contributor

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 Судя по этому тесту — первый объект лишний.

Copy link
Contributor

@qfox qfox Oct 12, 2016

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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

очень странно, что блок мы вынесли в отдельный объект, а элемент нет. Это реально такой хороший формат?

Copy link
Member Author

@skad0 skad0 Oct 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zxqfox не понял тебя. Формат реально не хороший :)

@skad0
Copy link
Member Author

skad0 commented Oct 17, 2016

up
@zxqfox @blond @tadatuta
@blond @blond @blond ^_^

@skad0 skad0 force-pushed the convert_v1 branch 2 times, most recently from 8150307 to 4504c7f Compare October 19, 2016 09:49
@tadatuta
Copy link
Member

tadatuta commented Dec 4, 2016

  1. нужно отребейзить
  2. нужно исправить мессадж второго коммита (convertation -> conversion)

Copy link
Member

@blond blond left a 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 => {
Copy link
Member

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',
Copy link
Member

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 => {
Copy link
Member

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 => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему тест исчез? Вроде годный

@skad0
Copy link
Member Author

skad0 commented Dec 11, 2016

🆙
@blond, по поводу тестов, не совсем понял какие кейсы еще не покрыты

Copy link
Member

@blond blond left a 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]);
Copy link
Member

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) {
Copy link
Member

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(
Copy link
Member

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Так как мы уже проходились ранее по элементам, которые добавили, мы можем знать, какие были добавлены, а какие нет.

Чтобы не ходить каждый раз заново по наполняемому массиву можно хранить карту, где ключ это скоуп БЭМ-сущности, а значение ссылка на объект БЭМ-сущности внутри acc.

@qfox
Copy link
Contributor

qfox commented Dec 23, 2016

@skad0 Tests were not passed. Check it please?

@qfox qfox mentioned this pull request Dec 23, 2016
{ elem: 'elem', mods: { m2:'v2' } }
]
}
], { format: 'v2' }));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wtf?

Copy link
Member Author

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 skad0 changed the title Add convert for v1 format [wip] Add convert for v1 format Dec 24, 2016
@qfox
Copy link
Contributor

qfox commented Jan 23, 2017

@skad0 Heya! Would be nice to see this in master

@skad0
Copy link
Member Author

skad0 commented Jan 23, 2017

@zxqfox will be happy if you can give me some advices tomorrow to complete this

@skad0
Copy link
Member Author

skad0 commented Jan 23, 2017

There is a try to create universal transformer based on which all other formatter can work.

@skad0
Copy link
Member Author

skad0 commented Feb 2, 2017

#92

@skad0 skad0 closed this Feb 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants