Skip to content

Commit a7a5f2d

Browse files
authored
feat: add memory address matcher (#42)
Allows matching memory addresses.
1 parent 883d591 commit a7a5f2d

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,3 +460,21 @@ const _HTTPS = or(
460460
* ```
461461
*/
462462
export const HTTPS = fmt(_HTTPS)
463+
464+
const _Memory = or(
465+
and(literal('memory'), string(), optional(peerId()))
466+
)
467+
468+
/**
469+
* Matches Memory addresses
470+
*
471+
* @example
472+
*
473+
* ```ts
474+
* import { multiaddr } from '@multiformats/multiaddr'
475+
* import { Memory } from '@multiformats/multiaddr-matcher'
476+
*
477+
* Memory.matches(multiaddr('/memory/0xDEADBEEF')) // true
478+
* ```
479+
*/
480+
export const Memory = fmt(_Memory)

test/index.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,20 @@ describe('multiaddr matcher', () => {
309309
'/ip4/0.0.0.0/udp/80/http'
310310
]
311311

312+
const exactMemory = [
313+
'/memory/0xDEADBEEF',
314+
'/memory/0xDEADBEEF/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v'
315+
]
316+
317+
const goodMemory = [
318+
...exactMemory,
319+
'/memory/0xDEADBEEF/webrtc/p2p/12D3KooWQF6Q3i1QkziJQ9mkNNcyFD8GPQz6R6oEvT75wgsVXm4v'
320+
]
321+
322+
const badMemory = [
323+
'/ip4/0.0.0.0/udp/80/http'
324+
]
325+
312326
function assertMatches (p: MultiaddrMatcher, ...tests: string[][]): void {
313327
tests.forEach((test) => {
314328
test.forEach((testcase) => {
@@ -416,4 +430,10 @@ describe('multiaddr matcher', () => {
416430
assertExactMatches(mafmt.HTTPS, exactHTTPS)
417431
assertMismatches(mafmt.HTTPS, badHTTPS)
418432
})
433+
434+
it('Memory addresses', () => {
435+
assertMatches(mafmt.Memory, goodMemory)
436+
assertExactMatches(mafmt.Memory, exactMemory)
437+
assertMismatches(mafmt.Memory, badMemory)
438+
})
419439
})

0 commit comments

Comments
 (0)