lue-bird / elm-morph / String.Morph

Morph for an elm/core String

alter

each : Morph.OneToOne Char Char -> MorphOrError String String error_

Morph.OneToOne each Char in a String

For fallible transformations etc, morph to other structures (but generic ones) like a list first and use its each morph.

only : String -> MorphRow () Char

Match a specific given String and nothing else. This is case sensitive.

import Morph
import List.Morph

-- match an exact text
"abc"
    |> Morph.toNarrow
        (String.Morph.only "abc" |> Morph.rowFinish |> Morph.over List.Morph.string)
--> Ok ()

-- case sensitive
"abC"
    |> Morph.toNarrow
        (String.Morph.only "abc" |> Morph.rowFinish |> Morph.over List.Morph.string)
    |> Result.toMaybe
--> Nothing

-- if there is still input remaining, fail
"abcdef"
    |> Morph.toNarrow
        (String.Morph.only "abc" |> Morph.rowFinish |> Morph.over List.Morph.string)
    |> Result.toMaybe
--> Nothing

See sequenceMap, broadSequenceMap to control what to morph for each Char.

transform

list : MorphOrError String (List Char) error_

Morph.OneToOne from a List Char.

import Morph

[ '0', '1', '2', '3' ] |> Morph.mapTo String.Morph.list
--> "0123"

"0123" |> Morph.toBroad String.Morph.list
--> [ '0', '1', '2', '3' ]

Inverse of String.Morph.list

To use a MorphRow ... Char with a String, use Morph.toNarrow/toBroad with

yourData
    |> Morph.toNarrowOrBroad
        (yourMorphRow
            |> Morph.rowFinish
            |> Morph.over String.Morph.List
        )

stack : MorphOrError String (Emptiable (Stacked Char) Possibly) error_

Morph.OneToOne from a stack of Chars.

import Stack
import Morph

Stack.topBelow '0' [ '1', '2' ]
    |> Morph.mapTo String.Morph.stack
--> "012"

Inverse of Stack.Morph.string

array : MorphOrError String (Array Char) error_

Morph.OneToOne from an Array Char

import Array
import Morph

Array.fromList [ '0', '1', '2', '3' ]
    |> Morph.mapTo String.Morph.array
--> "0123"

Inverse of Array.Morph.string

arraySized : MorphIndependently (ArraySized Char narrowRange_ -> Result error_ String) (String -> ArraySized Char (N.Min (N.Up0 broadX_)))

Morph.OneToOne from an ArraySized of Chars

import ArraySized
import Morph

ArraySized.l4 '0' '1' '2' '3'
    |> Morph.mapTo String.Morph.arraySized
--> "0123"

Inverse of ArraySized.Morph.string

value : Value.Morph.Internal.MorphValue String

String MorphValue

sequence

sequenceMap : (Char -> MorphRow narrowElement broadElement) -> String -> MorphRow (List narrowElement) broadElement

From the chars in a given String, create MorphRows that will be run in the same order, one after the other.

More details → List.Morph.sequenceMap

broadSequenceMap : (Char -> MorphRow () broadElement) -> String -> MorphRow () broadElement

Match broad MorphRows (those that can always produce the same broad value) based on given input elements in sequence.

More details → List.Morph.broadSequenceMap