Skip to content

Commit

Permalink
Use individual lodash functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Soto committed May 13, 2016
1 parent e65ad72 commit 469a8b9
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 64 deletions.
34 changes: 18 additions & 16 deletions lib/api/attributes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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 forEach = require('lodash.foreach'),
extend = require('lodash.assignin'),
some = require('lodash.some'),
$ = 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-',

// Lookup table for coercing string data-* attributes to their corresponding
// JavaScript primitives
Expand Down Expand Up @@ -74,7 +76,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 +110,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 +141,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 @@ -156,11 +158,11 @@ var setData = function(el, name, value) {
el.data = {};
}

if (typeof name === 'object') return _.extend(el.data, name);
if (typeof name === 'object') return extend(el.data, name);
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 Expand Up @@ -316,7 +318,7 @@ exports.removeAttr = function(name) {
};

exports.hasClass = function(className) {
return _.some(this, function(elem) {
return some(this, function(elem) {
var attrs = elem.attribs,
clazz = attrs && attrs['class'],
idx = -1,
Expand Down
4 changes: 2 additions & 2 deletions lib/api/css.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _ = require('lodash'),
var pick = require('lodash.pick'),
domEach = require('../utils').domEach;
var toString = Object.prototype.toString;

Expand Down Expand Up @@ -67,7 +67,7 @@ function getCss(el, prop) {
if (typeof prop === 'string') {
return styles[prop];
} else if (Array.isArray(prop)) {
return _.pick(styles, prop);
return pick(styles, prop);
} else {
return styles;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/api/forms.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 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'),
var map = require('lodash.map'),
submittableSelector = 'input,select,textarea,keygen',
r20 = /%20/g,
rCRLF = /\r?\n/g;
Expand All @@ -10,7 +10,7 @@ exports.serialize = function() {
var arr = this.serializeArray();

// Serialize each element into a key/value string
var retArr = _.map(arr, function(data) {
var retArr = map(arr, function(data) {
return encodeURIComponent(data.name) + '=' + encodeURIComponent(data.value);
});

Expand Down Expand Up @@ -48,7 +48,7 @@ exports.serializeArray = function() {
} else {
// If we have an array of values (e.g. `<select multiple>`), return an array of key/value pairs
if (Array.isArray(val)) {
return _.map(val, function(val) {
return map(val, function(val) {
// We trim replace any line endings (e.g. `\r` or `\r\n` with `\r\n`) to guarantee consistency across platforms
// These can occur inside of `<textarea>'s`
return {name: name, value: val.replace( rCRLF, '\r\n' )};
Expand Down
14 changes: 8 additions & 6 deletions lib/api/manipulation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var _ = require('lodash'),
var flatten = require('lodash.flatten'),
bind = require('lodash.bind'),
forEach = require('lodash.foreach'),
parse = require('../parse'),
$ = require('../static'),
updateDOM = parse.update,
Expand All @@ -17,7 +19,7 @@ exports._makeDomArray = function makeDomArray(elem, clone) {
} else if (elem.cheerio) {
return clone ? cloneDom(elem.get(), elem.options) : elem.get();
} else if (Array.isArray(elem)) {
return _.flatten(elem.map(function(el) {
return flatten(elem.map(function(el) {
return this._makeDomArray(el, clone);
}, this));
} else if (typeof elem === 'string') {
Expand Down Expand Up @@ -134,7 +136,7 @@ exports.wrap = function(wrapper) {
var wrapperFn = typeof wrapper === 'function' && wrapper,
lastIdx = this.length - 1;

_.forEach(this, _.bind(function(el, i) {
forEach(this, bind(function(el, i) {
var parent = el.parent || el.root,
siblings = parent.children,
dom, index;
Expand Down Expand Up @@ -346,7 +348,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 +369,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 +399,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
40 changes: 22 additions & 18 deletions lib/api/traversing.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
var _ = require('lodash'),
select = require('css-select'),
var select = require('css-select'),
bind = require('lodash.bind'),
forEach = require('lodash.foreach'),
reject = require('lodash.reject'),
filter = require('lodash.filter'),
reduce = require('lodash.reduce'),
utils = require('../utils'),
domEach = utils.domEach,
uniqueSort = require('htmlparser2').DomUtils.uniqueSort,
isTag = utils.isTag;

exports.find = function(selectorOrHaystack) {
var elems = _.reduce(this, function(memo, elem) {
return memo.concat(_.filter(elem.children, isTag));
var elems = reduce(this, function(memo, elem) {
return memo.concat(filter(elem.children, isTag));
}, []);
var contains = this.constructor.contains;
var haystack;
Expand Down Expand Up @@ -128,7 +132,7 @@ exports.next = function(selector) {
if (!this[0]) { return this; }
var elems = [];

_.forEach(this, function(elem) {
forEach(this, function(elem) {
while ((elem = elem.next)) {
if (isTag(elem)) {
elems.push(elem);
Expand All @@ -146,7 +150,7 @@ exports.nextAll = function(selector) {
if (!this[0]) { return this; }
var elems = [];

_.forEach(this, function(elem) {
forEach(this, function(elem) {
while ((elem = elem.next)) {
if (isTag(elem) && elems.indexOf(elem) === -1) {
elems.push(elem);
Expand All @@ -171,7 +175,7 @@ exports.nextUntil = function(selector, filterSelector) {
untilNode = selector;
}

_.forEach(this, function(elem) {
forEach(this, function(elem) {
while ((elem = elem.next)) {
if ((untilNode && elem !== untilNode) ||
(untilNodes && untilNodes.indexOf(elem) === -1) ||
Expand All @@ -194,7 +198,7 @@ exports.prev = function(selector) {
if (!this[0]) { return this; }
var elems = [];

_.forEach(this, function(elem) {
forEach(this, function(elem) {
while ((elem = elem.prev)) {
if (isTag(elem)) {
elems.push(elem);
Expand All @@ -212,7 +216,7 @@ exports.prevAll = function(selector) {
if (!this[0]) { return this; }
var elems = [];

_.forEach(this, function(elem) {
forEach(this, function(elem) {
while ((elem = elem.prev)) {
if (isTag(elem) && elems.indexOf(elem) === -1) {
elems.push(elem);
Expand All @@ -237,7 +241,7 @@ exports.prevUntil = function(selector, filterSelector) {
untilNode = selector;
}

_.forEach(this, function(elem) {
forEach(this, function(elem) {
while ((elem = elem.prev)) {
if ((untilNode && elem !== untilNode) ||
(untilNodes && untilNodes.indexOf(elem) === -1) ||
Expand All @@ -259,9 +263,9 @@ exports.prevUntil = function(selector, filterSelector) {
exports.siblings = function(selector) {
var parent = this.parent();

var elems = _.filter(
var elems = filter(
parent ? parent.children() : this.siblingsAndMe(),
_.bind(function(elem) { return isTag(elem) && !this.is(elem); }, this)
bind(function(elem) { return isTag(elem) && !this.is(elem); }, this)
);

if (selector !== undefined) {
Expand All @@ -273,8 +277,8 @@ exports.siblings = function(selector) {

exports.children = function(selector) {

var elems = _.reduce(this, function(memo, elem) {
return memo.concat(_.filter(elem.children, isTag));
var elems = reduce(this, function(memo, elem) {
return memo.concat(filter(elem.children, isTag));
}, []);

if (selector === undefined) return this._make(elems);
Expand All @@ -283,7 +287,7 @@ exports.children = function(selector) {
};

exports.contents = function() {
return this._make(_.reduce(this, function(all, elem) {
return this._make(reduce(this, function(all, elem) {
all.push.apply(all, elem.children);
return all;
}, []));
Expand All @@ -296,7 +300,7 @@ exports.each = function(fn) {
};

exports.map = function(fn) {
return this._make(_.reduce(this, function(memo, el, i) {
return this._make(reduce(this, function(memo, el, i) {
var val = fn.call(el, i, el);
return val == null ? memo : memo.concat(val);
}, []));
Expand Down Expand Up @@ -325,8 +329,8 @@ var makeFilterMethod = function(filterFn) {
};
};

exports.filter = makeFilterMethod(_.filter);
exports.not = makeFilterMethod(_.reject);
exports.filter = makeFilterMethod(filter);
exports.not = makeFilterMethod(reject);

exports.has = function(selectorOrHaystack) {
var that = this;
Expand Down
13 changes: 8 additions & 5 deletions lib/cheerio.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

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 All @@ -25,7 +28,7 @@ var api = [
var Cheerio = module.exports = function(selector, context, root, options) {
if (!(this instanceof Cheerio)) return new Cheerio(selector, context, root, options);

this.options = _.defaults(options || {}, this.options);
this.options = defaults(options || {}, this.options);

// $(), $(null), $(undefined), $(false)
if (!selector) return this;
Expand All @@ -44,7 +47,7 @@ var Cheerio = module.exports = function(selector, context, root, options) {

// $([dom])
if (Array.isArray(selector)) {
_.forEach(selector, _.bind(function(elem, idx) {
forEach(selector, bind(function(elem, idx) {
this[idx] = elem;
}, this));
this.length = selector.length;
Expand Down Expand Up @@ -85,7 +88,7 @@ var Cheerio = module.exports = function(selector, context, root, options) {
* Mix in `static`
*/

_.extend(Cheerio, require('./static'));
extend(Cheerio, require('./static'));

/*
* Set a signature of the object
Expand Down Expand Up @@ -135,7 +138,7 @@ Cheerio.prototype.toArray = function() {
* Plug in the API
*/
api.forEach(function(mod) {
_.extend(Cheerio.prototype, mod);
extend(Cheerio.prototype, mod);
});

var isNode = function(obj) {
Expand Down
Loading

0 comments on commit 469a8b9

Please sign in to comment.