This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
1.0.0 1.0.1
README

ReadingTime

ReadingTime functions and Msg.

Types

type alias Stats = { duration : String , minutes : Float , time : Int , words : Int }

Stats type

Regex

stripTab : String -> String

Remove tabs from string

stripReturn : String -> String

Remove returns from string

stripNewLine : String -> String

Remove new lines from string

Functions

countWords : String -> Int

Split a string and count it's length

getTime : Float -> Int

Multiple a float value by 60 and then 1000

report : Int -> Maybe Float -> Stats

Return a specific Stats record

stats : String -> Maybe Float -> Stats

Intitialise reporting

getRoundedReadingTime : Float -> String

Rount up a Float and convert to string

getMinutes : Int -> Maybe Float -> Float

Calculate the minutes to read a string of words divided by option words per minute (wpm)

getTime : Float -> Int

Multiple a float value by 60 and then 1000

module ReadingTime exposing (..)

{-| ReadingTime functions and Msg.

# Types
@docs Stats

# Regex
@docs stripTab, stripReturn, stripNewLine

# Functions
@docs countWords, getTime, report, stats, getRoundedReadingTime, getMinutes, getTime
-}

import String
import Regex


{-| Stats type
-}
type alias Stats =
    { duration : String
    , minutes : Float
    , time : Int
    , words : Int
    }


{-| Split a string and count it's length
-}
countWords : String -> Int
countWords str =
    if str == "" || str == " " then
        0
    else
        str
            |> String.split " "
            |> List.length


{-| Remove tabs from string
-}
stripTab : String -> String
stripTab str =
    str
        |> Regex.replace Regex.All (Regex.regex "\\t") (\_ -> "")


{-| Remove returns from string
-}
stripReturn : String -> String
stripReturn str =
    str
        |> Regex.replace Regex.All (Regex.regex "\\r") (\_ -> "")


{-| Remove new lines from string
-}
stripNewLine : String -> String
stripNewLine str =
    str
        |> Regex.replace Regex.All (Regex.regex "\\n") (\_ -> "")


{-| Rount up a Float and convert to string
-}
getRoundedReadingTime : Float -> String
getRoundedReadingTime minutes =
    ceiling minutes
        |> toString


{-| Calculate the minutes to read a string of words divided by option words per minute (wpm)
-}
getMinutes : Int -> Maybe Float -> Float
getMinutes words wpm =
    (toFloat words) / (Maybe.withDefault 200 wpm)


{-| Multiple a float value by 60 and then 1000
-}
getTime : Float -> Int
getTime minutes =
    minutes
        * 60
        * 1000
        |> round


{-| Return a specific Stats record
-}
report : Int -> Maybe Float -> Stats
report words wpm =
    let
        minutes =
            getMinutes words wpm
    in
        { duration = (getRoundedReadingTime minutes) ++ " min read"
        , minutes = minutes
        , time = getTime minutes
        , words = words
        }


{-| Intitialise reporting
-}
stats : String -> Maybe Float -> Stats
stats str wpm =
    let
        words =
            str
                |> stripTab
                |> stripNewLine
                |> stripNewLine
                |> countWords
    in
        report words wpm