Skip to content

Commit 2d1c612

Browse files
committed
fix: add type guard and mirgation docs
1 parent bd44bca commit 2d1c612

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ js-multiaddr <!-- omit in toc -->
1717

1818
- [Background](#background)
1919
- [What is multiaddr?](#what-is-multiaddr)
20+
- [Migrate to 0.9](#migrate-to-09)
2021
- [Install](#install)
2122
- [NPM](#npm)
2223
- [Browser: `<script>` Tag](#browser-script-tag)
@@ -38,6 +39,45 @@ A standard way to represent addresses that
3839
- have a nice string representation
3940
- encapsulate well
4041

42+
## Migrate to 0.9
43+
Before 0.9 `multiaddr` would return an constructor function.
44+
```js
45+
const multiaddr = require('multiaddr')
46+
47+
const mh1 = multiaddr('/ip4/127.0.0.1/udp/1234')
48+
49+
const mh2 = new multiaddr('/ip4/127.0.0.1/udp/1234')
50+
// both mh1 and mh2 were multiaddr instances
51+
52+
multiaddr.isName()
53+
multiaddr.protocols
54+
// etc
55+
56+
```
57+
In 0.9 `multiaddr` returns a class.
58+
```js
59+
const Multiaddr = require('multiaddr')
60+
// you need to use `new` to create and instance
61+
const mh1 = new Multiaddr('/ip4/127.0.0.1/udp/1234')
62+
```
63+
64+
```js
65+
// The Multiaddr class has a factory method to help migration
66+
const { multiaddr } = require('multiaddr')
67+
68+
const mh1 = multiaddr('/ip4/127.0.0.1/udp/1234')
69+
```
70+
```js
71+
// In case you are using the static methods/getters `fromNodeAddress`, `isName` , `isMultiaddr`, `protocols` and `resolvers`
72+
// You will need to do a couple more changes
73+
const Multiaddr = require('multiaddr')
74+
const { multiaddr, isName } = Multiaddr
75+
76+
// multiaddr.isName() will not work anymore, only the default export has those methods/getters
77+
Multiaddr.isName() // or just `isName()`
78+
79+
```
80+
4181
## Install
4282

4383
```sh

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const resolvers = new Map()
1212
const symbol = Symbol.for('@multiformats/js-multiaddr/multiaddr')
1313

1414
/**
15-
* @typedef {import('./types').Protocol}Protocol
15+
* @typedef {import('./types').Protocol} Protocol
1616
*/
1717

1818
/**
@@ -561,7 +561,7 @@ class Multiaddr {
561561
* Check if object is a CID instance
562562
*
563563
* @param {any} value
564-
* @returns {boolean}
564+
* @returns {value is Multiaddr}
565565
*/
566566
static isMultiaddr (value) {
567567
return value instanceof Multiaddr || Boolean(value && value[symbol])

0 commit comments

Comments
 (0)