Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: accept strings as well as Multiaddr instances #25

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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
}

/**
Expand Down
11 changes: 7 additions & 4 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
Expand Down Expand Up @@ -299,23 +299,26 @@ 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`)
})
})
}

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`)
})
})
}

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`)
})
})
}
Expand Down
Loading