Skip to content

Commit

Permalink
fixup! wip
Browse files Browse the repository at this point in the history
  • Loading branch information
skad0 committed Oct 1, 2016
1 parent 831438c commit dc67cbf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
9 changes: 6 additions & 3 deletions lib/convert.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const assert = require('assert');

const converters = {
v2: function (decl) {
Array.isArray(decl) || (decl = [decl]);
Expand Down Expand Up @@ -30,9 +32,10 @@ const converters = {
};

module.exports = function (decl, opts) {
const defaultOpts = { version: 'v2' };
opts || (opts = defaultOpts);
const version = opts.version || defaultOpts.version;
opts || (opts = {});
assert(opts.version, 'You must declare target version');

const version = opts.version;

if (!converters.hasOwnProperty(version)) {
throw new Error('Unknown format');
Expand Down
9 changes: 6 additions & 3 deletions lib/stringify.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
'use strict';

const assert = require('assert');

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

module.exports = function (decl, opts) {
const defaultOpts = { version: 'v2' };
opts || (opts = defaultOpts);
const version = opts.version || defaultOpts.version;
opts || (opts = {});
assert(opts.version, 'You must declare target version');

const version = opts.version;

let result = '';

Expand Down
10 changes: 7 additions & 3 deletions test/convert/v2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ const test = require('ava');

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

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

test('should convert to v2 format', t => {
t.deepEqual(convert({ block: 'block' }), [{ block: 'block' }]);
t.deepEqual(convert({ block: 'block' }, { version: 'v2' }), [{ block: 'block' }]);
});

test('should convert with elem', t => {
t.deepEqual(
convert([
{ block: 'block' },
{ block: 'block', elem: 'elem' }
]),
], { version: 'v2' }),
[
{ block: 'block' },
{ block: 'block', elem: 'elem' }
Expand All @@ -25,7 +29,7 @@ test('should convert with mod', t => {
t.deepEqual(
convert([
{ block: 'block', modName: 'mod', modVal: 'val' }
]),
], { version: 'v2' }),
[{ block: 'block', mod: 'mod', val: 'val' }]
)
});
6 changes: 5 additions & 1 deletion test/stringify/v2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ const test = require('ava');

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

test('should throws error if no version given', t => {
t.throws(() => stringify({ block: 'block' }), 'You must declare target version');
});

test('should stringify v2 declaration', t => {
const obj = [{ block: 'block', elem: 'elem', mod: 'mod', val: 'val' }];
t.deepEqual(
stringify({ block: 'block', elem: 'elem', modName: 'mod', modVal: 'val' }),
stringify({ block: 'block', elem: 'elem', modName: 'mod', modVal: 'val' }, { version: 'v2' }),
'module.exports = ' + JSON.stringify(obj, null, 4) + ';\n'
);
});

0 comments on commit dc67cbf

Please sign in to comment.