Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
fix several issues
Browse files Browse the repository at this point in the history
  • Loading branch information
SqrTT committed Jan 31, 2017
1 parent d382da7 commit 236971b
Show file tree
Hide file tree
Showing 18 changed files with 140 additions and 133 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"env": {
"browser": true,
"node": true,
"amd": true
"amd": true,
"es6": true
},

"rules": {
Expand Down
12 changes: 0 additions & 12 deletions _arrayHelper.js

This file was deleted.

26 changes: 0 additions & 26 deletions _createIndexFinder.js

This file was deleted.

5 changes: 0 additions & 5 deletions _shallowProperty.js

This file was deleted.

11 changes: 1 addition & 10 deletions contains.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
const isArrayLike = require('./_arrayHelper').isArrayLike;
const values = require('./values');
const indexOf = require('./indexOf');

// Determine if the array or object contains a given item (using `===`).
// Aliased as `includes` and `include`.
module.exports = function(obj, item, fromIndex, guard) {
if (!isArrayLike(obj)) obj = values(obj);
if (typeof fromIndex != 'number' || guard) fromIndex = 0;
return indexOf(obj, item, fromIndex) >= 0;
};
module.exports = require('./includes');
17 changes: 16 additions & 1 deletion findIndex.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
const createPredicateIndexFinder = require('./_createPredicateIndexFinder');
const cb = require('./cb');
const getLength = require('./getLength');


// Generator function to create the findIndex and findLastIndex functions
var createPredicateIndexFinder = function(dir) {
return function(array, predicate, context) {
predicate = cb(predicate, context);
var length = getLength(array);
var index = dir > 0 ? 0 : length - 1;
for (; index >= 0 && index < length; index += dir) {
if (predicate(array[index], index, array)) return index;
}
return -1;
};
};

// Returns the first index on an array-like that passes a predicate test.
module.exports = createPredicateIndexFinder(1);
6 changes: 3 additions & 3 deletions first.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ module.exports = function first(array, n, guard) {
n = 1;
}
var result = [];
find(array, function(value) {
find(array, function (value) {
if (result.length !== n && result.length < n) {
result.push(value);
} else {
return true;
}
});
if (result.length === 1) {
if (result.length && n === 1) {
result = result.pop();
}
return result;
};
};
1 change: 1 addition & 0 deletions getLength.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./property')('length');
3 changes: 3 additions & 0 deletions id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (id) {
return id;
}
10 changes: 10 additions & 0 deletions includes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Determine if the array or object contains a given item (using `===`).
// Aliased as `includes` and `include`.
const isArrayLike = require('./isArrayLike');
const indexOf = require('./indexOf');

module.exports = function(obj, item, fromIndex, guard) {
if (!isArrayLike(obj)) obj = require('./values')(obj);
if (typeof fromIndex != 'number' || guard) fromIndex = 0;
return indexOf(obj, item, fromIndex) >= 0;
};
67 changes: 63 additions & 4 deletions indexOf.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,68 @@
const createIndexFinder = require('./_createIndexFinder');
const findIndex = require('./findIndex');
const sortedIndex = require('./sortedIndex');
const getLength = require('./getLength');
const cb = require('./cb');
const isNumber = require('./isNumber');

const _isNaN = function(obj) {
return _.isNumber(obj) && isNaN(obj);
};

// Generator function to create the findIndex and findLastIndex functions
var createPredicateIndexFinder = function(dir) {
return function(array, predicate, context) {
predicate = cb(predicate, context);
var length = getLength(array);
var index = dir > 0 ? 0 : length - 1;
for (; index >= 0 && index < length; index += dir) {
if (predicate(array[index], index, array)) return index;
}
return -1;
};
};

// Returns the first index on an array-like that passes a predicate test
const findIndex = createPredicateIndexFinder(1);


// Use a comparator function to figure out the smallest index at which
// an object should be inserted so as to maintain order. Uses binary search.
const sortedIndex = function(array, obj, iteratee, context) {
iteratee = cb(iteratee, context, 1);
var value = iteratee(obj);
var low = 0, high = getLength(array);
while (low < high) {
var mid = Math.floor((low + high) / 2);
if (iteratee(array[mid]) < value) low = mid + 1; else high = mid;
}
return low;
};

// Generator function to create the indexOf and lastIndexOf functions
var createIndexFinder = function(dir, predicateFind, sortedIndex) {
return function(array, item, idx) {
var i = 0, length = getLength(array);
if (typeof idx == 'number') {
if (dir > 0) {
i = idx >= 0 ? idx : Math.max(idx + length, i);
} else {
length = idx >= 0 ? Math.min(idx + 1, length) : idx + length + 1;
}
} else if (sortedIndex && idx && length) {
idx = sortedIndex(array, item);
return array[idx] === item ? idx : -1;
}
if (item !== item) {
idx = predicateFind(slice.call(array, i, length), _isNaN);
return idx >= 0 ? idx + i : -1;
}
for (idx = dir > 0 ? i : length - 1; idx >= 0 && idx < length; idx += dir) {
if (array[idx] === item) return idx;
}
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.
// If the array is large and already in sort order, pass `true`
// for **isSorted** to use binary search.
module.exports = createIndexFinder(1, findIndex, sortedIndex);
module.exports = createIndexFinder(1, findIndex, sortedIndex);
7 changes: 7 additions & 0 deletions isArrayLike.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const getLength = require('./getLength');
const MAX_ARRAY_INDEX = Math.pow(2, 53) - 1;

module.exports = function(collection) {
const length = getLength(collection);
return typeof length == 'number' && length >= 0 && length <= MAX_ARRAY_INDEX;
};
3 changes: 1 addition & 2 deletions isBoolean.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Is a given value a boolean?
module.exports = function(obj) {
return obj === true || obj === false || Object.prototype.toString.call(obj) === '[object Boolean]';
return obj === true || obj === false || Object.prototype.toString.call(obj) === '[object Boolean]';
};
3 changes: 3 additions & 0 deletions isNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(obj) {
return Object.prototype.toString.call(obj) === '[object Number]';
};
8 changes: 4 additions & 4 deletions range.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function range(start, stop, step) {
module.exports = function(start, stop, step) {
if (stop == null) {
stop = start || 0;
start = 0;
Expand All @@ -8,11 +8,11 @@ module.exports = function range(start, stop, step) {
}

var length = Math.max(Math.ceil((stop - start) / step), 0);
var rng = new Array(length);
var range = Array(length);

for (var idx = 0; idx < length; idx++, start += step) {
rng[idx] = start;
range[idx] = start;
}

return rng;
return range;
};
2 changes: 1 addition & 1 deletion sortedIndex.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const cb = require('./cb');
const getLength = require('./_arrayHelper').getLength;
const getLength = require('./getLength');

// Use a comparator function to figure out the smallest index at which
// an object should be inserted so as to maintain order. Uses binary search.
Expand Down
53 changes: 8 additions & 45 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,26 +382,7 @@
};

// Return the maximum element (or element-based computation).
_.max = function(obj, iteratee, context) {
var result = -Infinity, lastComputed = -Infinity, computed;
if (iteratee == null || (typeof iteratee == 'number' && typeof obj[0] != 'object') && obj != null) {
_.each(obj, function(value) {
if (value > result) {
result = value;
}
});
} else {
iteratee = cb(iteratee, context);
_.each(obj, function(v, index, list) {
computed = iteratee(v, index, list);
if (computed > lastComputed || computed === -Infinity && result === -Infinity) {
result = v;
lastComputed = computed;
}
});
}
return result;
};
_.max = require('./max');

// Return the minimum element (or element-based computation).
_.min = function(obj, iteratee, context) {
Expand Down Expand Up @@ -504,16 +485,16 @@
_.countBy = group(function(result, value, key) {
if (_.has(result, key)) result[key]++; else result[key] = 1;
});

var reStrSymbol = /[^\ud800-\udfff]|[\ud800-\udbff][\udc00-\udfff]|[\ud800-\udfff]/g;
// Safely create a real, live array from anything iterable.
_.toArray = function(obj) {
if (!obj) return [];
if (_.isArray(obj)) return slice.call(obj);
if (_.isString(obj)) {
// Keep surrogate pair characters together
return obj.match(reStrSymbol);
}
// Keep surrogate pair characters together
return obj.match(reStrSymbol);
}
if (isArrayLike(obj)) return _.map(obj, _.identity);
return _.values(obj);
};
Expand Down Expand Up @@ -549,7 +530,7 @@
return true;
}
});
if (result.length === 1) {
if (result.length && n === 1) {
result = result.pop();
}
return result;
Expand Down Expand Up @@ -783,24 +764,7 @@
// Generate an integer Array containing an arithmetic progression. A port of
// the native Python `range()` function. See
// [the Python documentation](http://docs.python.org/library/functions.html#range).
_.range = function(start, stop, step) {
if (stop == null) {
stop = start || 0;
start = 0;
}
if (!step) {
step = stop < start ? -1 : 1;
}

var length = Math.max(Math.ceil((stop - start) / step), 0);
var range = Array(length);

for (var idx = 0; idx < length; idx++, start += step) {
range[idx] = start;
}

return range;
};
_.range = require('./range');

// Split an **array** into several arrays containing **count** or less elements
// of initial array
Expand Down Expand Up @@ -1357,7 +1321,6 @@
}



// Is a given object a finite number?
_.isFinite = function(obj) {
return !_.isSymbol(obj) && isFinite(obj) && !isNaN(parseFloat(obj));
Expand Down Expand Up @@ -1655,4 +1618,4 @@
return '' + this._wrapped;
};

}(this));
}(module.exports));
Loading

0 comments on commit 236971b

Please sign in to comment.