foliojs/restructure

[idea] offsetOf for Struct

vshymanskyy opened this issue · 0 comments

It's sometimes useful to be able to know a relative offset of a specific field of a Struct.
Currently this works for me, but I would expect to see such API available out-of-the box ;)
Thanks!

r.Struct.prototype.offsetOf = function(field) {
  let keys = Object.keys(this.fields);
  let offset = 0;
  for (let i=0; i<keys.length; i++) {
    let key = keys[i];
    if (key == field) {
      return offset;
    }

    offset += this.fields[key].size();
  }
  return -1;
}