This repository has been archived by the owner on Nov 4, 2022. It is now read-only.
forked from jashkenas/underscore
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
140 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ | |
"env": { | ||
"browser": true, | ||
"node": true, | ||
"amd": true | ||
"amd": true, | ||
"es6": true | ||
}, | ||
|
||
"rules": { | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./property')('length'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = function (id) { | ||
return id; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]'; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]'; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.