From 7b59cd3329ed1671bf656a217e6a3702e764425c Mon Sep 17 00:00:00 2001 From: Ilya Kashlakov Date: Sun, 3 Jul 2016 16:19:30 +0300 Subject: [PATCH 1/4] Fix bem-config default object --- lib/util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/util.js b/lib/util.js index b4ea017..ffa1df9 100644 --- a/lib/util.js +++ b/lib/util.js @@ -49,7 +49,7 @@ exports.conditionsFromBEMItems = function(items) { * @returns {Object} */ exports.initializeConfig = function(config) { - config = config.extended; + config = config.extended || {}; /** * Returns list of levels From 1e027821c79cba1acb1e62ca44ceccf7c7dd0b27 Mon Sep 17 00:00:00 2001 From: Ilya Kashlakov Date: Sun, 3 Jul 2016 19:27:58 +0300 Subject: [PATCH 2/4] BEM Entity converter for checkers --- lib/util.js | 13 +++++++++++-- package.json | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/util.js b/lib/util.js index ffa1df9..501f04b 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,6 +1,7 @@ 'use strict'; -var Filter = require('./filter'), +var _ = require('lodash'), + Filter = require('./filter'), Criteria = require('./criteria/criteria'), CriteriaCollection = require('./criteria/criteria-collection'); @@ -94,8 +95,16 @@ exports.getFiltersForConditions = function(conditions) { return new Filter(criteria); }), apply = function(item) { + // Prepare BEM entity for check + var prepareBemEntity = function(item) { + var bemEntity = _.omit(item, _.isObject); + return _.reduce(_.values(_.pick(item, _.isObject)), function(result, currentItem) { + return _.merge(result, currentItem); + }, bemEntity); + }; + return filters.some(function(filter) { - return filter.apply(item); + return filter.apply(prepareBemEntity(item)); }); }; diff --git a/package.json b/package.json index 961fc6d..77ccd39 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "JSONStream": "^1.0.7", "bem-config": "git://github.com/bem-incubator/bem-config.git", "bem-naming": "^0.5.1", - "bem-walk": "0.0.4", + "bem-walk": "1.0.0-1", "chalk": "^1.1.1", "coa": "^1.0.1", "flow-tostring": "^1.0.1", From 6ab90b65a1b38f96bfcfec514e4dbbf10a30b9ac Mon Sep 17 00:00:00 2001 From: Ilya Kashlakov Date: Mon, 4 Jul 2016 08:00:56 +0300 Subject: [PATCH 3/4] Test for preparing BEM entity before apply filters --- README.md | 6 +++--- test/lib/util.js | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c41ef44..498431b 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # bem-tools-find [![NPM version](http://img.shields.io/npm/v/bem-tools-find.svg?style=flat)](http://www.npmjs.org/package/bem-tools-find) -[![Build Status](https://travis-ci.org/bem-incubator/bem-tools-find.svg)](https://travis-ci.org/bem-incubator/bem-tools-find) -[![Coverage Status](https://coveralls.io/repos/bem-incubator/bem-tools-find/badge.svg?branch=master&service=github)](https://coveralls.io/github/bem-incubator/bem-tools-find?branch=master) -[![David](https://img.shields.io/david/bem-incubator/bem-tools-find.svg)](https://david-dm.org/bem-incubator/bem-tools-find) +[![Build Status](https://travis-ci.org/bem-contrib/bem-tools-find.svg)](https://travis-ci.org/bem-contrib/bem-tools-find) +[![Coverage Status](https://coveralls.io/repos/bem-contrib/bem-tools-find/badge.svg?branch=master&service=github)](https://coveralls.io/github/bem-contrib/bem-tools-find?branch=master) +[![David](https://img.shields.io/david/bem-contrib/bem-tools-find.svg)](https://david-dm.org/bem-contrib/bem-tools-find) ![Logo](./logo.ico) diff --git a/test/lib/util.js b/test/lib/util.js index e3f48fd..c3ecf72 100644 --- a/test/lib/util.js +++ b/test/lib/util.js @@ -161,5 +161,31 @@ describe('util', function() { it('should return result with apply function', function() { assert.instanceOf(util.getFiltersForConditions().apply, Function); }); + + it('should prepare BEM entity before apply filter', function() { + var Filter = require('../../lib/filter'); + sinon.stub(Filter.prototype, 'apply', function() { + return true; + }); + var apply = util.getFiltersForConditions({ + blocks: ['foo'], + levels: [], + modifiers: [], + elements: [], + techs: [] + }).apply; + apply({ + entity: { + block: 'foo' + }, + tech: 'js' + }); + assert.equal(Filter.prototype.apply.calledOnce, true); + assert.equal(Filter.prototype.apply.calledWith({ + block: 'foo', + tech: 'js' + }), true); + Filter.prototype.apply.restore(); + }); }); }); From bf333b9d38ddcd597ea4de810f9b33c85db17b3c Mon Sep 17 00:00:00 2001 From: Ilya Kashlakov Date: Mon, 4 Jul 2016 17:24:11 +0300 Subject: [PATCH 4/4] Support latest bem-walker format --- lib/filter.js | 2 +- lib/util.js | 13 +------ test/lib/filter.js | 94 ++++++++++++++++++++++++++++++++++++++-------- test/lib/util.js | 26 ------------- 4 files changed, 82 insertions(+), 53 deletions(-) diff --git a/lib/filter.js b/lib/filter.js index 4d3eecb..b1ee22e 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -40,7 +40,7 @@ Filter.prototype = { * @private */ _byBlock: function(item) { - return this._shouldPass(item, 'blocks', 'block'); + return this._shouldPass(item.entity, 'blocks', 'block'); }, /** diff --git a/lib/util.js b/lib/util.js index 501f04b..ffa1df9 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,7 +1,6 @@ 'use strict'; -var _ = require('lodash'), - Filter = require('./filter'), +var Filter = require('./filter'), Criteria = require('./criteria/criteria'), CriteriaCollection = require('./criteria/criteria-collection'); @@ -95,16 +94,8 @@ exports.getFiltersForConditions = function(conditions) { return new Filter(criteria); }), apply = function(item) { - // Prepare BEM entity for check - var prepareBemEntity = function(item) { - var bemEntity = _.omit(item, _.isObject); - return _.reduce(_.values(_.pick(item, _.isObject)), function(result, currentItem) { - return _.merge(result, currentItem); - }, bemEntity); - }; - return filters.some(function(filter) { - return filter.apply(prepareBemEntity(item)); + return filter.apply(item); }); }; diff --git a/test/lib/filter.js b/test/lib/filter.js index 140e599..6ec0c45 100644 --- a/test/lib/filter.js +++ b/test/lib/filter.js @@ -4,18 +4,56 @@ var Filter = require('../../lib/filter'); describe('Filter', function() { describe('apply', function() { - testBySingleCriteria_('blocks', 'block'); testBySingleCriteria_('elements', 'elem'); testBySingleCriteria_('modifiers', 'modName'); testBySingleCriteria_('techs', 'tech'); + it('should pass block item if no info about blocks provided in config', function() { + var filter = new Filter({}), + item = { + entity: { + block: 'foo' + } + }; + assert.equal(filter.apply(item), true); + }); + + it('should pass block item if block matches blocks to search', function() { + var opts = { + blocks: 'foo' + }; + var filter = new Filter(opts), + item = { + entity: { + block: 'foo' + } + }; + assert.equal(filter.apply(item), true); + }); + + it('should not pass block item if block not match blocks to search', function() { + var opts = { + blocks: ['foo'] + }; + var filter = new Filter(opts), + item = { + entity: { + block: 'bar' + } + }; + + assert.equal(filter.apply(item), false); + }); + it('should not pass if matches block, but not matches elem', function() { var filter = new Filter({ blocks: ['foo'], elements: ['bar'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, elem: 'baz' }; @@ -28,7 +66,9 @@ describe('Filter', function() { elements: ['bar'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, elem: 'bar' }; @@ -41,7 +81,9 @@ describe('Filter', function() { modifiers: ['bar'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, modName: 'baz' }; @@ -54,7 +96,9 @@ describe('Filter', function() { modifiers: ['bar'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, modName: 'bar' }; @@ -67,7 +111,9 @@ describe('Filter', function() { techs: ['bar'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, tech: 'baz' }; @@ -80,7 +126,9 @@ describe('Filter', function() { techs: ['bar'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, tech: 'bar' }; @@ -94,7 +142,9 @@ describe('Filter', function() { modifiers: ['fizz'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, elem: 'bar', modName: 'buzz' }; @@ -109,7 +159,9 @@ describe('Filter', function() { modifiers: ['fizz'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, elem: 'bar', modName: 'fizz' }; @@ -125,7 +177,9 @@ describe('Filter', function() { techs: ['buzz'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, elem: 'bar', modName: 'fizz', tech: 'BAZ' @@ -142,7 +196,9 @@ describe('Filter', function() { techs: ['buzz'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, elem: 'bar', modName: 'fizz', tech: 'buzz' @@ -158,7 +214,9 @@ describe('Filter', function() { techs: ['buzz'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, elem: 'bar', tech: 'BAZ' }; @@ -173,7 +231,9 @@ describe('Filter', function() { techs: ['buzz'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, elem: 'bar', tech: 'buzz' }; @@ -188,7 +248,9 @@ describe('Filter', function() { techs: ['buzz'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, modName: 'fizz', tech: 'BAZ' }; @@ -203,7 +265,9 @@ describe('Filter', function() { techs: ['buzz'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, modName: 'fizz', tech: 'buzz' }; diff --git a/test/lib/util.js b/test/lib/util.js index c3ecf72..e3f48fd 100644 --- a/test/lib/util.js +++ b/test/lib/util.js @@ -161,31 +161,5 @@ describe('util', function() { it('should return result with apply function', function() { assert.instanceOf(util.getFiltersForConditions().apply, Function); }); - - it('should prepare BEM entity before apply filter', function() { - var Filter = require('../../lib/filter'); - sinon.stub(Filter.prototype, 'apply', function() { - return true; - }); - var apply = util.getFiltersForConditions({ - blocks: ['foo'], - levels: [], - modifiers: [], - elements: [], - techs: [] - }).apply; - apply({ - entity: { - block: 'foo' - }, - tech: 'js' - }); - assert.equal(Filter.prototype.apply.calledOnce, true); - assert.equal(Filter.prototype.apply.calledWith({ - block: 'foo', - tech: 'js' - }), true); - Filter.prototype.apply.restore(); - }); }); });