Skip to content

Instantly share code, notes, and snippets.

@venkatperi
Last active September 17, 2018 03:55
Show Gist options
  • Save venkatperi/2c74f000de5e1c86dd8c50fabbff03c5 to your computer and use it in GitHub Desktop.
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
/**
* 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