Skip to content

Commit

Permalink
Convert renamed to format
Browse files Browse the repository at this point in the history
  • Loading branch information
skad0 committed Dec 11, 2016
1 parent 474c72c commit 80bb7b4
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 31 deletions.
14 changes: 7 additions & 7 deletions lib/convert.js → lib/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ const isNotSupported = () => {
);
};

const converters = {
enb: require('./convert/enb'),
const formatters = {
enb: require('./format/enb'),
v2: isNotSupported,
v1: require('./convert/v1'),
v1: require('./format/v1'),
harmony: isNotSupported
};

/**
* Convert normalized declaration to target format
* Formats normalized declaration to target format
*
* @param {Array|Object} decl normalized declaration
* @param {Object} opts Addtional options
* @param {String} opts.format format format
* @return {Array} Array with converted declaration
* @return {Array} Array with formatted declaration
*/
module.exports = function (decl, opts) {
opts || (opts = {});
Expand All @@ -30,9 +30,9 @@ module.exports = function (decl, opts) {

assert(format, 'You must declare target format');

if (!converters[format]) {
if (!formatters[format]) {
throw new Error('Unknown format');
}

return converters[format](decl);
return formatters[format](decl);
};
2 changes: 1 addition & 1 deletion lib/convert/enb.js → lib/format/enb.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

/**
* Convert normalized declaration to enb format
* Formats normalized declaration to enb format
*
* @param {Array|Object} decl Source declaration
* @return {Array}
Expand Down
2 changes: 1 addition & 1 deletion lib/convert/v1.js → lib/format/v1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

/**
* Converts normalized declaration to v1 format
* Formats normalized declaration to v1 format
*
* @param {Array|Object} decl Source declaration
* @return {Array}
Expand Down
6 changes: 3 additions & 3 deletions lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const assert = require('assert');

const convert = require('./convert');
const formatter = require('./format');

/**
* Create string representation of declaration
Expand All @@ -20,8 +20,8 @@ module.exports = function (decl, opts) {

Array.isArray(decl) || (decl = [decl]);

const formatedDecl = convert(decl, { format });
const jsonStr = JSON.stringify(formatedDecl, null, 4);
const formattedDecl = formatter(decl, { format });
const jsonStr = JSON.stringify(formattedDecl, null, 4);

let exportsStr = '';

Expand Down
16 changes: 8 additions & 8 deletions test/convert/enb.test.js → test/format/enb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

const test = require('ava');

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

test('should throw exception if no format given', t => {
t.throws(() => convert({ entity: { block: 'block' }, tech: null }), 'You must declare target format');
t.throws(() => format({ entity: { block: 'block' }, tech: null }), 'You must declare target format');
});

test('should convert to enb format', t => {
t.deepEqual(convert({ entity: { block: 'block' }, tech: null }, { format: 'enb' }), [{ block: 'block' }]);
test('should format to enb format', t => {
t.deepEqual(format({ entity: { block: 'block' }, tech: null }, { format: 'enb' }), [{ block: 'block' }]);
});

test('should convert with elem', t => {
test('should format with elem', t => {
t.deepEqual(
convert([
format([
{ entity: { block: 'block' }, tech: null },
{ entity: { block: 'block', elem: 'elem' }, tech: null }
], { format: 'enb' }),
Expand All @@ -25,9 +25,9 @@ test('should convert with elem', t => {
);
});

test('should convert with mod', t => {
test('should format with mod', t => {
t.deepEqual(
convert([
format([
{ entity: { block: 'block', modName: 'mod', modVal: 'val' }, tech: null }
], { format: 'enb' }),
[{ block: 'block', mod: 'mod', val: 'val' }]
Expand Down
16 changes: 8 additions & 8 deletions test/convert/v1.test.js → test/format/v1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

const test = require('ava');

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

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

test('must group elems of one block', t => {
Expand All @@ -19,7 +19,7 @@ test('must group elems of one block', t => {
];

t.deepEqual(
convert(input, { format: 'v1' }),
format(input, { format: 'v1' }),
output
);
});
Expand All @@ -41,7 +41,7 @@ test('must group mods of one block', t => {
];

t.deepEqual(
convert(input, { format: 'v1' }),
format(input, { format: 'v1' }),
output
);
});
Expand All @@ -62,7 +62,7 @@ test('must group vals of mods block', t => {
];

t.deepEqual(
convert(input, { format: 'v1' }),
format(input, { format: 'v1' }),
output
);
});
Expand All @@ -88,7 +88,7 @@ test('must group elem mods of block', t => {
];

t.deepEqual(
convert(input, { format: 'v1' }),
format(input, { format: 'v1' }),
output
);
});
Expand All @@ -114,14 +114,14 @@ test('must group vals of elem mods', t => {
];

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

test('should create full entity with mods', t => {
t.deepEqual(
convert({ entity: { block: 'block1', modName: 'mod1', modVal: 'val1' } }, { format: 'v1' }),
format({ entity: { block: 'block1', modName: 'mod1', modVal: 'val1' } }, { format: 'v1' }),
[{
name: 'block1',
mods: [{
Expand Down
4 changes: 1 addition & 3 deletions test/stringify/v1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ test('should stringify v1 declaration', t => {
name: 'elem',
mods: [{
name: 'mod',
vals: [{
name: 'val'
}]
vals: ['val']
}]
}]
}];
Expand Down

0 comments on commit 80bb7b4

Please sign in to comment.