Skip to content
This repository has been archived by the owner on Sep 3, 2021. It is now read-only.

Commit

Permalink
feat: add toString function
Browse files Browse the repository at this point in the history
This makes stringifying a CID super easy like `${cid}` or `cid+''`.

License: MIT
Signed-off-by: Alan Shaw <[email protected]>
  • Loading branch information
alanshaw authored and vmx committed Sep 11, 2018
1 parent 4027108 commit f47e68c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ class CID {
}
}

toString (base) {
return this.toBaseEncodedString(base)
}

/**
* Serialize to a plain object.
*
Expand Down
13 changes: 13 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,19 @@ describe('CID', () => {
CID.isCID(Buffer.from('hello world'))
).to.equal(false)
})

it('.toString() outputs default base encoded CID', () => {
const mhStr = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'
const cid = new CID(mhStr)
expect(`${cid}`).to.equal(mhStr)
})

it('.toString(base) outputs base encoded CID', () => {
const b58v1Str = 'zdj7Wd8AMwqnhJGQCbFxBVodGSBG84TM7Hs1rcJuQMwTyfEDS'
const b32v1Str = 'bafybeidskjjd4zmr7oh6ku6wp72vvbxyibcli2r6if3ocdcy7jjjusvl2u'
const cid = new CID(b58v1Str)
expect(cid.toString('base32')).to.equal(b32v1Str)
})
})

describe('throws on invalid inputs', () => {
Expand Down

0 comments on commit f47e68c

Please sign in to comment.