Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,21 @@ const _HTTPS = or(
* ```
*/
export const HTTPS = fmt(_HTTPS)

const _Memory = or(
and(literal('memory'), string(), optional(peerId()))
)

/**
* Matches Memory addresses
*
* @example
*
* ```ts
* import { multiaddr } from '@multiformats/multiaddr'
* import { Memory } from '@multiformats/multiaddr-matcher'
*
* Memory.matches(multiaddr('/memory/0xDEADBEEF')) // true
* ```
*/
export const Memory = fmt(_Memory)
20 changes: 20 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,20 @@ describe('multiaddr matcher', () => {
'/ip4/0.0.0.0/udp/80/http'
]

const exactMemory = [
'/memory/0xDEADBEEF',
'/memory/0xDEADBEEF/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v'
]

const goodMemory = [
...exactMemory,
'/memory/0xDEADBEEF/webrtc/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v'
]

const badMemory = [
'/ip4/0.0.0.0/udp/80/http'
]

function assertMatches (p: MultiaddrMatcher, ...tests: string[][]): void {
tests.forEach((test) => {
test.forEach((testcase) => {
Expand Down Expand Up @@ -416,4 +430,10 @@ describe('multiaddr matcher', () => {
assertExactMatches(mafmt.HTTPS, exactHTTPS)
assertMismatches(mafmt.HTTPS, badHTTPS)
})

it('Memory addresses', () => {
assertMatches(mafmt.Memory, goodMemory)
assertExactMatches(mafmt.Memory, exactMemory)
assertMismatches(mafmt.Memory, badMemory)
})
})
Loading