Skip to content

Commit e7d18af

Browse files
Juanfb55
Juan
authored andcommittedJun 7, 2016
Use individual lodash functions (#864)
* Use individual lodash functions * Use `_` object for lodash functions
1 parent e65ad72 commit e7d18af

10 files changed

+81
-39
lines changed
 

‎lib/api/attributes.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
var _ = require('lodash'),
2-
$ = require('../static'),
3-
utils = require('../utils'),
4-
isTag = utils.isTag,
5-
domEach = utils.domEach,
6-
hasOwn = Object.prototype.hasOwnProperty,
7-
camelCase = utils.camelCase,
8-
cssCase = utils.cssCase,
9-
rspace = /\s+/,
10-
dataAttrPrefix = 'data-',
1+
var $ = require('../static'),
2+
utils = require('../utils'),
3+
isTag = utils.isTag,
4+
domEach = utils.domEach,
5+
hasOwn = Object.prototype.hasOwnProperty,
6+
camelCase = utils.camelCase,
7+
cssCase = utils.cssCase,
8+
rspace = /\s+/,
9+
dataAttrPrefix = 'data-',
10+
_ = {
11+
forEach: require('lodash.foreach'),
12+
extend: require('lodash.assignin'),
13+
some: require('lodash.some')
14+
},
1115

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

7680
if (typeof name === 'object') {
77-
_.each(name, function(value, name) {
81+
_.forEach(name, function(value, name) {
7882
setAttr(el, name, value);
7983
});
8084
} else {
@@ -108,7 +112,7 @@ exports.prop = function (name, value) {
108112
case 'style':
109113
property = this.css();
110114

111-
_.each(property, function (v, p) {
115+
_.forEach(property, function (v, p) {
112116
property[i++] = p;
113117
});
114118

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

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

142-
_.each(name, function(val, name) {
146+
_.forEach(name, function(val, name) {
143147
setProp(el, name, val);
144148
});
145149

@@ -160,7 +164,7 @@ var setData = function(el, name, value) {
160164
if (typeof name === 'string' && value !== undefined) {
161165
el.data[name] = value;
162166
} else if (typeof name === 'object') {
163-
_.exend(el.data, name);
167+
_.extend(el.data, name);
164168
}
165169
};
166170

‎lib/api/css.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
var _ = require('lodash'),
2-
domEach = require('../utils').domEach;
1+
var domEach = require('../utils').domEach,
2+
_ = {
3+
pick: require('lodash.pick'),
4+
};
5+
36
var toString = Object.prototype.toString;
47

58
/**

‎lib/api/forms.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// https://github.com/jquery/jquery/blob/2.1.3/src/manipulation/var/rcheckableType.js
22
// https://github.com/jquery/jquery/blob/2.1.3/src/serialize.js
3-
var _ = require('lodash'),
4-
submittableSelector = 'input,select,textarea,keygen',
3+
var submittableSelector = 'input,select,textarea,keygen',
54
r20 = /%20/g,
6-
rCRLF = /\r?\n/g;
5+
rCRLF = /\r?\n/g,
6+
_ = {
7+
map: require('lodash.map')
8+
};
79

810
exports.serialize = function() {
911
// Convert form elements into name/value objects

‎lib/api/manipulation.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
var _ = require('lodash'),
2-
parse = require('../parse'),
1+
var parse = require('../parse'),
32
$ = require('../static'),
43
updateDOM = parse.update,
54
evaluate = parse.evaluate,
65
utils = require('../utils'),
76
domEach = utils.domEach,
87
cloneDom = utils.cloneDom,
98
isHtml = utils.isHtml,
10-
slice = Array.prototype.slice;
9+
slice = Array.prototype.slice,
10+
_ = {
11+
flatten: require('lodash.flatten'),
12+
bind: require('lodash.bind'),
13+
forEach: require('lodash.foreach')
14+
};
1115

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

347351
exports.empty = function() {
348352
domEach(this, function(i, el) {
349-
_.each(el.children, function(el) {
353+
_.forEach(el.children, function(el) {
350354
el.next = el.prev = el.parent = null;
351355
});
352356

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

369373
domEach(this, function(i, el) {
370-
_.each(el.children, function(el) {
374+
_.forEach(el.children, function(el) {
371375
el.next = el.prev = el.parent = null;
372376
});
373377

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

398402
// Append text node to each selected elements
399403
domEach(this, function(i, el) {
400-
_.each(el.children, function(el) {
404+
_.forEach(el.children, function(el) {
401405
el.next = el.prev = el.parent = null;
402406
});
403407

‎lib/api/traversing.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
var _ = require('lodash'),
2-
select = require('css-select'),
1+
var select = require('css-select'),
32
utils = require('../utils'),
43
domEach = utils.domEach,
54
uniqueSort = require('htmlparser2').DomUtils.uniqueSort,
6-
isTag = utils.isTag;
5+
isTag = utils.isTag,
6+
_ = {
7+
bind: require('lodash.bind'),
8+
forEach: require('lodash.foreach'),
9+
reject: require('lodash.reject'),
10+
filter: require('lodash.filter'),
11+
reduce: require('lodash.reduce')
12+
};
713

814
exports.find = function(selectorOrHaystack) {
915
var elems = _.reduce(this, function(memo, elem) {

‎lib/cheerio.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
var parse = require('./parse'),
66
isHtml = require('./utils').isHtml,
7-
_ = require('lodash');
7+
_ = {
8+
extend: require('lodash.assignin'),
9+
bind: require('lodash.bind'),
10+
forEach: require('lodash.foreach'),
11+
defaults: require('lodash.defaults')
12+
};
813

914
/*
1015
* The API

‎lib/static.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
* Module dependencies
33
*/
44

5-
var select = require('css-select'),
5+
var serialize = require('dom-serializer'),
6+
select = require('css-select'),
67
parse = require('./parse'),
7-
serialize = require('dom-serializer'),
8-
_ = require('lodash');
8+
_ = {
9+
merge: require('lodash.merge'),
10+
defaults: require('lodash.defaults')
11+
};
912

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

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

‎package.json

+13-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,21 @@
2626
},
2727
"dependencies": {
2828
"css-select": "~1.2.0",
29+
"dom-serializer": "~0.1.0",
2930
"entities": "~1.1.1",
3031
"htmlparser2": "~3.8.1",
31-
"dom-serializer": "~0.1.0",
32-
"lodash": "^4.1.0"
32+
"lodash.assignin": "^4.0.9",
33+
"lodash.bind": "^4.1.4",
34+
"lodash.defaults": "^4.0.1",
35+
"lodash.filter": "^4.4.0",
36+
"lodash.flatten": "^4.2.0",
37+
"lodash.foreach": "^4.3.0",
38+
"lodash.map": "^4.4.0",
39+
"lodash.merge": "^4.4.0",
40+
"lodash.pick": "^4.2.1",
41+
"lodash.reduce": "^4.4.0",
42+
"lodash.reject": "^4.4.0",
43+
"lodash.some": "^4.4.0"
3344
},
3445
"devDependencies": {
3546
"benchmark": "~1.0.0",

‎test/cheerio.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
var expect = require('expect.js'),
2-
_ = require('lodash'),
32
htmlparser2 = require('htmlparser2'),
43
$ = require('../'),
54
fixtures = require('./fixtures'),
65
fruits = fixtures.fruits,
7-
food = fixtures.food;
6+
food = fixtures.food,
7+
_ = {
8+
filter: require('lodash.filter')
9+
};
810

911
// HTML
1012
var script = '<script src="script.js" type="text/javascript"></script>',

‎test/xml.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var expect = require('expect.js'),
2-
_ = require('lodash'),
3-
cheerio = require('..');
2+
cheerio = require('..'),
3+
_ = {
4+
extend: require('lodash.assignin')
5+
};
46

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

0 commit comments

Comments
 (0)
Please sign in to comment.