Skip to content

DavidAlphaFox/hashids-fs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hashids-fs

A hashids implementation in F#. It allows the encoding of one or more numbers into a single YouTube-like random string. Please visit http://hashids.org for implementations in other languages.

alt text

Usage in F#

Open the Hashids namespace

open Hashids

Create a configuration

A HashidConfiguration stores the salt, minimum id length, alphabet, and separators.

let config = 
    HashidConfiguration.create 
        { Salt = "this is my salt"
          MinimumHashLength = 0
          Alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
          Separators = "cfhistuCFHISTU" }

Alternatively, the values from the defaultOptions record can be modified.

let config = 
    HashidConfiguration.create 
        { HashidConfiguration.defaultOptions with Salt = "this is my salt" }

If the only thing that needs changing is the salt, use withSalt.

let config = HashidConfiguration.withSalt "this is my salt"

Create an encode and decode function

Create curried versions of Hashid.encode64 and Hashid.decode64 with a baked in configuration.

let encode = Hashid.encode64 config
let decode = Hashid.decode64 config

Encode and decode numbers

let id = encode [| 73L; 88L |]
let numbers = decode id

The resulting id will be rlVfvd.

Usage in C#

Using Hashids in C# is very similar to F# except for the currying. For a more object oriented approach please try Hashids.net (https://github.com/ullmark/hashids.net).

var config = HashidConfiguration.Create("this is my salt");
var id = Hashid.Encode(config, new int[] { 1, 2, 3 });
var numbers = Hashid.Decode(config, id);

Installation

Install the package with NuGet.

Install-Package hashids-fs

About

A hashids (http://hashids.org) implementation in F#

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • F# 100.0%