npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

path_description_language

v1.1.0

Published

helpers for compiling parameterized url paths into usable data for matchers and generators

Downloads

4

Readme

PDL

a mini language to describe parameterized url-style paths for use in routers and url generators

#What does this thing do? on it"s own, not much. It lets you take descriptive strings, or schema objects and compile them into pathData objects which can be used to make things such as url generators and matchers

var pathString = "/static/*path"

var schema = {
	"path":"/wine/:id/:field?",
	"params":{
		"id":{
			"regex":"\d\d\d\d\d\d\d\d"
		},
		"field":{
			"values":["price","age","vintage"]
		}
	}
};

var path1 = pdl.compilePath(pathString)
var path2 = pdl.compilePath(schema)

#so how does the language work? A path is a string which looks like a url path, e.g. "/home/root/beer." A path consists of tokens, each token is preceeded by a /. Tokens can either be regular strings or parameters, and the final token can be a "splat" which represents a number of trailing tokens. Parameters and splats can be made optional.

To make a token a parameter preceed it with a ':' to make a token a splat, preceed it with a '*' and to make either a parameter or a splat optional follow it with a question mark.

as an example in the path "/static/:type/:subtype?/*path" type is a parameter, subtype is an optional parameter, and path is a splat.

#what do pathData objects look like? the compilation of a path gets you a list of objects that can look a few different ways, if a token is a single value you will get:

{
	"type":"value",
	"val":[[the value from the path]],
	"matches": a function that will return true with the input is equal to the value
}

if the token is a parameter you will get:

{
	"type":"param",
	"name":the name of the parameter taken from the path string,
	"matches": a function that either returns true for anything, or a more specific matcher (see below),
	"isOptional": true if this is an optional param, false otherwise
}

and if the token is a splat you will get:

{
	"type":"splat",
	"name":the name of the splat taken from the path string,
	"isOptional": true if this is an optional slpat, false otherwise
}

#how do I restrict what kind of values will match a param? by using a schema! A schema is a POJSO with the following fields

  1. path, a string written in the path description language
  2. params, a map from parameter name to a matcher specification, which restricts the values that will match that param

if no matcher is specified, any value will match the param

#what kind of matchers are there? out of the box there are:

  1. regex: which takes a string that defines a regular expression matching values must match
  2. values: which takes an array of values, one of which matching values must match

there are also two pseudo matchers, value and whatever, which are used for when you have either a non-parameter token or a param with no matcher specified.

you can also add new param matchers if your application needs them by writing to the exported param matchers object.

#what else does this thing export? in order to help build things the PDL module exports 3 things

  1. the compilePath function, which takes a string or schema and returns a pathData object
  2. the tokenizer, which is the function that the PDL uses to break a path into tokens (it's basically jsut string.split('/'))
  3. the param matchers, which are rules used to match a parameter, this can be used to extend the PDLs matching function