array.js 936 B

1234567891011121314151617181920212223242526
  1. function btoa(b) {
  2. return new Buffer(b).toString('base64');
  3. };
  4. // This is Array extended to have .toString(['utf8'|'hex'|'base64'])
  5. function SeaArray() {}
  6. Object.assign(SeaArray, { from: Array.from })
  7. SeaArray.prototype = Object.create(Array.prototype)
  8. SeaArray.prototype.toString = function(enc, start, end) { enc = enc || 'utf8'; start = start || 0;
  9. const length = this.length
  10. if (enc === 'hex') {
  11. const buf = new Uint8Array(this)
  12. return [ ...Array(((end && (end + 1)) || length) - start).keys()]
  13. .map((i) => buf[ i + start ].toString(16).padStart(2, '0')).join('')
  14. }
  15. if (enc === 'utf8') {
  16. return Array.from(
  17. { length: (end || length) - start },
  18. (_, i) => String.fromCharCode(this[ i + start])
  19. ).join('')
  20. }
  21. if (enc === 'base64') {
  22. return btoa(this)
  23. }
  24. }
  25. module.exports = SeaArray;