Coder Social home page Coder Social logo

urlobj's Introduction

urlobj NPM Version Bower Version Build Status Dependency Status

Performant utilities for URL resolution and parsing built on core url.

This module provides many tools for working with Objects parsed with url.parse(). It also performs faster because it avoids the need to constantly reparse URL Strings during multiple operations.

Constants

component

URL components used in comparison operations.

  • component.NOTHING
  • component.PROTOCOL or components.SCHEME
  • component.TLD
  • component.DOMAIN
  • component.SUB_DOMAIN
  • component.HOSTNAME
  • component.PORT
  • component.HOST
  • component.AUTH
  • component.DIRECTORY
  • component.FILENAME
  • component.PATHNAME
  • component.QUERY or components.SEARCH
  • component.PATH
  • component.HASH or components.FRAGMENT
                                    HOST                         PATH
                                   ___|___                 _______|______
                                  /       \               /              \
               AUTH           HOSTNAME    PORT        PATHNAME          QUERY   HASH
         _______|_______   ______|______   |   __________|_________   ____|____   |
        /               \ /             \ / \ /                    \ /         \ / \
  foo://username:[email protected]:123/hello/world/there.html?name=ferret#foo
  \_/                     \_/ \_____/ \_/     \_________/ \________/
   |                       |     |     |           |           |
PROTOCOL              SUB_DOMAIN |    TLD      DIRECTORY   FILENAME
                                 |
                              DOMAIN

Note: there are a few breaks in the linearity of these values:

  • AUTH is prioritized after HOST because matching authentication on a different domain is pointless
  • TLD is prioritized before DOMAIN because matching a domain on a different top-level domain is pointless
  • SUB_DOMAIN is prioritized after DOMAIN

type

URL types used for discerning input.

  • type.UNKNOWN
  • type.ABSOLUTE
  • type.PROTOCOL_RELATIVE
  • type.ROOT_RELATIVE
  • type.DIRECTORY_RELATIVE
  • type.FILENAME_RELATIVE
  • type.QUERY_RELATIVE
  • type.EMPTY
  • type.HASH_RELATIVE

Functions

The following methods will accept URLs as Strings and/or Objects.

format(urlObj)

Converts a URL Object to a formatted URL String. Is merely an alias to core url.format().

minify(url, options)

Normalizes and minifies a URL with the following options:

  • clone; when set to true, the function will return a copy of url instead of mutating the original.
  • defaultPorts; a map of default ports for various protocols. Default value: {ftp:21, gopher:70, http:80, https:443}.
  • directoryIndexes; a list of filenames that are expected to be treated as directory indexes. Default value: ["index.html"].
  • removeAuth; when set to true, it will remove authentication information. Default value: false.
  • removeDefaultPorts; when set to true, it will remove ports that match any found in defaultPorts. Default value: true.
  • removeDirectoryIndexes; when set to true, it will remove filenames that match any found in directoryIndexes. Default value: true.
  • removeEmptyQueries; when set to true, it will remove empty query data such as "?", "?var=" and "&=". Default value: false.
  • removeRootTrailingSlash; when set to true, it will remove trailing slashes such as "http://domain.com/?var". Default value: true.

If url is an Object, it will be mutated/modified.

normalize(url, options)

Resolves dot segments ("../", "./") in a URL's path, removes port if it is default and removes empty queries ("path/?").

Options:

  • defaultPorts; a map of default ports for various protocols. Default value: {ftp:21, gopher:70, http:80, https:443}.
  • slashesDenoteHost; when set to true, it will parse "//domain.com/" as a URL instead of a path. Default value: false.

If url is an Object, it will be mutated/modified.

parse(url, parseQueryString, slashesDenoteHost)

parse(url, options)

