Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #25 from no23reason/master
Browse files Browse the repository at this point in the history
Use modularized lodash
  • Loading branch information
Mike Nikles committed Apr 27, 2016
2 parents a0cfb14 + 9d00351 commit 97587cd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
8 changes: 5 additions & 3 deletions lib/parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

var _ = require('lodash');
var compact = require('lodash.compact');
var find = require('lodash.find');
var map = require('lodash.map');
var htmlParser = require('htmlparser2');
var ProcessingInstructions = require('./processing-instructions');
var IsValidNodeDefinitions = require('./is-valid-node-definitions');
Expand All @@ -15,12 +17,12 @@ var Html2React = function(React, options) {

var traverseDom = function(node, isValidNode, processingInstructions, index) {
if (isValidNode(node)) {
var processingInstruction = _.find(processingInstructions || [],
var processingInstruction = find(processingInstructions || [],
function (processingInstruction) {
return processingInstruction.shouldProcessNode(node);
});
if (processingInstruction != null) {
var children = _.compact(_.map(node.children, function (child, i) {
var children = compact(map(node.children, function (child, i) {
return traverseDom(child, isValidNode, processingInstructions, i);
}));
return processingInstruction.processNode(node, children, index);
Expand Down
16 changes: 9 additions & 7 deletions lib/process-node-definitions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

var _ = require('lodash');
var camelCase = require('lodash.camelcase');
var forEach = require('lodash.foreach');
var includes = require('lodash.includes');
var ent = require('ent');

// https://github.com/facebook/react/blob/0.14-stable/src/renderers/dom/shared/ReactDOMComponent.js#L457
Expand All @@ -10,14 +12,14 @@ var voidElementTags = [
];

function createStyleJsonFromString(styleString) {
if (_.isNull(styleString) || _.isUndefined(styleString) || styleString === '') {
if (!styleString) {
return {};
}
var styles = styleString.split(';');
var singleStyle, key, value, jsonStyles = {};
for (var i = 0; i < styles.length; i++) {
singleStyle = styles[i].split(':');
key = _.camelCase(singleStyle[0]);
key = camelCase(singleStyle[0]);
value = singleStyle[1];
if (key.length > 0 && value.length > 0) {
jsonStyles[key] = value;
Expand All @@ -41,7 +43,7 @@ var ProcessNodeDefinitions = function(React) {
};
// Process attributes
if (node.attribs) {
_.each(node.attribs, function(value, key) {
forEach(node.attribs, function(value, key) {
switch (key || '') {
case 'style':
elementProps.style = createStyleJsonFromString(node.attribs.style);
Expand All @@ -56,11 +58,11 @@ var ProcessNodeDefinitions = function(React) {
});
}

if (_.contains(voidElementTags, node.name)) {
if (includes(voidElementTags, node.name)) {
return React.createElement(node.name, elementProps)
} else {
return React.createElement(node.name, elementProps, node.data, children);
}
return React.createElement(node.name, elementProps, node.data, children);
}

}

Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@
"dependencies": {
"ent": "^2.2.0",
"htmlparser2": "^3.8.3",
"lodash": "^3.9.3"
"lodash.camelcase": "^4.1.0",
"lodash.compact": "^3.0.1",
"lodash.find": "^4.3.0",
"lodash.foreach": "^4.2.0",
"lodash.includes": "^4.1.2",
"lodash.map": "^4.3.0"
},
"devDependencies": {
"coveralls": "2.11.9",
"istanbul": "0.4.3",
"lodash": "^4.11.1",
"mocha": "2.4.5",
"mocha-lcov-reporter": "1.2.0",
"react": "^0.14.7",
Expand Down

0 comments on commit 97587cd

Please sign in to comment.