Coder Social home page Coder Social logo

url.js's Introduction

URL.js

Build Status codecov

Parse and format URLs

Install

URL.js uses ESM & UMD.

The simplest way is to include URL.js in your HTML:

<script src="https://unpkg.com/url-js"></script>

or install it with npm:

npm i -S url-js

then import it as ES Module or CommonJS Module:

import URLJS from 'url-js'; // ESM
    // or
const URLJS = require('url-js'); // CommonJS

Usage

// 1. Parse full URL
var url = URLJS("https://www.duzun.me/path/index.php?var1=223#hash") ->
{
    "protocol": "https:",
    "username": "",
    "password": "",
    "host"    : "www.duzun.me",
    "hostname": "www.duzun.me",
    "port"    : "",
    "pathname": "/path/index.php",
    "search"  : "?var1=223",
    "query"   : "var1=223",
    "hash"    : "#hash",
    "path"    : "/path/index.php?var1=223",
    "origin"  : "https://www.duzun.me",
    "domain"  : "duzun.me",
    "href"    : "https://www.duzun.me/path/index.php?var1=223#hash"
}

String(url) -> "https://www.duzun.me/path/index.php?var1=223#hash"


// 2. Compute full URL for a relative URL
String( URLJS("new/?and=var", url) ) -> "https://www.duzun.me/path/new/?and=var"


// 3. Get part of an URL
URLJS.parseUrl("https://duzun.me/path/index.php", "origin") -> "https://duzun.me"
URLJS.parseUrl("https://duzun.me/path/index.php", "pathname") -> "/path/index.php"
URLJS.parseUrl("https://duzun.com/?var1=2&var2=d%27t&var3", "query", true) -> { var1: "2", var2: "d't", var3: "" }


// 4. Validation
URLJS.is_url('https://duzun.me') -> true
URLJS.is_domain('duzun.me')     -> true


// 5. GET < - > Object conversion
URLJS.fromObject({a:1,b:4}, "?")   -> "?&a=1&b=4"
URLJS.toObject('a=1&b=4')          -> { a: 1, b: 4 }

You can also import distinct functions as ESM:

import toObject from 'url-js/url/toObject';
import fromObject from 'url-js/url/fromObject';
import fromLocation from 'url-js/url/fromLocation';
import parseUrl from 'url-js/url/parseUrl'; // not the same as URLJS.parseUrl(), unless invoked as parseUrl.call(URLJS, href)
import { is_url, is_domain } from 'url-js/url/helpers';
import URLJS from 'url-js/url/URL'; // the same as 'url-js'

Changes in v2.0.0

Some properties of URLJS instances are computed from the base properties and can no longer be set to arbitrary values. Setting these properties to any value would set the values for related base properties:

.origin .host .path .href .domain

Incompatible changes in v1.0.0

When included in global scope with a <script> tag, the name is URLJS now instead of URL, to avoid collision with the new URL API. Thus, there is no need in URLJS.noConflict() any more.

url.js's People

Contributors

dependabot[bot] avatar duzun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

url.js's Issues

URL.js Version Bump

Hello - we are in need of a version bump in order to resolve an internal CVE. Are there any plans to update URL.js?

Thank you!

How to write ts declaration file

I want to use 'url-js' in my React+TypeScript project, but I get an error that the declaration file for module 'url-js' cannot be found. "/Users/admin/Desktop/react_demo_project/ee-openapi-h5/node_modules/url-js/dist/url.js" implicitly has type "any".
Try using npm i --save-dev @types/url-js (if it exists), or add a new declaration (.d.ts) file containing declare module 'url-js'; ts(7016) .

So how to write the declaration file of ts?

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.