Giter Site home page Giter Site logo

saadtalaat / url-parse Goto Github PK

View Code? Open in Web Editor NEW

This project forked from unshiftio/url-parse

0.0 2.0 0.0 82 KB

Small footprint URL parser that works seamlessly across Node.js and browser environments.

Home Page: http://unshift.io

License: MIT License

JavaScript 100.00%

url-parse's Introduction

url-parse

Made by unshiftVersion npmBuild StatusDependenciesCoverage StatusIRC channel

Sauce Test Status

The url-parse method exposes two different API interfaces. The url interface that you know from Node.js and the new URL interface that is available in the latest browsers.

In version 0.1 we moved from a DOM based parsing solution, using the <a> element, to a full Regular Expression solution. The main reason for this was to make the URL parser available in different JavaScript environments as you don't always have access to the DOM. An example of such environment is the Worker interface. The RegExp based solution didn't work well as it required a lot of lookups causing major problems in FireFox. In version 1.0.0 we ditched the RegExp based solution in favor of a pure string parsing solution which chops up the URL into smaller pieces. This module still has a really small footprint as it has been designed to be used on the client side.

In addition to URL parsing we also expose the bundled querystringify module.

Installation

This module is designed to be used using either browserify or Node.js it's released in the public npm registry and can be installed using:

npm install url-parse

Usage

All examples assume that this library is bootstrapped using:

'use strict';

var URL = require('url-parse');

To parse an URL simply call the URL method with the URL that needs to be transformed into an object.

var url = new URL('https://github.com/foo/bar');

The new keyword is optional but it will save you an extra function invocation. The constructor takes the following arguments:

  • url (String): A string representing an absolute or relative URL.
  • baseURL (Object | String): An object or string representing the base URL to use in case url is a relative URL. This argument is optional and defaults to location in the browser.
  • parser (Boolean | Function): This argument is optional and specifies how to parse the query string. By default it is false so the query string is not parsed. If you pass true the query string is parsed using the embedded querystringify module. If you pass a function the query string will be parsed using this function.

As said above we also support the Node.js interface so you can also use the library in this way:

'use strict';

var parse = require('url-parse')
  , url = parse('https://github.com/foo/bar', true);

The returned url instance contains the following properties:

  • protocol: Requested protocol without slashes (e.g. http:).
  • username: Username of basic authentication.
  • password: Password of basic authentication.
  • auth: Authentication information portion (e.g. username:password).
  • host: Host name with port number.
  • hostname: Host name without port number.
  • port: Optional port number.
  • pathname: URL path.
  • query: Parsed object containing query string, unless parsing is set to false.
  • hash: The "fragment" portion of the URL including the pound-sign (#).
  • href: The full URL.

URL.set(key, value)

A simple helper function to change parts of the URL and propagating it through all properties. When you set a new host you want the same value to be applied to port if has a different port number, hostname so it has a correct name again and href so you have a complete URL.

var parsed = parse('http://google.com/parse-things');

parsed.set('hostname', 'yahoo.com');
console.log(parsed.href); // http://yahoo.com/parse-things

It's aware of default ports so you cannot set a port 80 on an URL which has http as protocol.

URL.toString()

The returned url object comes with a custom toString method which will generate a full URL again when called. The method accepts an extra function which will stringify the query string for you. If you don't supply a function we will use our default method.

var location = url.toString(); // http://example.com/whatever/?qs=32

You would rarely need to use this method as the full URL is also available as href property. If you are using the URL.set method to make changes, this will automatically update.

Testing

The testing of this module is done in 3 different ways:

  1. We have unit tests that run under Node.js. You can run these tests with the npm test command.
  2. Code coverage can be run manually using npm run coverage.
  3. For browser testing we use Sauce Labs and zuul. You can run browser tests using the npm run test-browser command.

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.