Git Product home page Git Product logo

pathcat's Introduction

PathCat Icon

PathCat ๐Ÿพ

NuGet Badge

PathCat is a powerful and flexible URL building library for .NET, inspired by the popular JavaScript library pathcat. It provides an intuitive way to construct URLs with dynamic parameters, offering extensive configuration options to suit various serialization needs.

Features

  • Simple and expressive API for URL construction
  • Support for path placeholders and query parameters
  • Flexible parameter handling (dictionaries, anonymous objects, strongly-typed classes)
  • Customizable serialization formats for various data types
  • Efficient string manipulation using Span<T> and ReadOnlySpan<T>

Installation

Install PathCat via NuGet:

dotnet add package PathCat

Quick Start

using PathCat;

// Basic usage
string url = PathCat.BuildUrl("/users/:id", new { id = 123, filter = "active" });
// Result: "/users/123?filter=active"

// With nested objects
string url = PathCat.BuildUrl("/api/:version/resource/:id", new
{
    version = "v2",
    id = 789,
    options = new { sort = "asc", limit = 10 }
});
// Result: "/api/v2/resource/789?options.sort=asc&options.limit=10"

Advanced Usage

Configuration Options

PathCat offers various configuration options to customize its behavior:

var config = new PathCatConfig
{
    BooleanSerializationFormat = PathCatConfig.BooleanFormat.LowerCase,
    ArraySerializationFormat = PathCatConfig.ArrayFormat.Indexed,
    PropertyNameSerializationFormat = PathCatConfig.PropertyNameFormat.CamelCase,
    ObjectAccessorSerializationFormat = PathCatConfig.ObjectAccessorFormat.IndexBrackets,
    ArrayDelimiter = '|'
};

string url = PathCat.BuildUrl("/api", parameters, config);

Boolean Serialization

  • Default: Uses .NET's default boolean representation
  • LowerCase: Uses "true" or "false"
  • Numeric: Uses "1" or "0"
  • OnOff: Uses "on" or "off"

Array Serialization

  • Default: Repeats the key for each array element
  • Indexed: Uses indexed notation (e.g., items[0]=value)
  • Delimited: Joins array elements with a specified delimiter

Property Name Serialization

  • Default: Uses the original property names
  • CamelCase: Converts property names to camelCase
  • SnakeCase: Converts property names to snake_case

Object Accessor Serialization

  • DotNotation: Uses dot notation for nested objects
  • IndexBrackets: Uses bracket notation for nested objects
  • OmitParent: Flattens nested object structure

Examples

  1. Using different boolean formats:
var config = new PathCatConfig
{
    BooleanSerializationFormat = PathCatConfig.BooleanFormat.Numeric
};
string url = PathCat.BuildUrl("/api", new { enabled = true }, config);
// Result: "/api?enabled=1"
  1. Customizing array serialization:
var config = new PathCatConfig
{
    ArraySerializationFormat = PathCatConfig.ArrayFormat.Delimited,
    ArrayDelimiter = '|'
};
string url = PathCat.BuildUrl("/api", new { tags = new[] { "urgent", "important" } }, config);
// Result: "/api?tags=urgent|important"
  1. Using different property name formats:
var config = new PathCatConfig
{
    PropertyNameSerializationFormat = PathCatConfig.PropertyNameFormat.SnakeCase
};
string url = PathCat.BuildUrl("/api", new { FirstName = "John", LastName = "Doe" }, config);
// Result: "/api?first_name=John&last_name=Doe"
  1. Customizing object accessor format:
var config = new PathCatConfig
{
    ObjectAccessorSerializationFormat = PathCatConfig.ObjectAccessorFormat.IndexBrackets
};
string url = PathCat.BuildUrl("/api", new { user = new { name = "Alice", age = 30 } }, config);
// Result: "/api?user[name]=Alice&user[age]=30"

System.Text.Json Integration

PathCat now supports System.Text.Json serialization settings for property names. This feature allows you to leverage existing System.Text.Json configurations in your URL building process.

To use this feature:

var config = new PathCatConfig
{
    UseSystemTextJsonSerialization = true,
    SystemTextJsonOptions = new JsonSerializerOptions
    {
        PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
        DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
        Converters = { new JsonStringEnumConverter() }
    }
};

var parameters = new
{
    UserName = "JohnDoe",
    IsActive = true,
    LastLoginDate = DateTime.Now,
    UserType = UserType.Admin
};

string url = PathCat.BuildUrl("/api/users", parameters, config);
// Result: "/api/users?userName=JohnDoe&isActive=true&lastLoginDate=2023-05-15T10:30:00&userType=Admin"

Performance

PathCat is designed with performance in mind, utilizing Span<T> and ReadOnlySpan<T> for efficient string manipulation. It employs a pre-allocated buffer to minimize memory allocations during URL construction.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.


PathCat is a partial port of and inspired by the pathcat JavaScript library. While maintaining the core concepts, it has been adapted and enhanced for the .NET ecosystem.

pathcat's People

Contributors

zettersten avatar

Watchers

 avatar

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.