lodash - findIndex ๐Ÿ”–

Jung Hyun Kimยท2021๋…„ 3์›” 11์ผ
0

lodash

๋ชฉ๋ก ๋ณด๊ธฐ
5/5

lodash

  • A modern JavaScript utility library delivering modularity, performance, & extras

https://github.com/lodash/lodash

1. findIndex

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๋ฅผ ๋ณด์ž

2. baseFindIndex()

  • findIndexํ•จ์ˆ˜๋Š” 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;
  }

3. getIteratee()


    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;
    }

4. baseIteratee(value)

    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);
    }
// ์ˆ˜์ •์ค‘...... 
profile
์ฝ”๋ฆฐ์ด ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž๐Ÿ’ป๐Ÿ’›๐Ÿค™๐Ÿผ

0๊ฐœ์˜ ๋Œ“๊ธ€