How to emulate the takeLast RxJS operator in pure JavaScript