From 76eff0e344d9b9dd41fbac6516e01be7e65fe02a Mon Sep 17 00:00:00 2001 From: Vasiliy Loginevskiy Date: Wed, 3 May 2017 23:53:02 +0300 Subject: [PATCH] Add mod-name to modName (camelCase) plugin --- lib/plugins.js | 9 +++++++++ package.json | 1 + test/plugins.js | 26 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/lib/plugins.js b/lib/plugins.js index 1acd957b..4b294829 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -1,3 +1,4 @@ +var camelCase = require('camel-case'); var helpers = require('./helpers'); var styleToObj = helpers.styleToObj; var valToStr = helpers.valToStr; @@ -6,6 +7,13 @@ module.exports.copyMods = () => function copyMods(jsx, bemjson) { bemjson.mods && Object.assign(jsx.props, bemjson.mods); }; +module.exports.camelCaseProps = () => function camelCaseProps(jsx) { + jsx.props = Object.keys(jsx.props).reduce((acc, propKey) => { + acc[camelCase(propKey)] = jsx.props[propKey]; + return acc; + }, {}); +}; + module.exports.copyCustomFields = () => function copyCustomFields(jsx, bemjson) { var blackList = ['content', 'block', 'elem', 'mods', 'tag', 'js']; @@ -39,6 +47,7 @@ module.exports.keepWhiteSpaces = () => function keepWhiteSpaces(jsx) { module.exports.defaultPlugins = [ module.exports.keepWhiteSpaces, module.exports.copyMods, + module.exports.camelCaseProps, module.exports.copyCustomFields, module.exports.stylePropToObj ]; diff --git a/package.json b/package.json index 7a3dd6a8..d74ce6ba 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "dependencies": { "@bem/entity-name": "github:bem-sdk/bem-entity-name", "@bem/naming": "^2.0.0-6", + "camel-case": "^3.0.0", "pascal-case": "^2.0.1" } } diff --git a/test/plugins.js b/test/plugins.js index 06552762..d6d8f2c3 100644 --- a/test/plugins.js +++ b/test/plugins.js @@ -28,6 +28,32 @@ describe('pluginis', () => { }); }); + describe('camelCaseProps', () => { + it('should transform mod-name to modName', () => { + var res = T().process({ block: 'button2', mods: { 'has-clear': 'yes' } }); + + expect(res.JSX).to.equal( + `` + ); + }); + + it('should transform several mod-names to modName', () => { + var res = T().process({ block: 'button2', mods: { 'has-clear': 'yes', 'has-tick': 'too' } }); + + expect(res.JSX).to.equal( + `` + ); + }); + + it('should distinguish mod-name and modname', () => { + var res = T().process({ block: 'button2', mods: { 'has-clear': 'yes', 'hasclear': 'yes' } }); + + expect(res.JSX).to.equal( + `` + ); + }); + }); + describe('stylePropToObj', () => { it('styleProp to obj', () => { var res = T().process({ block: 'button2', style: 'width:200px' });