aboutsummaryrefslogtreecommitdiff
path: root/src/Format.purs
blob: bed59275e89fe48ad1c3a663b189e3f5d2f7ea97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module Format
  ( number
  , compare
  , unaccent
  ) where

import Data.Int (toNumber, fromNumber)
import Data.Array (replicate)
import Data.Maybe (fromMaybe)
import Data.String (length, joinWith, fromCharArray, toCharArray, toLower)
import Math (round, trunc, pow)

import Prelude hiding (compare)
import Prelude as Prelude

number :: Int -> Number -> String
number decimalLength num = formattedIntegerPart <> formattedDecimalPart
  where
  formattedIntegerPart =
    (if decimalLength > 0 then trunc num else round num)
      # fromNumber
      # fromMaybe 0
      # show

  formattedDecimalPart =
    if decimalLength > 0 then
      ((num - trunc num) * pow 10.0 (toNumber decimalLength))
        # round
        # fromNumber
        # fromMaybe 0
        # show
        # \str -> "," <> (joinWith "" $ replicate (decimalLength - length str) "0") <> str
    else
      ""

compare :: String -> String -> Ordering
compare xs ys = Prelude.compare (formatString xs) (formatString ys)
  where formatString = unaccent <<< toLower

unaccent :: String -> String
unaccent = fromCharArray <<< map unaccentChar <<< toCharArray
  where
  unaccentChar :: Char -> Char
  unaccentChar c = case c of
    'à' -> 'a'
    'á' -> 'a'
    'â' -> 'a'
    'ã' -> 'a'
    'ä' -> 'a'
    'ç' -> 'c'
    'è' -> 'e'
    'é' -> 'e'
    'ê' -> 'e'
    'ë' -> 'e'
    'ì' -> 'i'
    'í' -> 'i'
    'î' -> 'i'
    'ï' -> 'i'
    'ñ' -> 'n'
    'ò' -> 'o'
    'ó' -> 'o'
    'ô' -> 'o'
    'õ' -> 'o'
    'ö' -> 'o'
    'š' -> 's'
    'ù' -> 'u'
    'ú' -> 'u'
    'û' -> 'u'
    'ü' -> 'u'
    'ý' -> 'y'
    'ÿ' -> 'y'
    'ž' -> 'z'
    _ -> c