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/
  • 相关阅读:
    大数据实际应用及业务架构
    Hadoop 2.x 生态系统及技术架构图
    网站推广,经验分享
    生成数据字典
    检查sql执行效率
    DBobjectsCompareScript(数据库对象比较).sql
    秒杀多线程第一篇 多线程笔试面试题汇总
    二叉树基本操作(C++)
    生成器模式Builder
    delphi接口(抄自万一)
  • 原文地址:https://www.cnblogs.com/zhepama/p/3112008.html
Copyright © 2011-2022 走看看