mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-06-26 13:01:55 +00:00
feat: change toPrint output to match go implementation
This commit is contained in:
committed by
David Dias
parent
a786018528
commit
e8ab1b9281
@ -201,7 +201,9 @@ Returns an `obj` of the form
|
|||||||
|
|
||||||
### `toPrint()`
|
### `toPrint()`
|
||||||
|
|
||||||
Alias for `.toJSON()`.
|
Returns the Peer ID as a printable string.
|
||||||
|
|
||||||
|
Example: `<peer.ID xxxxxx>`
|
||||||
|
|
||||||
### `isEqual(id)`
|
### `isEqual(id)`
|
||||||
|
|
||||||
|
14
src/index.js
14
src/index.js
@ -67,9 +67,19 @@ class PeerId {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pretty print
|
|
||||||
toPrint () {
|
toPrint () {
|
||||||
return this.toJSON()
|
let pid = this.toB58String()
|
||||||
|
// All sha256 nodes start with Qm
|
||||||
|
// We can skip the Qm to make the peer.ID more useful
|
||||||
|
if (pid.startsWith('Qm')) {
|
||||||
|
pid = pid.slice(2)
|
||||||
|
}
|
||||||
|
let maxRunes = 6
|
||||||
|
if (pid.length < maxRunes) {
|
||||||
|
maxRunes = pid.length
|
||||||
|
}
|
||||||
|
|
||||||
|
return '<peer.ID ' + pid.substr(0, maxRunes) + '>'
|
||||||
}
|
}
|
||||||
|
|
||||||
// return the jsonified version of the key, matching the formatting
|
// return the jsonified version of the key, matching the formatting
|
||||||
|
@ -136,9 +136,10 @@ describe('PeerId', () => {
|
|||||||
it('Pretty printing', (done) => {
|
it('Pretty printing', (done) => {
|
||||||
PeerId.create(testOpts, (err, id1) => {
|
PeerId.create(testOpts, (err, id1) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
PeerId.createFromPrivKey(id1.toPrint().privKey, (err, id2) => {
|
PeerId.createFromPrivKey(id1.toJSON().privKey, (err, id2) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
expect(id1.toPrint()).to.be.eql(id2.toPrint())
|
expect(id1.toPrint()).to.be.eql(id2.toPrint())
|
||||||
|
expect(id1.toPrint()).to.equal('<peer.ID ' + id1.toB58String().substr(2, 6) + '>')
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user