lodash - basefill, fill ๐Ÿ”–

Jung Hyun Kimยท2020๋…„ 12์›” 15์ผ
0

lodash

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

lodash

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

1. fill

  • ๋ฐฐ์—ด๋‚ด์˜ ๊ฐ’์„ ํŠน์ • 'value'(2๋ฒˆ์งธ์ธ์ž)๋กœ ์ฑ„์›Œ๋„ฃ๋Š” ํ•จ์ˆ˜(๋ฐ”๊พธ๋Š”)
  • parameter๋ฅผ 4๊ฐœ ๊นŒ์ง€ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ณ  3๋ฒˆ์งธ , 4๋ฒˆ์งธ parameter๋Š” ์ˆซ์ž๋กœ์„œ array์˜ index๊ฐ’ 3๋ฒˆ์งธ ์ž๋ฆฌ~๋ถ€ํ„ฐ 4๋ฒˆ์งธ ์ž๋ฆฌ~๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๋Š” ๊ฐ’๊นŒ์ง€ ๋ฐ”๊พผ๋‹ค ๋ผ๋Š” ์˜๋ฏธ๋ฅผ ํฌํ•จํ•œ๋‹ค.

์ „์ฒด ์ฝ”๋“œ ๐Ÿ’œ

/**
  * Fills elements of `array` with `value` from `start` up to, but not
  * including, `end`.
  *
  * **Note:** This method mutates `array`.
  *
  * @param {Array} array The array to fill.
  * @param {*} value The value to fill `array` with.
  * @param {number} [start=0] The start position.
  * @param {number} [end=array.length] The end position.
  * @returns {Array} Returns `array`.
  * @example
  *
  * var array = [1, 2, 3];
  *
  * _.fill(array, 'a');
  * // => ['a', 'a', 'a']
  *
  * _.fill(Array(3), 2);
  * // => [2, 2, 2]
  *
  * _.fill([4, 6, 8, 10], '*', 1, 3);
  * // => [4, '*', '*', 10]
  */


function fill(array, value, start, end) {
   var length = array == null ? 0 : array.length;
   if (!length) {
     return [];
   }
   if (start && typeof start != 'number' && isIterateeCall(array, value, start)) {
     start = 0;
     end = length;
   }
   return baseFill(array, value, start, end);
 }

๋ถ€๋ถ„ ์ฝ”๋“œ ๐Ÿ’œ


function fill(array, value, start, end) {
  
// ๋นˆ ๋ฐฐ์—ด์ด ๋“ค์–ด ์˜ฌ๋•Œ ์˜ค์ž‘๋™ ํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์–ดํ•˜๋Š” ์ฝ”๋“œ๋กœ์„œ array์— null๊ฐ’์ด ๋“ค์–ด์˜ค๋ฉด fillํ•จ์ˆ˜๋Š” ๋นˆ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค 
      var length = array == null ? 0 : array.length;
      if (!length) {
        return [];
      }
  
// ์„ธ๋ฒˆ์งธ ์ธ์ž๊ฐ€ ๋“ค์–ด์˜ค๋Š”๋ฐ, start ๊ฐ’์ด ๋“ค์–ด์˜ค๋ฉฐ, start์˜ type์ด number๊ฐ€ ์•„๋‹ˆ๊ณ  , isiterateeCallํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ๊ฐ’์ด iteratee call์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
  
// If func is an array or object, the created function returns true for elements that contain the equivalent source properties, otherwise it returns false. ์ด๋ ‡๊ฒŒ ์ฒดํฌ! 
  
// start ๊ฐ’์ด ์•„์˜ˆ ์•ˆ๋“ค์–ด ์™”์„ ๊ฒฝ์šฐ์˜ ์ œํ•œ์€ baseFill์—์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค. 
      if (start && typeof start != 'number' && isIterateeCall(array, value, start)) {
        start = 0;
        end = length;
      }
      return baseFill(array, value, start, end);
    }

2. baseFill

์ „์ฒด์ฝ”๋“œ ๐Ÿ’œ


/**
* The base implementation of `_.fill` without an iteratee call guard.
     *
     * @private
     * @param {Array} array The array to fill.
     * @param {*} value The value to fill `array` with.
     * @param {number} [start=0] The start position.
     * @param {number} [end=array.length] The end position.
     * @returns {Array} Returns `array`.
     */

    function baseFill(array, value, start, end) {
      var length = array.length;
      start = toInteger(start);
      if (start < 0) {
        start = -start > length ? 0 : (length + start);
      }
      end = (end === undefined || end > length) ? length : toInteger(end);
      if (end < 0) {
        end += length;
      }
      end = start > end ? 0 : toLength(end);
      while (start < end) {
        array[start++] = value;
      }
      return array;
    } 

๋ถ€๋ถ„์ฝ”๋“œ ๐Ÿ’œ

/**
* The base implementation of `_.fill` without an iteratee call guard.
     *
     * @private
     * @param {Array} array The array to fill.
     * @param {*} value The value to fill `array` with.
     * @param {number} [start=0] The start position.
     * @param {number} [end=array.length] The end position.
     * @returns {Array} Returns `array`.
*/

    function baseFill(array, value, start, end) {
      var length = array.length;
      
// ์—ฌ๊ธฐ์—์„œ ๊ฐ’์ด undefined๋กœ ๋“ค์–ด์˜จ๋‹ค๋ฉด toInteger(undefined)๋Š” 0 ์„ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ 0๊ฐ’์œผ๋กœ ๊ธฐ๋ณธ ์…‹ํŒ… ํ•ด์ฃผ๋Š” ์‹์œผ๋กœ ๋ณด๋ฉด ๋œ๋‹ค. 
      start = toInteger(start);
      
      if (start < 0) {
        start = -start > length ? 0 : (length + start);
      }
      end = (end === undefined || end > length) ? length : toInteger(end);
      if (end < 0) {
        end += length;
      }
      end = start > end ? 0 : toLength(end);
      while (start < end) {
        array[start++] = value;
      }
      return array;
    } 
profile
์ฝ”๋ฆฐ์ด ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž๐Ÿ’ป๐Ÿ’›๐Ÿค™๐Ÿผ

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