array.js 885 B

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