Skip to content

Commit

Permalink
Use individual lodash functions (cheeriojs#864)
Browse files Browse the repository at this point in the history
* Use individual lodash functions

* Use `_` object for lodash functions
  • Loading branch information
Juan authored and fb55 committed Jun 7, 2016
1 parent e65ad72 commit e7d18af
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 39 deletions.
32 changes: 18 additions & 14 deletions lib/api/attributes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
var _ = require('lodash'),
$ = require('../static'),
utils = require('../utils'),
isTag = utils.isTag,
domEach = utils.domEach,
hasOwn = Object.prototype.hasOwnProperty,
camelCase = utils.camelCase,
cssCase = utils.cssCase,
rspace = /\s+/,
dataAttrPrefix = 'data-',
var $ = require('../static'),
utils = require('../utils'),
isTag = utils.isTag,
domEach = utils.domEach,
hasOwn = Object.prototype.hasOwnProperty,
camelCase = utils.camelCase,
cssCase = utils.cssCase,
rspace = /\s+/,
dataAttrPrefix = 'data-',
_ = {
forEach: require('lodash.foreach'),
extend: require('lodash.assignin'),
some: require('lodash.some')
},

// Lookup table for coercing string data-* attributes to their corresponding
// JavaScript primitives
Expand Down Expand Up @@ -74,7 +78,7 @@ exports.attr = function(name, value) {
if (!isTag(el)) return;

if (typeof name === 'object') {
_.each(name, function(value, name) {
_.forEach(name, function(value, name) {
setAttr(el, name, value);
});
} else {
Expand Down Expand Up @@ -108,7 +112,7 @@ exports.prop = function (name, value) {
case 'style':
property = this.css();

_.each(property, function (v, p) {
_.forEach(property, function (v, p) {
property[i++] = p;
});

Expand Down Expand Up @@ -139,7 +143,7 @@ exports.prop = function (name, value) {

if (typeof name === 'object') {

_.each(name, function(val, name) {
_.forEach(name, function(val, name) {
setProp(el, name, val);
});

Expand All @@ -160,7 +164,7 @@ var setData = function(el, name, value) {
if (typeof name === 'string' && value !== undefined) {
el.data[name] = value;
} else if (typeof name === 'object') {
_.exend(el.data, name);
_.extend(el.data, name);
}
};

Expand Down
7 changes: 5 additions & 2 deletions lib/api/css.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
var _ = require('lodash'),
domEach = require('../utils').domEach;
var domEach = require('../utils').domEach,
_ = {
pick: require('lodash.pick'),
};

var toString = Object.prototype.toString;

/**
Expand Down
8 changes: 5 additions & 3 deletions lib/api/forms.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// https://github.com/jquery/jquery/blob/2.1.3/src/manipulation/var/rcheckableType.js
// https://github.com/jquery/jquery/blob/2.1.3/src/serialize.js
var _ = require('lodash'),
submittableSelector = 'input,select,textarea,keygen',
var submittableSelector = 'input,select,textarea,keygen',
r20 = /%20/g,
rCRLF = /\r?\n/g;
rCRLF = /\r?\n/g,
_ = {
map: require('lodash.map')
};

exports.serialize = function() {
// Convert form elements into name/value objects
Expand Down
16 changes: 10 additions & 6 deletions lib/api/manipulation.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
var _ = require('lodash'),
parse = require('../parse'),
var parse = require('../parse'),
$ = require('../static'),
updateDOM = parse.update,
evaluate = parse.evaluate,
utils = require('../utils'),
domEach = utils.domEach,
cloneDom = utils.cloneDom,
isHtml = utils.isHtml,
slice = Array.prototype.slice;
slice = Array.prototype.slice,
_ = {
flatten: require('lodash.flatten'),
bind: require('lodash.bind'),
forEach: require('lodash.foreach')
};

// Create an array of nodes, recursing into arrays and parsing strings if
// necessary
Expand Down Expand Up @@ -346,7 +350,7 @@ exports.replaceWith = function(content) {

exports.empty = function() {
domEach(this, function(i, el) {
_.each(el.children, function(el) {
_.forEach(el.children, function(el) {
el.next = el.prev = el.parent = null;
});

Expand All @@ -367,7 +371,7 @@ exports.html = function(str) {
var opts = this.options;

domEach(this, function(i, el) {
_.each(el.children, function(el) {
_.forEach(el.children, function(el) {
el.next = el.prev = el.parent = null;
});

Expand Down Expand Up @@ -397,7 +401,7 @@ exports.text = function(str) {

// Append text node to each selected elements
domEach(this, function(i, el) {
_.each(el.children, function(el) {
_.forEach(el.children, function(el) {
el.next = el.prev = el.parent = null;
});

Expand Down
12 changes: 9 additions & 3 deletions lib/api/traversing.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
var _ = require('lodash'),
select = require('css-select'),
var select = require('css-select'),
utils = require('../utils'),
domEach = utils.domEach,
uniqueSort = require('htmlparser2').DomUtils.uniqueSort,
isTag = utils.isTag;
isTag = utils.isTag,
_ = {
bind: require('lodash.bind'),
forEach: require('lodash.foreach'),
reject: require('lodash.reject'),
filter: require('lodash.filter'),
reduce: require('lodash.reduce')
};

exports.find = function(selectorOrHaystack) {
var elems = _.reduce(this, function(memo, elem) {
Expand Down
7 changes: 6 additions & 1 deletion lib/cheerio.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

var parse = require('./parse'),
isHtml = require('./utils').isHtml,
_ = require('lodash');
_ = {
extend: require('lodash.assignin'),
bind: require('lodash.bind'),
forEach: require('lodash.foreach'),
defaults: require('lodash.defaults')
};

/*
* The API
Expand Down
11 changes: 7 additions & 4 deletions lib/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
* Module dependencies
*/

var select = require('css-select'),
var serialize = require('dom-serializer'),
select = require('css-select'),
parse = require('./parse'),
serialize = require('dom-serializer'),
_ = require('lodash');
_ = {
merge: require('lodash.merge'),
defaults: require('lodash.defaults')
};

/**
* $.load(str)
Expand Down Expand Up @@ -34,7 +37,7 @@ exports.load = function(content, options) {
// Mimic jQuery's prototype alias for plugin authors.
initialize.fn = initialize.prototype;

// Keep a reference to the top-level scope so we can chain methods that implicitly
// Keep a reference to the top-level scope so we can chain methods that implicitly
// resolve selectors; e.g. $("<span>").(".bar"), which otherwise loses ._root
initialize.prototype._originalRoot = root;

Expand Down
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,21 @@
},
"dependencies": {
"css-select": "~1.2.0",
"dom-serializer": "~0.1.0",
"entities": "~1.1.1",
"htmlparser2": "~3.8.1",
"dom-serializer": "~0.1.0",
"lodash": "^4.1.0"
"lodash.assignin": "^4.0.9",
"lodash.bind": "^4.1.4",
"lodash.defaults": "^4.0.1",
"lodash.filter": "^4.4.0",
"lodash.flatten": "^4.2.0",
"lodash.foreach": "^4.3.0",
"lodash.map": "^4.4.0",
"lodash.merge": "^4.4.0",
"lodash.pick": "^4.2.1",
"lodash.reduce": "^4.4.0",
"lodash.reject": "^4.4.0",
"lodash.some": "^4.4.0"
},
"devDependencies": {
"benchmark": "~1.0.0",
Expand Down
6 changes: 4 additions & 2 deletions test/cheerio.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
var expect = require('expect.js'),
_ = require('lodash'),
htmlparser2 = require('htmlparser2'),
$ = require('../'),
fixtures = require('./fixtures'),
fruits = fixtures.fruits,
food = fixtures.food;
food = fixtures.food,
_ = {
filter: require('lodash.filter')
};

// HTML
var script = '<script src="script.js" type="text/javascript"></script>',
Expand Down
6 changes: 4 additions & 2 deletions test/xml.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var expect = require('expect.js'),
_ = require('lodash'),
cheerio = require('..');
cheerio = require('..'),
_ = {
extend: require('lodash.assignin')
};

var xml = function(str, options) {
options = _.extend({ xmlMode: true }, options);
Expand Down

0 comments on commit e7d18af

Please sign in to comment.