LogoLogo
  • Dosaic
  • Hosting
    • WebHost
    • Generator
    • Abstractions
  • Plugins
    • Authorization
      • Abstractions
      • Keycloak
    • Endpoints
      • Abstractions
      • RestResourceEntity
    • Handlers
      • Abstractions
      • CQRS
    • Jobs
      • Hangfire
    • Management
      • Unleash
    • Mapping
      • Mapster
    • Messaging
      • Abstractions
      • MassTransit
    • Persistence
      • Abstractions
      • EntityFramework
      • InMemory
      • MongoDb
      • S3
      • VaultSharp
    • Validations
      • Abstractions
      • Attribute
  • Extensions
    • RestEase
    • Sqids
  • Testing
    • NUnit
Powered by GitBook
On this page
  • Installation
  • Usage
  • Basic Conversion
  • Custom Encoder
  • Default Configuration
  • Use Cases
  1. Extensions

Sqids

Dosaic.Extensions.Sqids is an extension library that provides methods to convert strings to and from Sqid format using the Sqids library.

Installation

To install the nuget package follow these steps:

dotnet add package Dosaic.Extensions.Sqids

or add as package reference to your .csproj

<PackageReference Include="Dosaic.Extensions.Sqids" Version="" />

Usage

The extension provides simple methods to convert strings to and from Sqid format.

Basic Conversion

Convert a string to a Sqid:

using Dosaic.Extensions.Sqids;

string originalString = "HelloWorld";
string sqidString = originalString.ToSqid();

Convert a Sqid back to the original string:

using Dosaic.Extensions.Sqids;

string sqidString = "kKs7PVdXUYnH"; // Example sqid
string originalString = sqidString.FromSqid();

Custom Encoder

You can also use a custom encoder for special use cases:

using Dosaic.Extensions.Sqids;
using Sqids;

var customEncoder = new SqidsEncoder<char>(new SqidsOptions
{
    Alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    MinLength = 8
});

string originalString = "HelloWorld";
string sqidString = originalString.ToSqid(customEncoder);
string decodedString = sqidString.FromSqid(customEncoder);

Default Configuration

By default, the extension uses the following configuration:

  • Alphabet: "kKsW7PVdXUYnHgQ6rujl0GepfNzB2qZ9bC83IyDmOAtJ4hcSvM1Roaw5LxEiTF"

  • Minimum Length: 10

You can modify the default encoder if needed:

using Dosaic.Extensions.Sqids;
using Sqids;

SqidExtensions.Encoder = new SqidsEncoder<char>(new SqidsOptions
{
    Alphabet = "yourCustomAlphabet",
    MinLength = 12
});

Use Cases

Sqids are useful for:

  • Creating URL-friendly identifiers

  • Obfuscating sequential IDs

  • Generating short, unique string identifiers

PreviousRestEaseNextNUnit

Last updated 1 month ago