zoukankan      html  css  js  c++  java
  • jsuri 让你方便的处理url

    https://github.com/derek-watson/jsUri

    Pass anything that your browser would recognize as a url to the new Uri() constructor

    var uri =newUri('http://user:pass@www.test.com:81/index.html?q=books#fragment');

    and then use the following accessor methods to get at the various parts.

    uri.protocol();                              // http
    uri
    .userInfo();                              // user:pass
    uri
    .host();                                  // www.test.com
    uri
    .port();                                  // 81
    uri
    .path();                                  // /index.html
    uri
    .query();                                 // q=books
    uri
    .anchor();                                // fragment

    The accessor methods accept an optional value for setting the property

    uri.protocol('https');
    uri
    .toString();                              // https://user:pass@www.test.com:81/index.html?q=books#fragment

    uri
    .host('mydomain.com');
    uri
    .toString();                              // https://user:pass@www.mydomain.com:81/index.html?q=books#fragment

    Fluent Manipulation

    The fluent interface provides a simple way to chain property assignment

    newUri()
       
    .setPath('/index.html')
       
    .setAnchor('content')
       
    .setHost('www.test.com')
       
    .setPort(8080)
       
    .setUserInfo('username:password')
       
    .setProtocol('https')
       
    .setQuery('this=that&some=thing')      // https://username:password@www.test.com:8080/index.html?this=that&some=thing#content

    newUri('http://www.test.com')
       
    .setHost('www.yahoo.com')
       
    .setProtocol('https')                  // https://www.yahoo.com

    newUri()
       
    .setPath('/archives/1979/')
       
    .setQuery('?page=1')                   // /archives/1979?page=1

    Query Parameter Access and Manipulation

    Special methods are available for fetching, building and modifying query string parameters. An emhpasis is placed on query string integrity; duplicate parameter names and values are preserved. Parameter ordering is preserved when possible. URI Components are decoded for comparision, but are otherwise left in their original state.

    Getting query param values by name

    Returns the first query param value for the key

    newUri('?cat=1&cat=2&cat=3').getQueryParamValue('cat')             // 1

    Returns all query param values the key

    newUri('?cat=1&cat=2&cat=3').getQueryParamValues('cat')            // [1, 2, 3]

    Adding query param values

    newUri().addQueryParam('q','books')                         // ?q=books

    newUri('http://www.github.com')
       
    .addQueryParam('testing','123')
       
    .addQueryParam('one',1)                                    // http://www.github.com/?testing=123&one=1

    // insert param at index 0
    newUri('?b=2&c=3&d=4').addQueryParam('a','1',0)            // ?a=1&b=2&c=3&d=4

    Replacing query param values

    Replaces every query string parameter named key with a single instance with the value newVal. If oldValue is supplied, only parameters valuedoldVal will be replaced.

    newUri('?a=1&b=2&c=3')
       
    .replaceQueryParam('a','eh')          // ?a=eh&b=2&c=3

    newUri('?a=1&b=2&c=3&c=4&c=5&c=6')
       
    .replaceQueryParam('c','five','5')   // ?a=1&b=2&c=3&c=4&c=five&c=6

    newUri().replaceQueryParam('page',2)   // ?page=2

    Deleting query param values

    Removes instances of query parameters named key. If value is passed, only params named key and valued value will be deleted.

    newUri('?a=1&b=2&c=3')
       
    .deleteQueryParam('a')                 // ?b=2&c=3

    newUri('test.com?a=1&b=2&c=3&a=eh')
       
    .deleteQueryParam('a','eh')           // test.com/?a=1&b=2&c=3

    Object Cloning

    Duplication (via .clone()) is an easy way to inflate an identical uri object, which you can muck around with as much as you like without destroying the original.

    var baseUri =newUri('http://localhost/');

        baseUri
    .clone().setProtocol('https');  // https://localhost/
        baseUri
    ;                               // http://localhost/
  • 相关阅读:
    OOP侧边分享按钮
    表格基础操作
    行为型模式之自定义语言的实现(解释器模式)
    行为型模式之请求发送者与接收者解耦(命令模式)
    行为型模式之请求的链式处理(职责链模式)
    Http、Socket、WebSocket之间联系与区别
    日期时间工具类DateTimeUtil(基于Java8的LocalDateTime)
    结构型模式之代理模式
    Java8 函数式接口@FunctionalInterface的使用说明
    结构型模式之实现对象的复用(享元模式)
  • 原文地址:https://www.cnblogs.com/zhepama/p/3112008.html
Copyright © 2011-2022 走看看