andreasewering/elm-placeholder/2.0.0

NOTE: the latest version is 2.2.0

Renamed to anmolitor/elm-placeholder. That is where the repository lives now. It has not been published as a package under the new name.

Versions published as andreasewering/elm-placeholder still install under that name. 4 versions of 4 come from the new location (see their notes).

Rerouted (renamed, verified). The GitHub user or repository was renamed. The tag is still there, at the new name. The Lamdera package server serves a copy recovered from https://github.com/anmolitor/elm-placeholder.

What we checked: The repo moved to github.com/anmolitor/elm-placeholder. Rebuilt with git archive from commit 10ae1f48dab0 of https://github.com/anmolitor/elm-placeholder (zip prefix andreasewering-elm-placeholder-10ae1f4/, America/Los_Angeles times): the zip is byte-identical to the one package.elm-lang.org recorded, SHA-1 5e158b3d0487fb764217c6205f151ed5feb5b1f2.

The Lamdera compiler uses this copy and prints a one-line notice saying so. elm itself fails to download this version.

Details · package.elm-lang.org's zip: https://github.com/andreasewering/elm-placeholder/zipball/2.0.0/

Placeholder.Internal

This module allows defining placeholder parsers based on a non-supported Syntax as well as using the internal Template type for different purposes.

Template

A Template includes an ordered list of placeholders and text segments. Templates guarantee that

List.length segments == List.length placeholders + 1

and they can be converted back to the same String representation they were parsed from.

parseTemplate : Syntax -> String -> Result String Template

Parse a String into a Template based on a Syntax.

syntax : Syntax
syntax = { startSymbol = "${", endSymbol = "}" }


parseTemplate syntax "This is ${name}s example" |> Result.map (always ())


--> Ok ()
parseTemplate syntax "This does not ${makeSense"


--> Err "TODO deadEndsToString" -- (Waiting for upstream fix in elm/parser)
getPlaceholderNames : Template -> List String

Get the names of the placeholders in a Template.

parseTemplate { startSymbol = "${", endSymbol = "}" } "${first} ${snd}"
    |> Result.map getPlaceholderNames
--> Ok ["first", "snd"]

Convert a parsed Template back to its String representation. For any template built with Syntax s,

templateToString >> parseTemplate s == identity


parseTemplate { startSymbol = "${", endSymbol = "}" } "This is ${name}s example"
    |> Result.map templateToString
--> Ok "This is ${name}s example"

Syntax

type alias Syntax =
{ startSymbol : String
, endSymbol : String
}

A Syntax tells the templateParser how to distinguish placeholders from regular text. At the moment, this can only be configured by setting start and end symbol.

parsePlaceholder1 : Syntax -> String -> Result String (F1 String)

Parse a String into a function substituting the argument at the position marked by the Syntax.

exampleOf : Result String (String -> String)
exampleOf = parsePlaceholder1 { startSymbol = "${", endSymbol = "}" } "This is ${name}s example"


Result.map ((|>) "Andy") exampleOf
--> Ok "This is Andys example"


parsePlaceholder1 { startSymbol = "${", endSymbol = "}" } "No placeholder"
--> Err "Expected more placeholders."


parsePlaceholder1 { startSymbol = "${", endSymbol = "}" } "${multiple}${placeholders}"
--> Err "Expected less placeholders."
parsePlaceholder2 : Syntax -> String -> Result String (F2 String)

Parse a String into a function substituting the arguments at the two positions marked by the Syntax.

parsePlaceholder2 { startSymbol = "{", endSymbol = "}" } "{greeting} {target}"
    |> Result.map (\f -> f "Hello" "World")
--> Ok "Hello World"
parsePlaceholder3 : Syntax -> String -> Result String (F3 String)

Parse a String into a function substituting the arguments at the three positions marked by the Syntax.

parsePlaceholder4 : Syntax -> String -> Result String (F4 String)

Parse a String into a function substituting the arguments at the four positions marked by the Syntax.