-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parse): add method to parse files
Fixes #33
- Loading branch information
Showing
7 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }]); | ||
}); |