Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zanonnicola/pathname-matcher

Match a given path name against a URL
https://github.com/zanonnicola/pathname-matcher

es6-modules javascript javascript-library matcher path regex

Last synced: 26 days ago
JSON representation

Match a given path name against a URL

Awesome Lists containing this project

README

        

# pathname-matcher

Match a given path name against a URL

## :package: Installation

```bash
npm install pathname-matcher --save
```

## :rocket: Load

```js
// using es modules
import pathnameMatcher from 'pathname-matcher'

// common.js
const pathnameMatcher = require('pathname-matcher')

```

Or use script tags and globals.

```html

```

And then grab it off the global like so:

```js
const isPath = pathnameMatcher.default;
```

## :bulb: Usage

Let's assume you want to run a function if a user navigate to a specific path

```javascript
// www.site.com/hello/search
if (pathnameMatcher(document.location.pathname, 'search')) {
console.log('Hello!!');
}

// www.site.com/search/myPage
if (pathnameMatcher(document.location.pathname, 'search/myPage')) {
console.log('Hello!!');
}
```

> You can provide any path or url as long as it is provided as a string

## API

#### `pathnameMatcher(URL: String, pathname: String)` [required]

Returns `true` if pathname matches the given URL (no trailing slash)

OR `false` when no match.

**pathname** should be passed without `/` at the beginning of the string. See examples above.

## Legal

Released under MIT license.