Skip to content

Commit

Permalink
Normalize arrangement
Browse files Browse the repository at this point in the history
  • Loading branch information
skad0 committed Oct 18, 2016
1 parent 8f40f48 commit 72d8b85
Show file tree
Hide file tree
Showing 35 changed files with 145 additions and 184 deletions.
16 changes: 2 additions & 14 deletions lib/index.js
@@ -1,24 +1,12 @@
'use strict';

const normalize = {
v1: require('./normalize'),
v2: require('./normalize2'),
harmony: require('./normalize-harmony'),
enb: require('./normalize2')
};

module.exports = {
normalize: (decl, opts) => {
opts || (opts = {});

return opts.format ? normalize[opts.format](decl) : normalize.v2(decl);
},
normalize: require('./normalize'),
merge: require('./merge'),
subtract: require('./subtract'),
intersect: require('./intersect'),
parse: require('./parse'),
assign: require('./assign'),
load: require('./load'),
stringify: require('./stringify'),
normalizer: format => (normalize[format] || normalize.v2)
stringify: require('./stringify')
};
92 changes: 10 additions & 82 deletions lib/normalize.js
@@ -1,88 +1,16 @@
'use strict';

const naming = require('bem-naming');

module.exports = function (decl) {
const res = [];
const hash = {};

function add(entity) {
const str = naming.stringify(entity);

if (!hash[str]) {
res.push({ entity: entity, tech: null });
}

hash[str] = true;
}

if (!decl) { return []; }
if (!Array.isArray(decl)) { decl = [decl]; }

for (let i = 0; i < decl.length; ++i) {
const entity = decl[i];
const block = entity.name;
const mods = entity.mods;
const elems = entity.elems;

add({ block: block });

if (mods) {
normalizeMods(block, mods);
}

if (elems) {
for (let j = 0; j < elems.length; ++j) {
const elem = elems[j];
const elemName = elem.name;
const elemMods = elem.mods;

add({ block: block, elem: elemName });

if (elemMods) {
normalizeMods(block, elemName, elemMods);
}
}
}
}

function normalizeMods(block, elem, mods) {
const isElem = arguments.length === 3;

if (!isElem) {
mods = elem;
}

for (let i = 0; i < mods.length; ++i) {
const mod = mods[i];
const vals = mod.vals;
const l = vals && vals.length;

let resItem;

if (l) {
for (let j = 0; j < l; ++j) {
resItem = {
block: block,
modName: mod.name, modVal: vals[j].name
};

isElem && (resItem.elem = elem);

add(resItem);
}
} else {
resItem = {
block: block,
modName: mod.name, modVal: true
};
const normalizer = {
v1: require('./normalize/v1'),
v2: require('./normalize/v2'),
harmony: require('./normalize/harmony'),
enb: require('./normalize/v2')
};

isElem && (resItem.elem = elem);
module.exports = (decl, opts) => {
opts || (opts = {});

add(resItem);
}
}
}
const format = opts.format || 'v2';

return res;
return normalizer[format](decl);
};
File renamed without changes.
80 changes: 80 additions & 0 deletions lib/normalize/v1.js
@@ -0,0 +1,80 @@
'use strict';

const naming = require('bem-naming');

module.exports = function (decl) {
const res = [];
const hash = {};

function add(entity) {
const str = naming.stringify(entity);

if (!hash[str]) {
res.push({ entity: entity, tech: null });
}

hash[str] = true;
}

if (!decl) { return []; }
if (!Array.isArray(decl)) { decl = [decl]; }

for (let i = 0; i < decl.length; ++i) {
const entity = decl[i];
const block = entity.name;
const mods = entity.mods;
const elems = entity.elems;

add({ block: block });

if (mods) {
normalizeMods(block, mods);
}

if (elems) {
for (let j = 0; j < elems.length; ++j) {
const elem = elems[j];
const elemName = elem.name;
const elemMods = elem.mods;

add({ block: block, elem: elemName });

if (elemMods) {
normalizeMods(block, elemName, elemMods);
}
}
}
}

function normalizeMods(block, elem, mods) {
const isElem = arguments.length === 3;

if (!isElem) {
mods = elem;
}

for (let i = 0; i < mods.length; ++i) {
const mod = mods[i];
const vals = mod.vals;
const hasVals = vals && vals.length;

let resItem;
let j = 0;

do {
resItem = {
block: block,
modName: mod.name,
modVal: hasVals ? vals[j].name : true
};

isElem && (resItem.elem = elem);

add(resItem);
++j;
} while (j < hasVals);
}
}

return res;
};
File renamed without changes.
24 changes: 12 additions & 12 deletions lib/parse.js
Expand Up @@ -5,15 +5,6 @@ const assert = require('assert');
const nodeEval = require('node-eval');

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

const normalizers = {
v1: normalize,
v2: normalize2,
harmony: normalizeHarmony
};

/**
* Parses BEMDECL file data
Expand All @@ -24,22 +15,31 @@ const normalizers = {
module.exports = function parse(bemdecl) {
assert(typeof bemdecl === 'object' || typeof bemdecl === 'string', 'Bemdecl must be String or Object');

const normalize = require('.').normalize;

const declaration = (typeof bemdecl === 'string') ? nodeEval(bemdecl) : bemdecl;
const hasOwn = Object.prototype.hasOwnProperty.bind(Object(declaration));
const format = declaration.format || detect(declaration);

let decl;

switch (format) {
case 'v1':
assert(hasOwn('blocks'), 'Invalid declaration format');
return normalizers.v1(declaration.blocks);
decl = declaration.blocks;
break;
case 'v2':
case 'enb':
assert(hasOwn('decl') || hasOwn('deps'), 'Invalid format of declaration.');
return normalizers.v2(declaration.decl || declaration.deps);
decl = declaration.decl || declaration.deps;
break;
case 'harmony':
assert(hasOwn('decl'), 'Invalid format of declaration.');
return normalizers.harmony(declaration.decl);
decl = declaration.decl;
break;
default:
throw new Error('Unknown BEMDECL format.');
}

return normalize(decl, { format: format });
};
35 changes: 0 additions & 35 deletions test/index.test.js
Expand Up @@ -31,41 +31,6 @@ test('should support `BEMDECL 2.0` format', t => {
t.deepEqual(decl, [{ entity: decls.normalized, tech: null }]);
});

test('should have `normalizer` method', t => {
t.truthy(typeof bemDecl.normalizer === 'function');
});

test('normalizer should support default value as `normalize`', t => {
var decl = bemDecl.normalizer('v1')(decls.v1);

t.deepEqual(decl, [{ entity: decls.normalized, tech: null }]);
});

test('should support `BEMDECL 1.0` format through normalizer', t => {
var decl = bemDecl.normalizer('v1')(decls.v1);

t.deepEqual(decl, [{ entity: decls.normalized, tech: null }]);
});

// TODO: define name of format
test('should have support `BEMDECL x.0` format through normalizer', t => {
var decl = bemDecl.normalizer('v2')(decls.v2);

t.deepEqual(decl, [{ entity: decls.normalized, tech: null }]);
})

test('should support `BEMDECL 2.0` format through normalizer', t => {
var decl = bemDecl.normalizer('harmony')(decls.v2);

t.deepEqual(decl, [{ entity: decls.normalized, tech: null }]);
});

test('should support uncorrect normalizer arg with default result', t => {
var decl = bemDecl.normalizer('levoe')(decls.v2);

t.deepEqual(decl, [{ entity: decls.normalized, tech: null }]);
});

test('should have `merge` method', t => {
t.truthy(typeof bemDecl.merge === 'function');
});
Expand Down
2 changes: 1 addition & 1 deletion test/normalize-harmony/block.test.js
@@ -1,7 +1,7 @@
'use strict';

const test = require('ava');
const normalize = require('../../lib/normalize-harmony');
const normalize = require('../../lib/normalize/harmony');

test('should support block', t => {
var block = { block: 'block' };
Expand Down
2 changes: 1 addition & 1 deletion test/normalize-harmony/common.test.js
@@ -1,7 +1,7 @@
'use strict';

const test = require('ava');
const normalize = require('../../lib/normalize-harmony');
const normalize = require('../../lib/normalize/harmony');

test('should support undefined', t => {
t.deepEqual(normalize(), []);
Expand Down
2 changes: 1 addition & 1 deletion test/normalize-harmony/elem.test.js
@@ -1,7 +1,7 @@
'use strict';

const test = require('ava');
const normalize = require('../../lib/normalize-harmony');
const normalize = require('../../lib/normalize/harmony');

test('should support elem', t => {
const decl = { block: 'block', elem: 'elem' };
Expand Down
2 changes: 1 addition & 1 deletion test/normalize-harmony/elems.test.js
@@ -1,7 +1,7 @@
'use strict';

const test = require('ava');
const normalize = require('../../lib/normalize-harmony');
const normalize = require('../../lib/normalize/harmony');

test('should support strings', t => {
const decl = {
Expand Down
2 changes: 1 addition & 1 deletion test/normalize-harmony/mix.test.js
@@ -1,7 +1,7 @@
'use strict';

const test = require('ava');
const normalize = require('../../lib/normalize-harmony');
const normalize = require('../../lib/normalize/harmony');

test('should support mix', t => {
const decl = {
Expand Down
2 changes: 1 addition & 1 deletion test/normalize-harmony/mods.test.js
@@ -1,7 +1,7 @@
'use strict';

const test = require('ava');
const normalize = require('../../lib/normalize-harmony');
const normalize = require('../../lib/normalize/harmony');

test('should support shortcut for bool mod', t => {
const decl = { block: 'block', modName: 'mod' };
Expand Down
2 changes: 1 addition & 1 deletion test/normalize-harmony/scope.test.js
@@ -1,7 +1,7 @@
'use strict';

const test = require('ava');
const normalize = require('../../lib/normalize-harmony');
const normalize = require('../../lib/normalize/harmony');

test('should support mod in block scope', t => {
const decl = {
Expand Down
2 changes: 1 addition & 1 deletion test/normalize/common.test.js
@@ -1,7 +1,7 @@
'use strict';

const test = require('ava');
const normalize = require('../../lib/normalize');
const normalize = require('../../lib/normalize/v1');

test('should support undefined', t => {
t.deepEqual(normalize(), []);
Expand Down
2 changes: 1 addition & 1 deletion test/normalize/elems.test.js
@@ -1,7 +1,7 @@
'use strict';

const test = require('ava');
const normalize = require('../../lib/normalize');
const normalize = require('../../lib/normalize/v1');

test('should support arrays', t => {
const decl = {
Expand Down
2 changes: 1 addition & 1 deletion test/normalize/mods.test.js
@@ -1,7 +1,7 @@
'use strict';

const test = require('ava');
const normalize = require('../../lib/normalize');
const normalize = require('../../lib/normalize/v1');

test('should support objects', t => {
const decl = { name: 'block', mods: [{ name: 'mod', vals: [{ name: 'val' }] }] };
Expand Down
2 changes: 1 addition & 1 deletion test/normalize2/block-mod.test.js
@@ -1,7 +1,7 @@
'use strict';

const test = require('ava');
const normalize = require('../../lib/normalize2');
const normalize = require('../../lib/normalize/v2');

test('should support mod', t => {
const decl = {
Expand Down

0 comments on commit 72d8b85

Please sign in to comment.