stacks-archive / stacks-transactions-js

The JavaScript library for generating Stacks 2.0 transactions
19 stars 17 forks source link

Avoid extending the native array class #111

Closed njordhov closed 3 years ago

njordhov commented 3 years ago

Describe the bug

Using stack-transactions-js in a project results in the Closure transpiler reporting:

@blockstack/stacks-transactions/lib/utils.js:23
This code cannot be converted from ES6. extending native class: Array

Expected behavior

Preferably avoid extending the native array class, or do it in a way that doesn't get flagged as problematic, potentially like suggested here.

Additional context

The offending code from /lib/utils.js:23 does indeed extend the native array class:

export class BufferArray extends Array<Buffer> {
  appendHexString(hexString: string) {
    this.push(Buffer.from(hexString, 'hex'));
  }

  appendByte(octet: number) {
    if (!Number.isInteger(octet) || octet < 0 || octet > 255) {
      throw new Error(`Value ${octet} is not a valid byte`);
    }
    this.push(Buffer.from([octet]));
  }

  concatBuffer(): Buffer {
    return Buffer.concat(this);
  }
}