Skip to content

Commit

Permalink
Add styleToObj tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeti-or committed Apr 19, 2017
1 parent 008f7d2 commit 303b573
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
14 changes: 14 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,22 @@ function objToStr(obj) {
return `{ ${keys.map(k => propToStr(k, obj[k])).join(', ')} }`;
}

function styleToObj(style) {
if (typeof style === 'string') {
return style.split(';').reduce((acc, st) => {
if (st.length) {
var prop = st.split(':');
acc[prop[0]] = prop[1];
}
return acc;
}, {});
}
return style;
}

module.exports = {
objToStr,
arrToStr,
styleToObj,
valToStr
};
16 changes: 9 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var pascalCase = require('pascal-case');

var reactMappings = require('./reactMappings');
var valToStr = require('./helpers').valToStr;
var styleToObj = require('./helpers').styleToObj;

var plugins = require('./plugins');

Expand All @@ -17,12 +18,12 @@ function JSXNode(tag, props, children) {
}

var propsToStr = props => Object.keys(props).reduce((acc, k) => {
if (typeof props[k] === 'string') {
return acc + ` ${k}=${valToStr(props[k])}`
} else {
return acc + ` ${k}={${valToStr(props[k])}}`
}
}, '');
if (typeof props[k] === 'string') {
return acc + ` ${k}=${valToStr(props[k])}`
} else {
return acc + ` ${k}={${valToStr(props[k])}}`
}
}, '');
var tagToClass = tag => reactMappings[tag] ? tag : pascalCase(tag);

JSXNode.prototype.toString = function() {
Expand Down Expand Up @@ -140,6 +141,7 @@ Transformer.prototype.Transformer = Transformer;
module.exports = function(opts) {
return new Transformer(opts || {});
};

module.exports.tagToClass = tagToClass;
module.exports.plugins = plugins;
module.exports.styleObj = plugins.helpers.styleObj;
module.exports.styleToObj = styleToObj;
21 changes: 3 additions & 18 deletions lib/plugins.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var styleToObj = require('./helpers').styleToObj;

module.exports.defaultPlugins = [
function copyMods(jsx, bemjson) {
Expand All @@ -15,16 +16,12 @@ module.exports.defaultPlugins = [
jsx.props[k] = bemjson[k];
});
},
function styleToObj(jsx) {
function stylePropToObj(jsx) {
jsx.props['style'] &&
(jsx.props['attrs']['style'] = jsx.props['style'] = styleObj(jsx.props['style']));
(jsx.props['attrs']['style'] = jsx.props['style'] = styleToObj(jsx.props['style']));
}
];

module.exports.helpers = {
styleObj: styleObj
};

module.exports.whiteList = function(options) {
options = options || {};
return function(jsx) {
Expand All @@ -36,15 +33,3 @@ module.exports.whiteList = function(options) {
}
};

function styleObj(style) {
if (typeof style === 'string') {
return style.split(';').reduce((acc, st) => {
if (st.length) {
var prop = st.split(':');
acc[prop[0]] = prop[1];
}
return acc;
}, {});
}
return style;
}
16 changes: 15 additions & 1 deletion test/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const expect = require('chai').expect;

const objToStr = require('../lib/helpers').objToStr;
const helpers = require('../lib/helpers');
const objToStr = helpers.objToStr;
const styleToObj = helpers.styleToObj;


describe('helpers: objToStr', () => {
Expand Down Expand Up @@ -58,3 +60,15 @@ describe('helpers: objToStr', () => {
});
});
});

describe('helpers: styleToObj', () => {
it('should transform style string to style obj', () => {
var obj = styleToObj('width:200px;height:100px;');
expect(obj).to.eql({ width: '200px', height: '100px' });
});

it('should not transform style obj to smth else', () => {
var obj = { width: '200px', height: '100px' };
expect(obj).to.eql({ width: '200px', height: '100px' });
});
});

0 comments on commit 303b573

Please sign in to comment.