mirror of
https://github.com/amark/gun.git
synced 2025-03-30 15:08:33 +00:00
25 lines
885 B
JavaScript
25 lines
885 B
JavaScript
|
|
require('./base64');
|
|
// This is Array extended to have .toString(['utf8'|'hex'|'base64'])
|
|
function SeaArray() {}
|
|
Object.assign(SeaArray, { from: Array.from })
|
|
SeaArray.prototype = Object.create(Array.prototype)
|
|
SeaArray.prototype.toString = function(enc, start, end) { enc = enc || 'utf8'; start = start || 0;
|
|
const length = this.length
|
|
if (enc === 'hex') {
|
|
const buf = new Uint8Array(this)
|
|
return [ ...Array(((end && (end + 1)) || length) - start).keys()]
|
|
.map((i) => buf[ i + start ].toString(16).padStart(2, '0')).join('')
|
|
}
|
|
if (enc === 'utf8') {
|
|
return Array.from(
|
|
{ length: (end || length) - start },
|
|
(_, i) => String.fromCharCode(this[ i + start])
|
|
).join('')
|
|
}
|
|
if (enc === 'base64') {
|
|
return btoa(this)
|
|
}
|
|
}
|
|
module.exports = SeaArray;
|
|
|