Giter Club home page Giter Club logo

chobot's Introduction

Chobot router

Chobot router is universal router solution which is completely independent of any framework. It can easily fit to both React FE applications and NodeJS BE (like Express apps).

Key differences from other libraries:

Non-greedy matching

Router tries to find best possible fit instead of stopping on the first match. This is key feature in modular applications when you cannot always guarantee order of route registration.

Also it can be quite useful for URL schemes containing routes like:

  • /users/:username
  • and /users/new-user.

In this example router is smart enough to match the static route over the one with dynamic username fragment.

URL generation

This library provides utilities for both URL matching and URL construction. It allows you to get rid of building URL fragments all across your code base and let the route definition to be single source of truth.

Custom parameters

Library supports definition of parameter types. Each parameter can be defined with custom parser (used for URL match) and formatter (used for URL construction). The parsers are designed as a "prefix" functions allowing to define nice routes like: /archive/:year-:month-:day.

Library comes with several param types bundled and you can easily define your own or just use plain old regular expressions.

Optional segments

Route can contain any number of optional segments. Just put them into brackets like: /my-route/:id[/view]. They can even be nested and contain multiple parameters.

Zero-copy matching

Matching uses zero-copy method which helps to keep the memory footprint down as well as it provides better performance. This makes it especially suitable for backend applications.

Other mentionable features

  • Rewritten in Typescript (full typings support)
  • Battle tested and almost fully covered with tests
  • Packaged for both CommonJS and ES6 environments
  • Library has close to zero dependencies (just deep-equal to not reinvent the wheel)
  • You can use it with whatever framework you like

Example usage

// Route definitions
// They are defined as a tree-like structure
const rootRoute = new Route(
  {
    name: 'Root',
    path: '/',
  },
  [
    // Index (Homepage)
    new Route({ path: '.', name: 'Homepage' }),
    // About page
    new Route({ path: 'about', name: 'About' }),
    // Article
    new Route({ path: 'articles/:id', name: 'ArticleDetail' }),
  ]
)

// Match URLs
rootRoute.match({ pathname: '/about' })
// Returns {
//   routes: [Route({ name: 'Root', path: '/' }, Route({ name: 'About', path: 'about' }))]
//   params: {}
// }

// Construct URLs
const router = new Router(rootRoute)
router.createUrl('ArticleDetail', { id: 123 }) // Returns '/articles/123'

For more advanced examples please take a look at /test/examples

Development

# Build
lerna run build:esm
lerna run build:cjs

# Format
lerna run format

# Test
lerna run test -- --color
lerna run test:coverage -- --color

chobot's People

Contributors

adam-stanek avatar dependabot[bot] 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.