Skip to content

Commit

Permalink
Merge pull request jashkenas#2138 from jridgewell/code-cleanup
Browse files Browse the repository at this point in the history
Some simple code cleanups
  • Loading branch information
akre54 committed Apr 2, 2015
2 parents 0fa1071 + 6b29e98 commit 483fc0f
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,6 @@
return cb(value, context, Infinity);
};

// An internal function for creating assigner functions.
var createAssigner = function(keysFunc, undefinedOnly) {
return function(obj) {
var length = arguments.length;
if (length < 2 || obj == null) return obj;
for (var index = 1; index < length; index++) {
var source = arguments[index],
keys = keysFunc(source),
l = keys.length;
for (var i = 0; i < l; i++) {
var key = keys[i];
if (!undefinedOnly || obj[key] === void 0) obj[key] = source[key];
}
}
return obj;
};
};

// An internal function for creating a new object that inherits from another.
var baseCreate = function(prototype) {
if (!_.isObject(prototype)) return {};
Expand Down Expand Up @@ -175,16 +157,16 @@
};

// Create a reducing function iterating left or right.
function createReduce(dir) {
var createReduce = function(dir) {
// Optimized iterator function as using arguments.length
// in the main function will deoptimize the, see #1991.
function iterator(obj, iteratee, memo, keys, index, length) {
var iterator = function(obj, iteratee, memo, keys, index, length) {
for (; index >= 0 && index < length; index += dir) {
var currentKey = keys ? keys[index] : index;
memo = iteratee(memo, obj[currentKey], currentKey, obj);
}
return memo;
}
};

return function(obj, iteratee, memo, context) {
iteratee = optimizeCb(iteratee, context, 4);
Expand All @@ -198,7 +180,7 @@
}
return iterator(obj, iteratee, memo, keys, index, length);
};
}
};

// **Reduce** builds up a single result from a list of values, aka `inject`,
// or `foldl`.
Expand Down Expand Up @@ -606,7 +588,7 @@
};

// Generator function to create the findIndex and findLastIndex functions
function createPredicateIndexFinder(dir) {
var createPredicateIndexFinder = function(dir) {
return function(array, predicate, context) {
predicate = cb(predicate, context);
var length = getLength(array);
Expand All @@ -616,7 +598,7 @@
}
return -1;
};
}
};

// Returns the first index on an array-like that passes a predicate test
_.findIndex = createPredicateIndexFinder(1);
Expand All @@ -636,7 +618,7 @@
};

// Generator function to create the indexOf and lastIndexOf functions
function createIndexFinder(dir, predicateFind, sortedIndex) {
var createIndexFinder = function(dir, predicateFind, sortedIndex) {
return function(array, item, idx) {
var i = 0, length = getLength(array);
if (typeof idx == 'number') {
Expand All @@ -658,7 +640,7 @@
}
return -1;
};
}
};

// Return the position of the first occurrence of an item in an array,
// or -1 if the item is not included in the array.
Expand Down Expand Up @@ -901,7 +883,7 @@
var nonEnumerableProps = ['valueOf', 'isPrototypeOf', 'toString',
'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString'];

function collectNonEnumProps(obj, keys) {
var collectNonEnumProps = function(obj, keys) {
var nonEnumIdx = nonEnumerableProps.length;
var constructor = obj.constructor;
var proto = (_.isFunction(constructor) && constructor.prototype) || ObjProto;
Expand All @@ -916,7 +898,7 @@
keys.push(prop);
}
}
}
};

// Retrieve the names of an object's own properties.
// Delegates to **ECMAScript 5**'s native `Object.keys`
Expand Down Expand Up @@ -997,6 +979,24 @@
return names.sort();
};

// An internal function for creating assigner functions.
var createAssigner = function(keysFunc, undefinedOnly) {
return function(obj) {
var length = arguments.length;
if (length < 2 || obj == null) return obj;
for (var index = 1; index < length; index++) {
var source = arguments[index],
keys = keysFunc(source),
l = keys.length;
for (var i = 0; i < l; i++) {
var key = keys[i];
if (!undefinedOnly || obj[key] === void 0) obj[key] = source[key];
}
}
return obj;
};
};

// Extend a given object with all the properties in passed-in object(s).
_.extend = createAssigner(_.allKeys);

Expand Down

0 comments on commit 483fc0f

Please sign in to comment.