While generating identifiers is the primary purpose for Ananoid, it is also
sometimes useful to parse raw strings into NanoId
instances (eg: when
rehydrating entities from a database). To help facilitate this, Ananoid
provides a few utilites, each of which validates that the string in question
could have been created from a given Alphabet
instance. Here, we parse a URL-safe identifier:
F#
open KnownAlphabets
let nanoId = NanoId.NewId()
nanoId
|> string
|> NanoId.parseAs UrlSafe
|> Option.iter (fun parsed -> printfn $"nanoId: %A{nanoId}, parsed: %A{parsed}")
VB
Imports KnownAlphabets
Dim nanoId = NanoId.NewId()
Dim parsed As NanoId
If UrlSafe.TryParse(nanoId.ToString(), Out parsed) Then
WriteLine($"nanoId: {nanoId}, parsed: {parsed}")
End If
|
C#
open static KnownAlphabets
var nanoId = NanoId.NewId();
if (UrlSafe.TryParse(nanoId.ToString(), out var parsed))
{
WriteLine($"nanoId: {nanoId}, parsed: {parsed}");
}
|
OUT
> dotnet fsi ~/scratches/nanoidparser.fsx
nanoId: ZoCRoGew6hVWYIIimu-p4, parsed: ZoCRoGew6hVWYIIimu-p4
|
Next, we see what happens if a raw string cannot be constituted from a given
alphabet (eg, using a purely numeric alphaber to parse a hexidecimal string):
F#
let nanoId = HexidecimalUppercase.MakeNanoId(size = 32)
match Numbers.ParseNanoId(string nanoId) with
| Some parsed ->
printfn $"nanoId: %A{nanoId}, parsed: %A{parsed}"
| None ->
printfn $"Failed to parse '%A{nanoId}' as a numeric nano identifer!"
VB
Dim nanoId = HexidecimalUppercase.MakeNanoId(size:=32)
Dim parsed As NanoId
If UrlSafe.TryParse(nanoId.ToString(), Out parsed) Then
WriteLine($"nanoId: {nanoId}, parsed: {parsed}")
Else
WriteLine($"Failed to parse '{nanoId}' as a numeric nano identifer!")
End If
|
C#
var nanoId = HexidecimalUppercase.MakeNanoId(size: 32);
var message =
UrlSafe.TryParse(nanoId.ToString(), out var parsed)
? $"nanoId: {nanoId}, parsed: {parsed}"
: $"Failed to parse '{nanoId}' as a numeric nano identifer!";
WriteLine(message);
|
OUT
> dotnet fsi ~/scratches/nanoidparser.fsx
Failed to parse 'B978025EB0903089A89F40E43632587D' as a numeric nano identifer!
|
Finally, it is worth noting: the length of the string being parsed is not
validated. However, by default, strings which are: null
, zero-lengh, or
consist only of whitespace will result in a parsed value of
NanoId.Empty.
F#
let didParse, parsed = NoLookalikes.TryParseNanoId String.Empty
if didParse then
printfn $"Is empty? %b{NanoId.isEmpty parsed}"
VB
Dim parsed As NanoId
If NoLookalikes.TryParseNanoId(String.Empty, parsed) Then
WriteLine($"Is empty? {NanoId.Empty.Equals(parsed)}")
End If
|
C#
if (NoLookalikes.ParseNanoId(string.Empty) is { Value: var parsed })
{
WriteLine($"Is empty? {parsed is {Length: 0}}");
}
|
OUT
> dotnet fsi ~/scratches/nanoidparser.fsx
Is empty? true
|
If more stringent behavior is required, Ananoid provides four functions/methods
an alternative. They are:
Specifically, each of these operations will report failure if the input
provided to them is: null
, zero-length, or consist only of whitespace.
This may be seen in the following examples:
F#
let parsed = null |> NanoId.parseNonEmptyAs Uppercase
printfn $"Did parse? %b{Option.isSome parsed}"
VB
Dim parsed As NanoId
If Uppercase.ParseNonEmptyNanoId(Nothing) Is Nothing Then
WriteLine("Did parse? false")
End If
|
C#
if (Uppercase.TryParseNonEmptyNanoId(null, out _) is false)
{
WriteLine("Did parse? false");
}
|
OUT
> dotnet fsi ~/scratches/nanoidparser.fsx
Did parse? false
|
The library is available under the Mozilla Public License, Version 2.0.
For more information see the project's License file.
val nanoId: obj
Multiple items
val string: value: 'T -> string
--------------------
type string = System.String
module Option
from Microsoft.FSharp.Core
val iter: action: ('T -> unit) -> option: 'T option -> unit
val parsed: obj
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
module String
from Microsoft.FSharp.Core
val isSome: option: 'T option -> bool