troch / path-parser Goto Github PK
View Code? Open in Web Editor NEWA small utility to parse paths.
License: MIT License
A small utility to parse paths.
License: MIT License
Hello everyone!
At first, thank you for that library.
Is there any way to make path-parser treat query parameters in the case-insensitive way?
import Path from 'path-parser';
const path = new Path('/users?offset&limit');
document.write(JSON.stringify(path.test('/users?limIt=15')));
document.write("<br>");
document.write(JSON.stringify(path.test('/users?limIt=15', { caseSensitive: false })));
Will write
null
null
But I want to get {limit: '15'}
.
How can I do it?
It would be very nice to have all params function the same way and be able to constrain a query param to be a specific shape. I often use this kind of functionality for a basic check of id or jwt shape.
Since last version, requiring this package in JS causes an error as seen on this example
Seems there is no support for percent encoding either unencoded or encoded e.g.
const Path = require('path-parser');
new Path('๐ฆ');
Error: Could not parse path '%F0%9F%A6%84'
at tokenise (/Users/alextes/code/notepad/js/node_modules/path-parser/dist/cjs/path-parser.js:95:15)
at new Path (/Users/alextes/code/notepad/js/node_modules/path-parser/dist/cjs/path-parser.js:166:23)
Since percent encoding is allowed in URLs and common in translated routes would you appreciate a PR that supports recognizing %
as part of a fragment?
test case
new Path('/settings/:id').test('/settings/1111@1111')
expect
match with { id: '1111@1111' }
actual
null
When I don't use regex it works fine but I need to restrict my root taxons so it doesn't match everything.
const pathDef = '/:taxon1<(men|women)>/:taxon2/products/:productSlug';
const pathToTest = '/men/t-shirts/products/some-tshirt-slug';
(new Path(pathDef)).test(pathToTest);
// { taxon1: 'men', taxon2: 'men', productSlug: 't-shirts' }
const pathDef = '/:taxon1/:taxon2/products/:productSlug';
const pathToTest = '/men/t-shirts/products/some-tshirt-slug';
(new Path(pathDef)).test(pathToTest);
// { taxon1: 'men', taxon2: 't-shirts', productSlug: 'some-tshirt-slug' }
Version: [email protected], 4.0.3, 4.0.4
NOTE: it is working as expected in
path-parser#4.0.1
const Path = require('path-parser');
const path = new Path('/test/:id/url');
And I am getting TypeError: Path is not a constructor
.
we might need to double check 59f43c5 commit.
Thank you.
Hey there!
Awesome work with this troch, just wanted to let you know that on the last release you've published, it breaks saying that Path is not a constructor.
Just by using const p = new Path('/api/names/:name/:number');
But somehow it says that is not a constructor.
As soon as I downgrade to another version of the package, the same code works just fine!
Would it be possible to get object query parameter support? A common way to support this is using brackets (e.g. express and jQuery):
http://localhost/page?a[b]=10
which serializes into the query parameters: { a: { b: 10 } }
. Given the library supports array query parameters, I think object query parameter support would be a natural evolution. Here's a reasonable implementation: http://api.jquery.com/jquery.param/
It would be fantastic if we could get functionality similar to:
$.param({ a: { b: 1, c: 2 }, d: [ 3, 4, { e: 5 } ] });
// "a[b]=1&a[c]=2&d[]=3&d[]=4&d[2][e]=5"
Last, I would have done this myself if possible, but the regex rules defined in the library are too strict, and are unable to match, for example, ?a[b]=10
.
Please, add example path
and rule
for matrix parameters, can't understand that
If I pass url params object containing url encoded value, it's encoded once again, else it's not encoded at all.
What I'd like to achieve is encoding (once) a param containing a path to be used elswhere. I get %252Fap%252Fexample-func-1.html%2523%252F
instead of %2Fap%2Fexample-func-1.html%23%2F
.
I would like to encode value once, but it seems to be impossible now.
This is cause by this line https://github.com/troch/path-parser/blob/master/modules/Path.ts#L190
If there is no way to prevent auto encoding - I'd be happy to introduce PR with update :)
I use router5 and here is an issue I filed there: router5/router5#404
Any plan to implement an optional paramenter like:
'/users/profile(/:id)'
Nice little library, very usefull and easy to use.
I get base64 encoded ids from my backend. They look like this: TW92aWU6MTI1OA==
(btoa("Movie:1258")
), the parser can't handle these ==
in the strings. Could this be easily fixed somehow?
Is there a way of adding the hash as a parameter (or even query parameters in the hash) and if not, should it be added as a feature?
If I include a "#" character in a route, I get the following error:
Error: Could not parse path.
at tokenise (removed non-helpful location in compiled code)
After a short look at the code I found out that quick fix would probably be to add "#" to the list of delimiters.
I mean adding something like:
// allow call as function
if (!(this instanceof Path))
return new Path(path);
I guess it comes down to personal preference, having function-style call support make using path-parser
directly easier (which I am doing at the moment, having to write a bunch of new Path
just seem a bit weird).
I was under the impression partialTest
operated a little differently. For example:
const p = new Path('/:foo/:bar');
p.partialTest('/baz') // #=> null
actual result: null
expected result: {foo: 'baz'}
Is it possible to use the API for partial URL matches? My url will sometimes have a :bar
and sometimes it won't.
A route like /clubs/:slug/posts?skip=0&limit=20
will cause parser to throw error. My expectation would be to leave querystring as it is.
This maybe tricky to do, would checking each query contains =
be enough?
A harder case would be /clubs/:slug/posts?skip&limit=20
, in this case user may expect build({ slug: 'abc', skip: 0 })
to work.
When query parameters are specified in the path pattern, it does not match paths that omit all query parameters
import { Path } from 'path-parser';
const path = new Path('/foo?:bar');
// Returns {bar: 1} as expected
console.log(path.test('/foo?bar=1'));
// Fails to match, returns null. I expect this to return {foo: undefined}
console.log(path.test('/foo'));
Seems like the reason this is happening is that the full path is passed into search-string
for query param parsing (
Line 187 in 2ee0233
/foo
, search-string
will try to interpret /foo
as a query param key, and end up returning {'/foo': null}
. This causes path matching to fail, because /foo
is an unexpected keyI understand most libraries use:
/users/:id
but Swagger, the no1 lib for defining API contracts and mocking, uses:
/users/{id}
I need to parse the Swagger-style syntax.
This is a request for that support to be added.
Thanks
With path test: ":collection/:filename"
And actual path containing a +
: \foo\something+1.png
partialTest
returns null
It works if I change the default in defaultOrConstrained
to this: "[a-zA-Z0-9-_.~%':|\+]+"
(add \+
)
Is this a bug or omitted for a reason? +
is a valid literal non-escaped character in a path segment.
Here TestMatch
type is defined as really generic object | null
, but if we look at the code in the urlTest
method (which computes and returns the TestMatch
) the right definition should be {[k: string]: string} | null
(a map instead of an object).
I guess that this would be a breaking change, even if only for Typescript users, and it will lead to bumping the major version of the package.
PR very soon ;)
I already asked on the router5 gitter room, but got no response. I think my question is at first related to path-parser.
Is it possible to match a route/url with multiple occurrences of the same param to an array?!
For example: I need to turn a url like /users?filter=foo&filter=bar into a params object which looks like {filter: ['foo', 'bar']}.
Or is there any other pattern for handling arrays without doing manual string splitting?!
It is common to use literal plus sign in query string as a synonym of space, and many websites support this:
https://google.com?q=foo+bar.
The w3c spec says:
Within the query string, the plus sign is reserved as shorthand notation for a space. Therefore, real plus signs must be encoded. This method was used to make query URIs easier to pass in systems which did not allow spaces.
However, there is a debate whether url encoding / decoding should treat plus signs this way, see https://stackoverflow.com/questions/5450190/how-to-encode-the-plus-symbol-in-a-url.
Can path-parser
(and router5
) have an option to enable such encoding/decoding of plus signs?
Hi,
in url-pattern documentation there is a way to specify optional parameter through path/(:id) syntax (optional segments, wildcards and escaping).
However this module, doesn't understand it, error is: Could not parse path.
I just upgraded from version 3.x.x to 4.2.0, and got the above error. And makes sense since, in 4.x.x, require('path-parser')
returns an Object:
const PathParser = require('path-parser');
> PathParser
{ Path: { [Function: Path] createPath: [Function] },
default: { [Function: Path] createPath: [Function] } }
So, if used in Node, const PathParser = require('path-parser').default
is required. But the CHANGELOG says nothing about it.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.