Parses (or re-parses) a URL into an Object containing its URL components with the following options:

  • defaultPorts; a map of default ports for various protocols. Default value: {ftp:21, gopher:70, http:80, https:443}.
  • directoryIndexes; a list of filenames that are expected to be treated as directory indexes. Default value: ["index.html"].
  • parseQueryString; when set to true, it will parse the query string into an object. Default value: false.
  • slashesDenoteHost; when set to true, it will parse "//domain.com/" as a URL instead of a path. Default value: false.

If url is an Object, it will be mutated/modified.

relation(url1, url2)

Returns a Number defining the relation between two URLs. That number corresponds to the value of a URL component in components.

Because the value returned is a Number, more complex comparisons are possible:

var relation = urlobj.relation(url1, url2);

if (relation >= urlobj.components.HOST) {
	console.log("same server!");
}

resolve(from, to, options)

Resolves a URL with a base URL like a browser would for an anchor tag. If to is an Object, it will be mutated/modified.

Options:

  • defaultPorts; a map of default ports for various protocols. Default value: {ftp:21, gopher:70, http:80, https:443}.
  • ignoreWww; when set to true, it will treat "www.domain.com" and "domain.com" as the same host. Default value: false.

Exposed Internal Methods

areSameDir(dirArray1, leadingSlash1, dirArray2, leadingSlash2)

Compares two directory Arrays to see if their paths are the same. leadingSlash1 and leadingSlash2 denote that the corresponding path is absolute and not relative. Input should first be normalized.

areSameQuery(queryObj1, queryObj2)

Compares two query Objects to see if their data is the same. Order does not matter.

joinDirs(dirArray, leadingSlash)

Joins all directories of a directory Array into a String. leadingSlash denotes that the path is absolute and not relative.

joinQuery(queryObj, skipEmpties)

Joins all keys of an Object into a query String.

When skipEmpties is true, empty query data such as "?var=" and "&=" will be excluded. Its default value is false.

normalizeDirs(dirArray, leadingSlash)

Resolves dot segments ("../", "./") in a directory Array and returns a new Array (within an Object). leadingSlash denotes that the path is absolute and not relative. This method will attempt to resolve to a root. If none is found, the parent-most dot segment will remain.

Examples using Strings instead of Arrays:

  • Turns "/dir1/dir2/../" into "/dir1/"
  • Turns "dir/../" into ""
  • Turns "/../dir/" into "/dir/"
  • Leaves "../dir/" untouched
  • Leaves "../../dir/" untouched

parsePath(pathString)

Parses a path String into an Object containing a directory Array and a filename String.

resolveDirs(fromDirArray, fromLeadingSlash, toDirArray, toLeadingSlash)

Resolves a base directory Array to another directory Array and returns a new, normalized Array (within an Object). fromLeadingSlash and toLeadingSlash denote that the corresponding path is absolute and not relative.

Roadmap

urlobj's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

urlobj's Issues

Error thrown in areSameQuery

This issue can easily be replicated by executing this:

   areSameQuery(
       { var1:'123' },
       { var1:'456' }
   );

When two queries are compared, and both contain same-length strings, they will wind up getting compared like arrays, instead of strings.

Should probably add this to the unit tests after fixing.

Cannot read property on linux (using broken-link-checker)

[...]/node_modules/urlobj/lib/parseUrl.js:124
			if (url.search[0] === "?")
			              ^

TypeError: Cannot read property '0' of null
    at Object.parseUrl [as parse] ([...]/node_modules/urlobj/lib/parseUrl.js:124:18)

# or

/usr/lib/node_modules/broken-link-checker/node_modules/urlcache/node_modules/urlobj/lib/parseUrl.js:124
			if (url.search[0] === "?")
			              ^

TypeError: Cannot read property '0' of null
    at Object.parseUrl [as parse] (/usr/lib/node_modules/broken-link-checker/node_modules/urlcache/node_modules/urlobj/lib/parseUrl.js:124:18)

This solves it (line 122):

if (url.search !== "" && url.search)

On windows everything is fine

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.