diff --git a/src/index.ts b/src/index.ts index 0a2a920..8285339 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) diff --git a/test/index.spec.ts b/test/index.spec.ts index 475dddc..9a1027e 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -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) => { @@ -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) + }) })