lodash
- A modern JavaScript utility library delivering modularity, performance & extras.
/**
* 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);
}
/**
* 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;
}