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

Commit f47e68c

Browse files
alanshawvmx
authored andcommitted
feat: add toString function
This makes stringifying a CID super easy like `${cid}` or `cid+''`. License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 4027108 commit f47e68c

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ class CID {
186186
}
187187
}
188188

189+
toString (base) {
190+
return this.toBaseEncodedString(base)
191+
}
192+
189193
/**
190194
* Serialize to a plain object.
191195
*

test/index.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,19 @@ describe('CID', () => {
199199
CID.isCID(Buffer.from('hello world'))
200200
).to.equal(false)
201201
})
202+
203+
it('.toString() outputs default base encoded CID', () => {
204+
const mhStr = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'
205+
const cid = new CID(mhStr)
206+
expect(`${cid}`).to.equal(mhStr)
207+
})
208+
209+
it('.toString(base) outputs base encoded CID', () => {
210+
const b58v1Str = 'zdj7Wd8AMwqnhJGQCbFxBVodGSBG84TM7Hs1rcJuQMwTyfEDS'
211+
const b32v1Str = 'bafybeidskjjd4zmr7oh6ku6wp72vvbxyibcli2r6if3ocdcy7jjjusvl2u'
212+
const cid = new CID(b58v1Str)
213+
expect(cid.toString('base32')).to.equal(b32v1Str)
214+
})
202215
})
203216

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

0 commit comments

Comments
 (0)