Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
resolve-pathname
Advanced tools
The resolve-pathname npm package is a utility for resolving target URLs or file paths relative to a base URL or file path. It is particularly useful in web development for handling relative paths and ensuring that URLs are correctly formed.
Resolve Relative Path
This feature allows you to resolve a relative path against a base path. In this example, 'bar' is resolved relative to '/foo', resulting in '/foo/bar'.
const resolvePathname = require('resolve-pathname');
const resolvedPath = resolvePathname('bar', '/foo');
console.log(resolvedPath); // Output: '/foo/bar'
Resolve Path with Parent Directory
This feature handles paths that navigate up the directory tree. Here, '../bar' is resolved relative to '/foo/baz', resulting in '/bar'.
const resolvePathname = require('resolve-pathname');
const resolvedPath = resolvePathname('../bar', '/foo/baz');
console.log(resolvedPath); // Output: '/bar'
Resolve Absolute Path
This feature ensures that absolute paths are correctly resolved. In this case, '/bar' remains '/bar' regardless of the base path '/foo'.
const resolvePathname = require('resolve-pathname');
const resolvedPath = resolvePathname('/bar', '/foo');
console.log(resolvedPath); // Output: '/bar'
The 'path' module is a core Node.js module that provides utilities for working with file and directory paths. It offers similar functionalities such as resolving paths, joining paths, and normalizing paths. Unlike resolve-pathname, it is built into Node.js and does not need to be installed separately.
The 'url' module is another core Node.js module that provides utilities for URL resolution and parsing. It can be used to resolve relative URLs against base URLs, similar to resolve-pathname. However, it is more focused on URL manipulation rather than file paths.
The 'resolve-url' package is a lightweight utility for resolving relative URLs against base URLs. It is similar to resolve-pathname but is specifically designed for URL resolution rather than file paths.
resolve-pathname resolves URL pathnames identical to the way browsers resolve the pathname of an <a href>
value. The goals are:
Using npm:
$ npm install --save resolve-pathname
Then, use as you would anything else:
// using ES6 modules
import resolvePathname from 'resolve-pathname'
// using CommonJS modules
var resolvePathname = require('resolve-pathname')
The UMD build is also available on unpkg:
<script src="https://unpkg.com/resolve-pathname/umd/resolve-pathname.min.js"></script>
You can find the library on window.resolvePathname
.
import resolvePathname from 'resolve-pathname'
// Simply pass the pathname you'd like to resolve. Second
// argument is the path we're coming from, or the current
// pathname. It defaults to "/".
resolvePathname('about', '/company/jobs') // /company/about
resolvePathname('../jobs', '/company/team/ceo') // /company/jobs
resolvePathname('about') // /about
resolvePathname('/about') // /about
// Index paths (with a trailing slash) are also supported and
// work the same way as browsers.
resolvePathname('about', '/company/info/') // /company/info/about
// In browsers, it's easy to resolve a URL pathname relative to
// the current page. Just use window.location! e.g. if
// window.location.pathname == '/company/team/ceo' then
resolvePathname('cto', window.location.pathname) // /company/team/cto
resolvePathname('../jobs', window.location.pathname) // /company/jobs
url.resolve
implementation for full URLsFAQs
Resolve URL pathnames using JavaScript
The npm package resolve-pathname receives a total of 2,552,185 weekly downloads. As such, resolve-pathname popularity was classified as popular.
We found that resolve-pathname 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
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.