Skip to content

Commit

Permalink
feat(parse): add method to parse files
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
qfox committed May 11, 2016
1 parent 70d98c4 commit a6f95f0
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ module.exports = {
},
merge: require('./merge'),
subtract: require('./subtract'),
intersect: require('./intersect')
intersect: require('./intersect'),
parse: require('./parse')
};
39 changes: 39 additions & 0 deletions lib/parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

const normalize = require('./normalize');
const normalize2 = require('./normalize-harmony');

const normalizers = {
'1.0': normalize,
'2.0': normalize2,
next: normalize2
};

/**
* Parses BEMDECL file data
*
* @param {{version: string, decl: BEMEntityPart[]}|{blocks: BEMEntityPart[]}} bemdecl [description]
* @returns {[type]} [description]
*/
module.exports = function parse(bemdecl) {
const hasOwn = Object.prototype.hasOwnProperty.bind(Object(bemdecl));

let version, decl;

// Legacy 1.0 format
if (hasOwn('blocks')) {
version = '1.0';
decl = bemdecl.blocks;
} else if (hasOwn('version') && hasOwn('decl')) {
version = bemdecl.version;
decl = bemdecl.decl;
}

const normalizer = normalizers[version];

if (!decl || !normalizer) {
throw new Error('Unknown BEMDECL format.');
}

return normalizer(decl);
};
4 changes: 4 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ test('should have `merge` method', t => {
test('should have `subtract` method', t => {
t.truthy(typeof bemDecl.subtract === 'function');
});

test('should have `parse` method', t => {
t.truthy(typeof bemDecl.parse === 'function');
});
12 changes: 12 additions & 0 deletions test/parse/common.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const test = require('ava');
const parse = require('../..').parse;

test('should throw if undefined', t => {
t.throws(() => parse(), /Unknown BEMDECL format/);
});

test('should throw if unsupported', t => {
t.throws(() => parse({ version: 'unknown', components: [] }), /Unknown BEMDECL format/);
});
13 changes: 13 additions & 0 deletions test/parse/legacy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const test = require('ava');
const parse = require('../..').parse;

test('should parse empty legacy blocks property', t => {
t.deepEqual(parse({ blocks: [] }), []);
});

test('should parse blocks property with single entity', t => {
t.deepEqual(parse({ blocks: [{ name: 'doesnt-matter' }] }),
[{ block: 'doesnt-matter' }]);
});
13 changes: 13 additions & 0 deletions test/parse/v1.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const test = require('ava');
const parse = require('../..').parse;

test('should parse empty legacy blocks property', t => {
t.deepEqual(parse({ version: '1.0', decl: [] }), []);
});

test('should parse blocks property with single entity', t => {
t.deepEqual(parse({ version: '1.0', decl: [{ name: 'doesnt-matter' }] }),
[{ block: 'doesnt-matter' }]);
});
13 changes: 13 additions & 0 deletions test/parse/v2.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const test = require('ava');
const parse = require('../..').parse;

test('should parse empty legacy blocks property', t => {
t.deepEqual(parse({ version: '2.0', decl: [] }), []);
});

test('should parse blocks property with single entity', t => {
t.deepEqual(parse({ version: '2.0', decl: [{ block: 'doesnt-matter' }] }),
[{ block: 'doesnt-matter' }]);
});

0 comments on commit a6f95f0

Please sign in to comment.