diff --git a/src/index.ts b/src/index.ts index 0b327c3..a530bb0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -40,7 +40,7 @@ import { base64url } from 'multiformats/bases/base64' /** * Split a multiaddr into path components */ -const toParts = (ma: Multiaddr): string[] => { +const toParts = (ma: Multiaddr | string): string[] => { return ma.toString().split('/').slice(1) } @@ -208,7 +208,7 @@ const and = (...matchers: Matcher[]): Matcher => { } function fmt (...matchers: Matcher[]): MultiaddrMatcher { - function match (ma: Multiaddr): string[] | false { + function match (ma: Multiaddr | string): string[] | false { let parts = toParts(ma) for (const matcher of matchers) { @@ -224,13 +224,13 @@ function fmt (...matchers: Matcher[]): MultiaddrMatcher { return parts } - function matches (ma: Multiaddr): boolean { + function matches (ma: Multiaddr | string): boolean { const result = match(ma) return result !== false } - function exactMatch (ma: Multiaddr): boolean { + function exactMatch (ma: Multiaddr | string): boolean { const result = match(ma) if (result === false) { @@ -255,13 +255,13 @@ export interface MultiaddrMatcher { * Returns true if the passed multiaddr can be treated as this type of * multiaddr */ - matches(ma: Multiaddr): boolean + matches(ma: Multiaddr | string): boolean /** * Returns true if the passed multiaddr terminates as this type of * multiaddr */ - exactMatch(ma: Multiaddr): boolean + exactMatch(ma: Multiaddr | string): boolean } /** diff --git a/test/index.spec.ts b/test/index.spec.ts index 6d81f65..70343a1 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -54,7 +54,7 @@ describe('multiaddr matcher', () => { const goodTCP = [ ...exactTCP, - '/ip4/0.0.7.6/tcp/wss', + '/ip4/0.0.7.6/tcp/80/wss', '/ip6/::/tcp/0/p2p/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79/p2p-circuit/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64', '/dns4/protocol.ai/tcp/80/webrtc' ] @@ -299,7 +299,8 @@ describe('multiaddr matcher', () => { function assertMatches (p: MultiaddrMatcher, ...tests: string[][]): void { tests.forEach((test) => { test.forEach((testcase) => { - expect(p.matches(multiaddr(testcase))).to.equal(true, `${testcase} did not match`) + expect(p.matches(multiaddr(testcase))).to.equal(true, `${testcase} as Multiaddr did not match`) + expect(p.matches(testcase)).to.equal(true, `${testcase} as string did not match`) }) }) } @@ -307,7 +308,8 @@ describe('multiaddr matcher', () => { function assertExactMatches (p: MultiaddrMatcher, ...tests: string[][]): void { tests.forEach((test) => { test.forEach((testcase) => { - expect(p.exactMatch(multiaddr(testcase))).to.equal(true, `${testcase} did not exact match`) + expect(p.exactMatch(multiaddr(testcase))).to.equal(true, `${testcase} as Multiaddr did not exact match`) + expect(p.exactMatch(testcase)).to.equal(true, `${testcase} as string did not exact match`) }) }) } @@ -315,7 +317,8 @@ describe('multiaddr matcher', () => { function assertMismatches (p: MultiaddrMatcher, ...tests: string[][]): void { tests.forEach((test) => { test.forEach((testcase) => { - expect(p.matches(multiaddr(testcase))).to.equal(false, `${testcase} matched when it should not have`) + expect(p.matches(multiaddr(testcase))).to.equal(false, `${testcase} as Multiaddr matched when it should not have`) + expect(p.matches(testcase)).to.equal(false, `${testcase} as string matched when it should not have`) }) }) }