Skip to content

yairopro/to-absolute-url

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

toAbsoluteUrl

A function to resolve relative paths to absolute urls (using stacktrace.js & native URL module).

Instalation

import toAbsoluteUrl from "to-absolute-url"

Usage

Get absolute url of current file
// https://domaine-name.com/path/to/module.js
toAbsoluteUrl() // returns "https://domaine-name.com/path/to/module.js"
Get absolute url of a relative path
// https://domaine-name.com/dir/dir1/module1.js
toAbsoluteUrl("../dir2/module2.js") // returns "https://domaine-name.com/dir/dir2/module2.js"
toAbsoluteUrl("../dir2/module2.js", /*depth*/ 0) // idem
Get absolute url of a relative path from the caller function

Useful when you create modules which receive relative path as parameter.

// https://domaine-name.com/dir/dir1/whereAmI.js
export default function whereAmI(){
    let callerUrl = toAbsoluteUrl(null, 1 /*Depth in the stacktrace. Default value : 0.*/);
    console.log("You a running at " + callerUrl);
}


// https://domaine-name.com/index.js
import whereAmI from "./dir/dir1/whereAmI.js"
whereAmI(); // logs "You are running at https://domaine-name.com/index.js"

About

A function to resolve relative paths from current script's url to absolute urls.

Resources

License

Stars

Watchers

Forks

Packages

No packages published