feat: change toPrint output to match go implementation

This commit is contained in:
Michael Garvin
2018-06-20 14:03:14 -07:00
committed by David Dias
parent a786018528
commit e8ab1b9281
3 changed files with 17 additions and 4 deletions

View File

@ -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)`

View File

@ -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

View File

@ -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()
}) })
}) })