-
Notifications
You must be signed in to change notification settings - Fork 22
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
3 changed files
with
68 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* 使用 value 来填充(替换) arr,从 start 位置开始, 到 end 位置结束(但不包括 end 位置)。 | ||
* | ||
* @param arr 要填充改变的数组 | ||
* @param [value] 填充(替换)值 | ||
* @param [start=0] 开始位置 | ||
* @param [end=arr.length] 结束位置 | ||
* @returns 填充改变后的数组 | ||
*/ | ||
export default function fill(arr: any[], value?: any, start: number = 0, end: number = arr.length): any[] { | ||
const newArr = arr.slice() | ||
const arrLength = newArr.length | ||
start = Math.max(0, start < 0 ? arrLength + start : start) | ||
end = Math.min(arrLength, end < 0 ? arrLength + end : end) | ||
while (start < end) { | ||
newArr[start++] = value | ||
} | ||
return newArr | ||
} |
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