-- {-# OPTIONS_GHC -funfolding-fun-discount=90 #-}
-- |
-- Module      : Data.Text.Normalize
-- Copyright   : (c) 2016 Harendra Kumar
--
-- License     : BSD-style
-- Maintainer  : harendra.kumar@gmail.com
-- Stability   : experimental
-- Portability : GHC
--
-- Unicode normalization for @Text@ data type.
--
module Data.Text.Normalize
    (
    -- * Normalization Modes
      NormalizationMode(..)
    -- * Normalization API
    , normalize
    ) where

import           Data.Text                             (Text)
import           Data.Unicode.Internal.NormalizeStream ( DecomposeMode(..)
                                                       , stream
                                                       , unstream
                                                       , unstreamC)
import           Data.Unicode.Types                    (NormalizationMode (..))

-- | Perform Unicode normalization on @Text@ according to the specified
-- normalization mode.
normalize :: NormalizationMode -> Text -> Text
normalize :: NormalizationMode -> Text -> Text
normalize NormalizationMode
mode =
    case NormalizationMode
mode of
      NormalizationMode
NFD  -> (DecomposeMode -> Stream Char -> Text
unstream DecomposeMode
DecomposeNFD)   (Stream Char -> Text) -> (Text -> Stream Char) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Stream Char
stream
      NormalizationMode
NFKD -> (DecomposeMode -> Stream Char -> Text
unstream DecomposeMode
DecomposeNFKD)  (Stream Char -> Text) -> (Text -> Stream Char) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Stream Char
stream
      NormalizationMode
NFC  -> (DecomposeMode -> Stream Char -> Text
unstreamC DecomposeMode
DecomposeNFD)  (Stream Char -> Text) -> (Text -> Stream Char) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Stream Char
stream
      NormalizationMode
NFKC -> (DecomposeMode -> Stream Char -> Text
unstreamC DecomposeMode
DecomposeNFKD) (Stream Char -> Text) -> (Text -> Stream Char) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Stream Char
stream