path-to-regexp vs uri-js vs query-string vs url-template vs uri-template vs template-url
"URL 和 URI 處理"npm套件對比
3 年
path-to-regexpuri-jsquery-stringurl-templateuri-templatetemplate-url類似套件:
URL 和 URI 處理是什麼?

這些庫提供了處理 URL 和 URI 的工具,包括解析、格式化、編碼和解碼查詢字串、路由參數等。它們在 Web 開發中非常有用,特別是在處理 API 請求、路由和動態生成 URL 時。這些庫各有特點,適用於不同的場景和需求。

npm下載趨勢
GitHub Stars 排名
統計詳情
套件
下載數
Stars
大小
Issues
發布時間
許可
path-to-regexp74,950,434
8,44355.2 kB2610 個月前MIT
uri-js54,617,127
313-305 年前BSD-2-Clause
query-string12,899,686
6,87253 kB291 個月前MIT
url-template4,889,771
1877.99 kB12 年前BSD-3-Clause
uri-template130,345
42-14 年前MIT
template-url55,318
2-1410 年前ISC
功能比較: path-to-regexp vs uri-js vs query-string vs url-template vs uri-template vs template-url

URL 解析與序列化

  • path-to-regexp:

    path-to-regexp 專注於路由模式的解析,並不處理查詢字串或片段。

  • uri-js:

    uri-js 提供全面的 URI 解析和序列化,支持各種 URI 組件。

  • query-string:

    query-string 提供查詢字串的解析和序列化,支持嵌套物件和陣列。

  • url-template:

    url-template 提供基於模板的 URL 生成,解析功能較為簡單。

  • uri-template:

    uri-template 專注於根據模板生成 URL,不處理查詢字串。

  • template-url:

    template-url 不處理解析,專注於根據模板生成 URL。

查詢字串處理

  • path-to-regexp:

    不處理查詢字串。

  • uri-js:

    提供基本的查詢字串處理,但不專注於此。

  • query-string:

    提供強大的查詢字串解析和序列化功能,支持嵌套和陣列。

  • url-template:

    不處理查詢字串。

  • uri-template:

    不處理查詢字串。

  • template-url:

    不處理查詢字串。

模板支持

  • path-to-regexp:

    不支持模板。

  • uri-js:

    不支持模板。

  • query-string:

    不支持模板。

  • url-template:

    支持基於模板生成 URL,解析模板中的佔位符。

  • uri-template:

    支持根據 URI 模板生成 URL,符合 RFC 6570 標準。

  • template-url:

    支持使用佔位符和自定義分隔符生成 URL。

輕量級與性能

  • path-to-regexp:

    path-to-regexp 非常輕量,性能優越,特別是在處理路由時。

  • uri-js:

    uri-js 相對較大,但提供全面的 URI 處理功能,性能在可接受範圍內。

  • query-string:

    query-string 也很輕量,對查詢字串的解析和序列化非常高效。

  • url-template:

    url-template 輕量級,適合需要快速模板解析和生成的應用。

  • uri-template:

    uri-template 輕量,專注於模板處理,性能良好。

  • template-url:

    template-url 輕量且性能良好,適合快速生成 URL。

代碼示例

  • path-to-regexp:

    路由參數解析示例

    import { pathToRegexp, match } from 'path-to-regexp';
    
    const pattern = '/users/:id';
    const keys = [];
    const regexp = pathToRegexp(pattern, keys);
    const result = regexp.exec('/users/123');
    
    console.log(result); // [ '/users/123', '123', index: 0, input: '/users/123', groups: undefined ]
    console.log(keys); // [ { name: 'id', prefix: ':', delimiter: '/', optional: false, repeat: false } ]
    
  • uri-js:

    URI 解析示例

    import { URI } from 'uri-js';
    
    const uri = URI.parse('https://example.com:8080/path?query=123#fragment');
    console.log(uri);
    // { scheme: 'https', authority: 'example.com:8080', path: '/path', query: 'query=123', fragment: 'fragment' }
    
    const uriString = URI.serialize(uri);
    console.log(uriString);
    // https://example.com:8080/path?query=123#fragment
    
  • query-string:

    查詢字串解析示例

    import queryString from 'query-string';
    
    const parsed = queryString.parse('?name=John&age=30&hobbies[]=reading&hobbies[]=traveling');
    console.log(parsed);
    // { name: 'John', age: '30', hobbies: [ 'reading', 'traveling' ] }
    
    const stringified = queryString.stringify(parsed);
    console.log(stringified);
    // name=John&age=30&hobbies%5B0%5D=reading&hobbies%5B1%5D=traveling
    
  • url-template:

    URL 模板示例

    import { parseTemplate, buildUrl } from 'url-template';
    
    const template = parseTemplate('/users/{id}/posts/{postId}');
    const url = template.expand({ id: 123, postId: 456 });
    console.log(url); // /users/123/posts/456
    
  • uri-template:

    URI 模板生成示例

    import { UriTemplate } from 'uri-template';
    
    const template = new UriTemplate('/users/{id}/posts/{postId}');
    const url = template.expand({ id: 123, postId: 456 });
    console.log(url); // /users/123/posts/456
    
  • template-url:

    URL 模板生成示例

    import templateUrl from 'template-url';
    
    const url = templateUrl('/users/{id}/posts/{postId}', { id: 123, postId: 456 });
    console.log(url); // /users/123/posts/456
    
如何選擇: path-to-regexp vs uri-js vs query-string vs url-template vs uri-template vs template-url
  • path-to-regexp:

    如果您需要將路由模式轉換為正則表達式,或從 URL 中提取參數,path-to-regexp 是一個輕量級且高效的選擇。它特別適合用於路由庫或需要自定義 URL 解析的應用。

  • uri-js:

    uri-js 是一個全面的 URI 處理庫,支持解析、格式化和驗證 URI。它遵循 RFC 標準,適合需要處理各種 URI 類型和進行嚴格驗證的應用。

  • query-string:

    選擇 query-string 如果您需要一個簡單且高效的工具來解析和序列化查詢字串。它支持嵌套物件和陣列的處理,並且 API 直觀易用,適合快速開發。

  • url-template:

    url-template 是一個輕量級的 URI 模板庫,支持解析和生成基於模板的 URL。它簡單易用,適合需要快速集成 URI 模板功能的項目。

  • uri-template:

    選擇 uri-template 如果您需要根據 URI 模板生成 URL。它支持 RFC 6570 標準,適合需要從模板生成動態 URL 的應用,特別是在 API 開發中。

  • template-url:

    如果您需要根據模板動態生成 URL,template-url 提供了一個簡單的解決方案。它支持使用佔位符和自定義分隔符,適合需要根據變數生成 URL 的場景。