Skip to content

Commit c992dce

Browse files
alanshawdryajov
authored andcommitted
improves readability and adds test cases
1 parent f1d0a3d commit c992dce

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ function Multiaddr (addr) {
2828
}
2929

3030
// default
31-
addr = addr == null ? '' : addr
31+
if (addr == null) {
32+
addr = ''
33+
}
3234

3335
if (addr instanceof Buffer) {
3436
/**

test/index.spec.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,18 @@ describe('construction', () => {
4040
expect(multiaddr('').toString()).to.equal('/')
4141
})
4242

43-
it('throws on non string or buffer', () => {
44-
expect(() => multiaddr({})).to.throw(/addr must be a string/)
43+
it('null/undefined construct still works', () => {
44+
expect(multiaddr().toString()).to.equal('/')
45+
expect(multiaddr(null).toString()).to.equal('/')
46+
expect(multiaddr(undefined).toString()).to.equal('/')
47+
})
48+
49+
it('throws on truthy non string or buffer', () => {
50+
const errRegex = /addr must be a string/
51+
expect(() => multiaddr({})).to.throw(errRegex)
52+
expect(() => multiaddr([])).to.throw(errRegex)
53+
expect(() => multiaddr(138)).to.throw(errRegex)
54+
expect(() => multiaddr(true)).to.throw(errRegex)
4555
})
4656

4757
it('throws on falsy non string or buffer', () => {

0 commit comments

Comments
 (0)