Giter VIP home page Giter VIP logo

path-recognition's Introduction

Path Recognition

This package tackles the issue of trying to match a url with a certain list of possible paths. The paths can be static or dynamics, where dynamic paths are define by starting with a special character.

As well, it is offered some util functions for basic (but painful to research) url parsing.

Table of Contents

Installing

Using npm:

$ npm install path-recognition

Functions

The main function of the package is the Path Recognition one (which will be the first to be explained), but also comes with a little set of extra util functions.

Path Recognition Function

Try to match a certain URL versus a list of possible paths. If the url doesn't match with any path from the list, it will return undefined as part of the response object.

The response has the following to properties:

  1. match: which can be the found path or undefined when not found
  2. params: Hash object that may contain possible params based on dynamic routes. If no param is found, the object will be empty

Dynamic routes need to be identified by certain pattern at the beginning of the string of the element.You can set the identifier for a custom one. THe default one is :. Notice with the given description that to identify such dynamic routes, you need a pattern which the path starts with. So an identifier that encloses the param, like [id], is not valid. (This convention can be seen in Next.js for instance)

Path Trimmer Function

This function will trim the path from both ends of the string from the character /.

Pathname Extractor Function

Obtains the pathname from a cerain URL. The main pain that this function helps solves is to seperate the querystring.

Examples

Path Recognition

Example to recognize with which match a url matches the most.

import pathRecognition from 'path-recognition';

const possibleUrls = [
  '/',
  '/disable',
  '/guests',
  '/invitations',
  '/invitations/pending',
  '/follows/followers',
  '/follows/followings',
  '/:did',
  '/access/:did',
  '/ban/:did',
  '/unban/:did',
  '/follows/unfollow/:did/follower',
  '/follows/unfollow/:did/following',
  '/invitations/:inviteId',
];

const pathToMatch = '/follows/unfollow/did:key:kbd12asco1/follower';

const match = pathRecognition(possibleUrls, pathToMatch);
// Output:
// {
//   match: '/follows/unfollow/:did/follower',
//   params: {
//     did: 'did:key:kbd12asco1',
//   },
// }

Path Trimmer

Example to trim a path from the character / at the beginning and end of the string.

import { pathTrimmer } from 'path-recognition';

const pathToTrim = '/path/to/trim';

const trimmedPath = pathTrimmer(pathToTrim);
// Output:
// 'path/to/trim'

path-recognition's People

Contributors

darwin-luque avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.