選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

README.md 2.3 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # resolve-pathname [![Travis][build-badge]][build] [![npm package][npm-badge]][npm]
  2. [build-badge]: https://img.shields.io/travis/mjackson/resolve-pathname/master.svg?style=flat-square
  3. [build]: https://travis-ci.org/mjackson/resolve-pathname
  4. [npm-badge]: https://img.shields.io/npm/v/resolve-pathname.svg?style=flat-square
  5. [npm]: https://www.npmjs.org/package/resolve-pathname
  6. [resolve-pathname](https://www.npmjs.com/package/resolve-pathname) resolves URL pathnames identical to the way browsers resolve the pathname of an `<a href>` value. The goals are:
  7. - 100% compatibility with browser pathname resolution
  8. - Pure JavaScript implementation (no DOM dependency)
  9. ## Installation
  10. Using [npm](https://www.npmjs.com/):
  11. $ npm install --save resolve-pathname
  12. Then, use as you would anything else:
  13. ```js
  14. // using ES6 modules
  15. import resolvePathname from 'resolve-pathname';
  16. // using CommonJS modules
  17. var resolvePathname = require('resolve-pathname');
  18. ```
  19. The UMD build is also available on [unpkg](https://unpkg.com):
  20. ```html
  21. <script src="https://unpkg.com/resolve-pathname"></script>
  22. ```
  23. You can find the library on `window.resolvePathname`.
  24. ## Usage
  25. ```js
  26. import resolvePathname from 'resolve-pathname';
  27. // Simply pass the pathname you'd like to resolve. Second
  28. // argument is the path we're coming from, or the current
  29. // pathname. It defaults to "/".
  30. resolvePathname('about', '/company/jobs'); // /company/about
  31. resolvePathname('../jobs', '/company/team/ceo'); // /company/jobs
  32. resolvePathname('about'); // /about
  33. resolvePathname('/about'); // /about
  34. // Index paths (with a trailing slash) are also supported and
  35. // work the same way as browsers.
  36. resolvePathname('about', '/company/info/'); // /company/info/about
  37. // In browsers, it's easy to resolve a URL pathname relative to
  38. // the current page. Just use window.location! e.g. if
  39. // window.location.pathname == '/company/team/ceo' then
  40. resolvePathname('cto', window.location.pathname); // /company/team/cto
  41. resolvePathname('../jobs', window.location.pathname); // /company/jobs
  42. ```
  43. ## Prior Work
  44. - [url.resolve](https://nodejs.org/api/url.html#url_url_resolve_from_to) - node's `url.resolve` implementation for full URLs
  45. - [resolve-url](https://www.npmjs.com/package/resolve-url) - A DOM-dependent implementation of the same algorithm