lodash
- A modern JavaScript utility library delivering modularity, performance, & extras
https://github.com/lodash/lodash
findIndex๋ ๋ฐฐ์ด์ ๋ฃ๊ณ , ๊ธฐ์ค(ํจ์, ๋์ผํ obj, property ๊ฐ๋ฑ ์ด๋ ํ ๊ฐ์ ๋ฃ๊ณ , ์ฐพ์ผ๋ ค๊ณ ํ๋ ์ธ๋ฑ์ค ์์(Optional)์ ๋ฃ์ผ๋ฉด ์ฐพ์์ฃผ๋ ํจ์์ด๋ค.
์ฆ ์๋ users๋ฅผ ์ ๋ ฅํด์ index๋ฅผ ์ฐพ๊ธฐ ํ๋ฉด ์๋ ์ฃผ์๊ณผ ๊ฐ์ด ๋ฆฌํดํ๊ฒ๋ ํ๋ ํจ์์ด๋ค.
var users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }];
* _.findIndex(users, function(o) { return o.user == 'barney'; });
* // => 0
*
* // The `_.matches` iteratee shorthand.
* _.findIndex(users, { 'user': 'fred', 'active': false });
* // => 1
*
* // The `_.matchesProperty` iteratee shorthand.
* _.findIndex(users, ['active', false]);
* // => 0
*
* // The `_.property` iteratee shorthand.
* _.findIndex(users, 'active');
* // => 2
*/
function findIndex(array, predicate, fromIndex) {
var length = array == null ? 0 : array.length;
if (!length) {
return -1;
}
var index = fromIndex == null ? 0 : toInteger(fromIndex);
if (index < 0) {
index = nativeMax(length + index, 0); // Math.max()
}
return baseFindIndex(array, getIteratee(predicate, 3), index);
}
function findIndex(array, predicate, fromIndex) {
// array ๊ฐ ์ ํจํ์ง ๊ฒ์ฆํด์ฃผ๋ ์ฌ๋ฌ๊ฐ์ง ์์
var length = array == null ? 0 : array.length;
// length ์์ฒด๊ฐ false์ด๋ฉด ์์๊ฐ ์๋์ง ๊ฒ์ฆ์ด ์๋๋ฏ๋ก ์๋ค๋ -1 ์ ๋ฆฌํด
if (!length) {
return -1;
}
// index ์์ฒด๊ฐ ์๋ค์ด ์ฌ๋ 0๋ฒ์งธ index ๋ถํฐ ๋ณผ์ ์๋๋ก
var index = fromIndex == null ? 0 : toInteger(fromIndex);
// index์ 0 ๋ณด๋ค ์์ ์๊ฐ ๋ค์ด์ค๋ฉด, ๊ธธ์ด๋งํผ์ ๋ํ๋ค์๋ ์์์ด๋ฉด 0์ผ๋ก ์์์ด๋ฉด ํด๋น index ๊ธฐ์ค์ผ๋ก ์ ์ฉํด์ค๋ค.
if (index < 0) {
index = nativeMax(length + index, 0);
}
// Math.max(length + index, 0)์ ๊ฐ๋ค๊ณ ๋ณด๋ฉด๋๋ค.
// ๊ทธ๋ฌ๊ณ ๋๋ค baseFindIndexํจ์๋ฅผ ํธ์ถํ๋ค.
return baseFindIndex(array, getIteratee(predicate, 3), index);
}
findIndex๋ฅผ ์๊ธฐ์ํด ์ฌ์ ์ ์ผ๋ก ์์์ผํ๋ ๋๊ฐ์ง ํจ์ getIteratee
์ baseFindIndex
๋ฅผ ๋ณด์
function baseFindIndex(array, predicate, fromIndex, fromRight) {
var length = array.length,
// index๋ fromRight์ parameter๋ฅผ ๋ฃ์ด์คฌ๋ค๋ฉด 1์ ๋ํด์ฃผ๊ณ ์๋๋ฉด -1์ ํด์ค๋ค์ ๋ํด์ค๋ค. ๋นผ์ฃผ๋ ์ด์ ๋ ์๋ while๋ฌธ์์ ๋ํด์ฃผ๋ฉด์ loop์ ๋์์ผ ํ๊ธฐ ๋๋ฌธ์ด๋ค!
index = fromIndex + (fromRight ? 1 : -1);
// while๋ฌธ์ ๋๋ ๊ธฐ์ค์ ์ค๋ฅธ์ชฝ๋ถํฐ ๊ธฐ์ค์ด๋ผ๋ฉด index์์ ํ๋์ฉ ๋นผ๋ฉด์, ์ผ์ชฝ์์๋ถํฐ ์ค๋ฅธ์ชฝ์ด๋ผ๋ฉด index์ 1์ ๋ํ๋ฉด์ ๋ฐฐ์ด์ ๊ธธ์ด ์ ๊น์ง ๋์์ค๋ค .
while ((fromRight ? index-- : ++index < length)) {
//if๋ฌธ์ด true์ผ๋ index๋ฅผ return ํ๊ณ ์์ผ๋ฉด -1์ ๋ฆฌํดํ๋ค.
if (predicate(array[index], index, array)) {
return index;
}
}
return -1;
}
function getIteratee() {
var result = lodash.iteratee || iteratee;
//baseIteratee๋ฅผ ๋ฆฌํดํ๋ ํจ์์ด๊ณ arguments๋ฅผ ๋ฐ์ผ๋ฉด baseIteratee(a,b) ์ด๋ฐ์์ผ๋ก baseIteratee๋ฅผ ํธ์ถํ๊ฒ ๋๋ค.
result = result === iteratee ? baseIteratee : result;
return arguments.length ? result(arguments[0], arguments[1]) : result;
}
function baseIteratee(value) {
if (typeof value == 'function') {
return value;
}
if (value == null) {
return identity;
}
// object๊ฐ ๋ค์ด์ฌ๋ value๊ฐ arrayํ์์ผ๋
if (typeof value == 'object') {
return isArray(value)
//
? baseMatchesProperty(value[0], value[1])
//
: baseMatches(value);
}
//
return property(value);
}
// ์์ ์ค......