Skip to content

Instantly share code, notes, and snippets.

@frankmeola
Created April 30, 2014 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankmeola/9c106aad9d853d6b4229 to your computer and use it in GitHub Desktop.
Save frankmeola/9c106aad9d853d6b4229 to your computer and use it in GitHub Desktop.
Sculpt your code in a REPL blog post code
#load "Sculpt.fs"
open Sculpt.Experimentation
open System
let toSlug (filename:string) =
let replace (old:string) (n:string) (statement:string) =
statement.Replace(old, n)
let singleSpace (statement:string) =
System.Text.RegularExpressions.Regex.Replace(statement, @"[\s]+", " ")
filename.ToLower() |> replace "-" "" |> singleSpace |> replace " " "-"
let isWellFormedRelativeUri (uri:string) =
let knownGoodUriRoot = "http://demo.com/"
let absoluteUrl = knownGoodUriRoot+uri
(String.IsNullOrWhiteSpace(absoluteUrl) = false)
&& Uri.IsWellFormedUriString(absoluteUrl, UriKind.RelativeOrAbsolute)
&& (fst (Uri.TryCreate(absoluteUrl, UriKind.RelativeOrAbsolute)))
experiment "Convert a blog into a valid url" (fun _ ->
hypothesis "A blog title can be converted into a valid url"
assumption "I can determine when a url is invalid" (fun _ ->
expect (("something??/hello there" |> isWellFormedRelativeUri) = false)
)
assumption "I can determine when a url is valid" (fun _ ->
expect (("something??/hello-there" |> isWellFormedRelativeUri))
)
example "Simple one word title" (fun _ ->
let slug = toSlug "demo"
expect ("demo" = slug)
expect (slug |> isWellFormedRelativeUri)
)
example "Capitalized one word title" (fun _ ->
let slug = toSlug "Demo"
expect ("demo" = slug)
expect (slug |> isWellFormedRelativeUri)
)
example "Phrase as a title" (fun _ ->
let slug = toSlug "This post is amazing"
expect ("this-post-is-amazing" = slug)
expect (slug |> isWellFormedRelativeUri)
)
example "Phrase as a title with dashes" (fun _ ->
let slug = toSlug "This post is amazing - No Joke"
expect ("this-post-is-amazing-no-joke" = slug)
expect (slug |> isWellFormedRelativeUri)
)
findings "Creating a url slug from a title is possible! While this method does not account for UTF-8 encodings or foreign languages it solves the cases we were after."
)
namespace Sculpt
module Experimentation =
open System
let startExperiment name = printfn "Conducting Experiment) %s" name
let successfulExperiment name = printfn "\r\n The experiment (%s) has concluded successfully. Huzzah!" name
let stateHypothesis theory = printfn "\r\n Hypothesis: %s\r\n" theory
let startExample name = printf " Example) %s - " name
let successfulExample status = printfn "%s\r\n" status
let presumeAssumption desc = printf " Assumption) %s - " desc
let assumptionIsValid status = printfn "%s\r\n" status
let assumptionIsInvalid (ex:Exception) = printfn "INVALID: %s\r\n" ex.Message
let failedExample (ex:Exception) = printfn "FAILED: %s\r\n" ex.Message
let reportFindings conclusion = printf " Findings) %s\r\n" conclusion
let experiment (experimentName:string) (contents: unit -> unit) =
startExperiment experimentName
contents()
successfulExperiment experimentName
let hypothesis (theory:string) = stateHypothesis theory
let assumption (description:string) (workspace: unit -> unit) =
presumeAssumption description
try
workspace()
assumptionIsValid "VALID"
with
| ex -> assumptionIsInvalid ex; raise(ex)
let example (exampleName:string) (workspace: unit -> unit) =
startExample exampleName
try
workspace()
successfulExample "PASSED"
with
| ex -> failedExample ex; raise(ex)
let expect (result:bool) =
if false = result then
raise(Exception("Unexpected result"))
let findings (conclusion:string) = reportFindings conclusion
type ExperimentBase() as this =
member this.experiment(experimentName:string, contents:Action) =
experiment experimentName (fun _ -> contents.Invoke())
member this.hypothesis(theory:string) =
hypothesis theory
member this.assumption(description:string, workspace:Action) =
assumption description (fun _ -> workspace.Invoke())
member this.example(exampleName:string, workspace:Action) =
example exampleName (fun _ -> workspace.Invoke())
member this.expect(result:bool) =
expect result
member this.findings(conclusion:string) =
findings conclusion
<Query Kind="Program">
<Reference Relative="bin\Debug\Sculpt.dll"><path to compiled dll>\bin\Debug\Sculpt.dll</Reference>
</Query>
void Main()
{
new SlugExperiment();
}
public class SlugExperiment : Sculpt.Experimentation.ExperimentBase
{
public SlugExperiment()
{
experiment("Convert a blog into a valid url", () => {
hypothesis("A blog title can be converted into a valid url");
assumption("I can determine when a url is invalid", () => {
expect(isWellFormedRelativeUri("something??/hello there") == false);
});
assumption("I can determine when a url is valid", () => {
expect(isWellFormedRelativeUri("something??/hello-there"));
});
example("Simple one word title", () => {
var slug = toSlug("demo");
expect("demo" == slug);
expect(isWellFormedRelativeUri(slug));
});
example("Capitalized one word title", () => {
var slug = toSlug("Demo");
expect ("demo" == slug);
expect (isWellFormedRelativeUri(slug));
});
example("Phrase as a title", () => {
var slug = toSlug("This post is amazing");
expect ("this-post-is-amazing" == slug);
expect (isWellFormedRelativeUri(slug));
});
example("Phrase as a title with dashes", () => {
var slug = toSlug("This post is amazing - No Joke");
expect ("this-post-is-amazing-no-joke" == slug);
expect (isWellFormedRelativeUri(slug));
});
findings("Creating a url slug from a title is possible! While this method does not account for UTF-8 encodings or foreign languages it solves the cases we were after.");
});
}
public bool isWellFormedRelativeUri(string uri)
{
var knownGoodUriRoot = "http://demo.com/";
var absoluteUrl = knownGoodUriRoot+uri;
Uri tmp;
return (string.IsNullOrWhiteSpace(absoluteUrl) == false)
&& Uri.IsWellFormedUriString(absoluteUrl, UriKind.RelativeOrAbsolute)
&& (Uri.TryCreate(absoluteUrl, UriKind.RelativeOrAbsolute, out tmp));
}
public string toSlug(string filename)
{
Func<string, string> singleSpace = (string statement) => System.Text.RegularExpressions.Regex.Replace(statement, @"[\s]+", " ");
return singleSpace(filename.ToLower().Replace("-", "")).Replace(" ", "-");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment