![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
url-composer
Advanced tools
Small lib for parsing and building dynamic URLs
You can install the lib via npm
npm install --save url-composer
or bower
bower install --save url-composer
The library is very simple to use
import url from 'url-composer'
url.build({
host: 'https://github.com',
path: '/:username',
params: { username: 'RasCarlito' },
query: { tab: 'repositories' },
hash: 'your-repos-filter'
})
// "https://github.com/RasCarlito?tab=repositories#your-repos-filter"
Everything is optional. So calling url.build()
without any parameters would just generate an empty String
.
Note: Path and query parameters are encoded using encodeURIComponent
The path option has an advanced syntax to handle injection of parameters.
Like in the first example
import url from 'url-composer'
url.build({
path: '/users/:id',
params: { id: 42 }
})
// "/users/42"
With optional parameters you can make a portion of the path
optional using parentheses.
Depending on the params
passed that portion will be included or left out.
import url from 'url-composer'
const path = '/users/:id(/edit/:section)'
url.build({
path,
params: { id: 42 }
})
// "/users/42"
url.build({
path,
params: { id: 42, section: 'profile' }
})
// "/users/42/edit/profile"
You can test a path to validate that it corresponds to a given schema
import url from 'url-composer'
const path = '/users/:id(/edit/:section)'
// Testing path directly
url.test({ path, url: '/users/42' }) // true
url.test({ path, url: '/something/different' }) // false
// Getting the regex instead
const re = url.regex(path)
re.test('/users/42/edit/profile') // true
You can parse a path to extract the dynamic parts into an Array
or an Object
.
It will also extract the search query if it is present and place it as the last item in the resulting Array
or in a query
key in the resulting Object
.
Missing optional parameters will result to null
in the extracted values.
Lets look at some code to actually see how it works:
import url from 'url-composer'
// Parsing dynamic parts into an Array
url.parse({
path: '/users/42/edit/profile',
definition: '/users/:id(/edit/:section)'
})
// ['42', 'profile', null]
// Parsing dynamic parts into an Object
url.parse({
path: '/users/42/edit/profile',
definition: '/users/:id(/edit/:section)',
object: true
})
// { id: '42', section: 'profile', query: null }
// Parsing a path with a search query
url.parse({
path: '/users/42/edit/profile?expand=true',
definition: '/users/:id(/edit/:section)'
})
// ['42', 'profile', 'expand=true']
// Parsing dynamic parts into an Object
url.parse({
path: '/users/42/edit/profile?expand=true',
definition: '/users/:id(/edit/:section)',
object: true
})
// { id: '42', section: 'profile', query: 'expand=true' }
FAQs
Building dynamic URLs
We found that url-composer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.