Last active
September 17, 2018 03:55
-
-
Save venkatperi/2c74f000de5e1c86dd8c50fabbff03c5 to your computer and use it in GitHub Desktop.
For a fixed sized array, adds item to the end of an array and drops elements from the front, if needed
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
/** | |
* For a fixed sized array, adds item to the end of | |
* an array and drops elements from the front, if needed | |
* | |
* @param item {T}- to be added to the end of the array | |
* @param arr {T[]} - the array | |
* @param size {number} - Max size of the array. | |
* @return {T[]} The updated array | |
*/ | |
function pushFixed<T>(item: T, arr: Array<T>, size: number): Array<T> { | |
return arr.slice(Math.max(0, arr.length - size + 1)).concat(item) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment