-
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
Changes from all commits
0819e18
c1372a8
474c72c
80bb7b4
0cdd4bb
b766e43
b7f4afc
09487dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const no = require('./lib/normalize'); | ||
console.log(JSON.stringify( | ||
{ | ||
block: 'block', | ||
elems: [ | ||
{ elem: 'elem', mods: { m1:'v1' } }, | ||
{ elem: 'elem2' }, | ||
{ elem: 'elem', mods: { m2:'v2' } } | ||
] | ||
}, null, 4 | ||
)); | ||
console.log('==========='); | ||
console.log(no([ | ||
{ | ||
block: 'block', | ||
elems: [ | ||
{ elem: 'elem', mods: { m1:'v1' } }, | ||
{ elem: 'elem2' }, | ||
{ elem: 'elem', mods: { m2:'v2' } } | ||
] | ||
} | ||
], { format: 'v2' })); | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Formats normalized declaration to v1 format | ||
* | ||
* @param {Array|Object} decl Source declaration | ||
* @return {Array} | ||
*/ | ||
module.exports = function (decl) { | ||
Array.isArray(decl) || (decl = [decl]); | ||
|
||
// previous block in declaration | ||
let prevBlock = ''; | ||
|
||
return !decl.length ? [] : decl.reduce((acc, dep) => { | ||
const canGroup = dep.entity.block === prevBlock; | ||
const entity = dep.entity; | ||
const item = { name: entity[entity.elem ? 'elem' : 'block'] }; | ||
|
||
if (entity.modName) { | ||
item.mods = [{ | ||
name: entity.modName, | ||
vals: [entity.modVal] | ||
}]; | ||
} | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Зачем проверка на |
||
// If exists block | ||
const curBlock = acc.find( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Зачем переносы строк? |
||
curEntity => curEntity.name === entity.block | ||
); | ||
|
||
if (curBlock && canGroup) { | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Так как мы уже проходились ранее по элементам, которые добавили, мы можем знать, какие были добавлены, а какие нет. Чтобы не ходить каждый раз заново по наполняемому массиву можно хранить карту, где ключ это скоуп БЭМ-сущности, а значение ссылка на объект БЭМ-сущности внутри |
||
curElem || curBlock.elems.push(result.elems[0]); | ||
} else { | ||
curBlock.elems = result.elems; | ||
} | ||
} else { | ||
acc.push(result); | ||
} | ||
|
||
// If such element already exists and we have mod | ||
const modsAddition = acc.find(curEntity => { | ||
const curElem = curEntity.elems ? | ||
curEntity.elems.find(elem => elem.name === entity.elem) : false; | ||
return curEntity.name === entity.block && curElem; | ||
}); | ||
|
||
if (entity.modName) { | ||
const curElem = modsAddition.elems.find(elem => elem.name === entity.elem); | ||
if (curElem.mods) { | ||
// have an array of mods in elem | ||
// check if such mod already exists | ||
const curMod = curElem.mods.find( | ||
mod => mod.name === entity.modName | ||
); | ||
if (curMod) { | ||
// check if val already exists | ||
const curVal = curMod.vals.find(val => | ||
val === entity.modVal | ||
); | ||
|
||
curVal || curMod.vals.push(result.elems[0].mods[0].vals[0]); | ||
} else { | ||
// add new mod | ||
curElem.mods.push(result.elems[0].mods[0]); | ||
} | ||
} | ||
} | ||
} else if (entity.modName) { | ||
const curBlock = acc.find(curEntity => curEntity.name === entity.block); | ||
// if block with mods prop exists search for such mod | ||
if (curBlock && canGroup) { | ||
if (curBlock.mods) { | ||
const curMod = curBlock.mods.find(mod => mod.name === entity.modName); | ||
if (curMod) { | ||
// if such mod exists push new val | ||
curMod.vals.push(result.mods[0].vals[0]); | ||
} else { | ||
// if no such mod push new mod | ||
curBlock.mods.push(result.mods[0]); | ||
} | ||
} else { | ||
curBlock.mods = result.mods; | ||
} | ||
} else { | ||
acc.push(result); | ||
} | ||
} else { | ||
acc.push(result); | ||
} | ||
// save previous block | ||
prevBlock = dep.entity.block; | ||
|
||
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.
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