mirror of
https://github.com/amark/gun.git
synced 2025-06-07 22:56:42 +00:00
Converted SeaArray to ES6 class & some cleanup
This commit is contained in:
parent
5f29bd5a54
commit
a5bb0716c3
42
sea.js
42
sea.js
@ -52,24 +52,23 @@
|
|||||||
global.localStorage = localStorage
|
global.localStorage = localStorage
|
||||||
}
|
}
|
||||||
// This is Array extended to have .toString(['utf8'|'hex'|'base64'])
|
// This is Array extended to have .toString(['utf8'|'hex'|'base64'])
|
||||||
function SeaArray() {}
|
class SeaArray extends Array {
|
||||||
Object.assign(SeaArray, { from: Array.from })
|
toString(enc = 'utf8', start = 0, end) {
|
||||||
SeaArray.prototype = Object.create(Array.prototype)
|
const { length } = this
|
||||||
SeaArray.prototype.toString = function(enc = 'utf8', start = 0, end) {
|
if (enc === 'hex') {
|
||||||
const { length } = this
|
const buf = new Uint8Array(this)
|
||||||
if (enc === 'hex') {
|
return [ ...Array(((end && (end + 1)) || length) - start).keys()]
|
||||||
const buf = new Uint8Array(this)
|
.map((i) => buf[ i + start ].toString(16).padStart(2, '0')).join('')
|
||||||
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(
|
||||||
if (enc === 'utf8') {
|
{ length: (end || length) - start },
|
||||||
return Array.from(
|
(_, i) => String.fromCharCode(this[ i + start])
|
||||||
{ length: (end || length) - start },
|
).join('')
|
||||||
(_, i) => String.fromCharCode(this[ i + start])
|
}
|
||||||
).join('')
|
if (enc === 'base64') {
|
||||||
}
|
return btoa(this)
|
||||||
if (enc === 'base64') {
|
}
|
||||||
return btoa(this)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,7 +81,7 @@
|
|||||||
console.warn('new SafeBuffer() is depreciated, please use SafeBuffer.from()')
|
console.warn('new SafeBuffer() is depreciated, please use SafeBuffer.from()')
|
||||||
return SafeBuffer.from(...props)
|
return SafeBuffer.from(...props)
|
||||||
}
|
}
|
||||||
SafeBuffer.prototype = Object.create(Array.prototype)
|
SafeBuffer.prototype = Object.create(SeaArray.prototype)
|
||||||
Object.assign(SafeBuffer, {
|
Object.assign(SafeBuffer, {
|
||||||
// (data, enc) where typeof data === 'string' then enc === 'utf8'|'hex'|'base64'
|
// (data, enc) where typeof data === 'string' then enc === 'utf8'|'hex'|'base64'
|
||||||
from() {
|
from() {
|
||||||
@ -103,13 +102,13 @@
|
|||||||
} else if (enc === 'utf8') {
|
} else if (enc === 'utf8') {
|
||||||
const { length } = input
|
const { length } = input
|
||||||
const words = new Uint16Array(length)
|
const words = new Uint16Array(length)
|
||||||
Array.from({ length }, (_, i) => words[i] = input.charCodeAt(i))
|
SeaArray.from({ length }, (_, i) => words[i] = input.charCodeAt(i))
|
||||||
buf = SeaArray.from(words)
|
buf = SeaArray.from(words)
|
||||||
} else if (enc === 'base64') {
|
} else if (enc === 'base64') {
|
||||||
const dec = atob(input)
|
const dec = atob(input)
|
||||||
const { length } = dec
|
const { length } = dec
|
||||||
const bytes = new Uint8Array(length)
|
const bytes = new Uint8Array(length)
|
||||||
Array.from({ length }, (_, i) => bytes[i] = dec.charCodeAt(i))
|
SeaArray.from({ length }, (_, i) => bytes[i] = dec.charCodeAt(i))
|
||||||
buf = SeaArray.from(bytes)
|
buf = SeaArray.from(bytes)
|
||||||
} else if (enc === 'binary') {
|
} else if (enc === 'binary') {
|
||||||
buf = SeaArray.from(input)
|
buf = SeaArray.from(input)
|
||||||
@ -144,7 +143,6 @@
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
SafeBuffer.prototype.from = SafeBuffer.from
|
SafeBuffer.prototype.from = SafeBuffer.from
|
||||||
SafeBuffer.prototype.toString = SeaArray.prototype.toString
|
|
||||||
|
|
||||||
const Buffer = SafeBuffer
|
const Buffer = SafeBuffer
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user