Skip to content

Commit

Permalink
Add convert for v1 format
Browse files Browse the repository at this point in the history
  • Loading branch information
skad0 committed Oct 17, 2016
1 parent 8f40f48 commit 352f26a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const isNotSupported = () => {
const converters = {
enb: require('./convert/enb'),
v2: isNotSupported,
v1: isNotSupported,
v1: require('./convert/v1'),
harmony: isNotSupported
};

Expand Down
22 changes: 22 additions & 0 deletions lib/convert/v1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

/**
* Converts normalized declaration to v1 format
*
* @param {Array|Object} decl Source declaration
* @return {Array}
*/
module.exports = function (decl) {
Array.isArray(decl) || (decl = [decl]);

return !decl.length ? [] : decl.reduce((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;
}, []);
};
33 changes: 33 additions & 0 deletions test/convert/v1.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const test = require('ava');

const convert = require('../../lib/convert');

test('must return empty decl', t => {
t.deepEqual(convert([], { format: 'v1' }), []);
});

test('must transform deps to bemdecl', t => {
const input = [
{ entity: { block: 'block1' }, tech: null },
{ entity: { block: 'block1', modName: 'mod1', modVal: true }, tech: null },
{ entity: { block: 'block1', modName: 'mod1', modVal: 'val1' }, tech: null },
{ entity: { block: 'block1', elem: 'elem1' }, tech: null },
{ entity: { block: 'block1', elem: 'elem1', modName: 'mod2', modVal: true }, tech: null },
{ entity: { block: 'block1', elem: 'elem1', modName: 'mod2', modVal: 'val2' }, tech: null }
];
const output = [
{ name: 'block1' },
{ name: 'block1', mods: [{ name: 'mod1', vals: [{ name: true }] }] },
{ name: 'block1', mods: [{ name: 'mod1', vals: [{ name: 'val1' }] }] },
{ name: 'block1', elems: [{ name: 'elem1' }] },
{ name: 'block1', elems: [{ name: 'elem1', mods: [{ name: 'mod2', vals: [{ name: true }] }] }] },
{ name: 'block1', elems: [{ name: 'elem1', mods: [{ name: 'mod2', vals: [{ name: 'val2' }] }] }] }
];

t.deepEqual(
convert(input, { format: 'v1' }),
output
);
});

0 comments on commit 352f26a

Please sign in to comment.