Say I have a simple Buffer in Node.js like so:
const bytes = Buffer.from('abcdefg');
this buffer instance has slice
and concat
as methods, but I am really not sure how to use these to basically create the functionality of pop/shift/splice of an array.
here are the Buffer docs: https://nodejs.org/api/buffer.html
What I am basically looking to do is read/remove the first X number of bytes, like so:
function read(x){
// return the first x number of bytes from buffer
// and remove those bytes from the buffer
// side-effects be damned for the moment
}
here's what I have but it seems pretty "wrong" to me even though it also seems to work:
let items = Buffer.from('abcdefg');
function read(x){
const b = items.slice(0,x);
items = items.slice(x,items.length);
return b;
}
console.log(String(read(4)));
console.log(String(items));
Is there a better way to do this?
also, I am not sure if read is the right word, but pop would connote an array...what's the right word to use the describe what this function does?