git.iko.soy/rowtype-yoga/purescript-yoga-currency
on v1.0.0 help sign in
93 lines 3.0 kB history raw
1// Generates src/Data/Currency.purs from Intl.supportedValuesOf('currency')
3const currencies = Intl.supportedValuesOf("currency").sort()
4const n = currencies.length
5const lines = []
7lines.push(`module Data.Currency where`)
8lines.push(``)
9lines.push(`import Prelude`)
10lines.push(``)
11lines.push(`import Control.Monad.Except (throwError)`)
12lines.push(`import Data.Enum (class Enum, class BoundedEnum, Cardinality(..), enumFromTo)`)
13lines.push(`import Data.Maybe (Maybe(..))`)
14lines.push(`import Foreign (ForeignError(..))`)
15lines.push(`import Yoga.JSON (class ReadForeign, class WriteForeign, readImpl, writeImpl)`)
16lines.push(``)
18// ADT
19lines.push(`data Currency`)
20currencies.forEach((c, i) => {
21 const prefix = i === 0 ? " = " : " | "
22 lines.push(`${prefix}${c}`)
23})
24lines.push(``)
25lines.push(`derive instance Eq Currency`)
26lines.push(`derive instance Ord Currency`)
27lines.push(``)
28lines.push(`instance Show Currency where`)
29lines.push(` show = toString`)
30lines.push(``)
32// Bounded
33lines.push(`instance Bounded Currency where`)
34lines.push(` bottom = ${currencies[0]}`)
35lines.push(` top = ${currencies[n - 1]}`)
36lines.push(``)
38// Enum
39lines.push(`instance Enum Currency where`)
40lines.push(` succ = case _ of`)
41currencies.forEach((c, i) => {
42 if (i < n - 1) lines.push(` ${c} -> Just ${currencies[i + 1]}`)
43 else lines.push(` ${c} -> Nothing`)
44})
45lines.push(` pred = case _ of`)
46currencies.forEach((c, i) => {
47 if (i === 0) lines.push(` ${c} -> Nothing`)
48 else lines.push(` ${c} -> Just ${currencies[i - 1]}`)
49})
50lines.push(``)
52// BoundedEnum
53lines.push(`instance BoundedEnum Currency where`)
54lines.push(` cardinality = Cardinality ${n}`)
55lines.push(` toEnum = case _ of`)
56currencies.forEach((c, i) => lines.push(` ${i} -> Just ${c}`))
57lines.push(` _ -> Nothing`)
58lines.push(` fromEnum = case _ of`)
59currencies.forEach((c, i) => lines.push(` ${c} -> ${i}`))
60lines.push(``)
62// toString
63lines.push(`toString :: Currency -> String`)
64lines.push(`toString = case _ of`)
65currencies.forEach(c => lines.push(` ${c} -> "${c}"`))
66lines.push(``)
68// fromString
69lines.push(`fromString :: String -> Maybe Currency`)
70lines.push(`fromString = case _ of`)
71currencies.forEach(c => lines.push(` "${c}" -> Just ${c}`))
72lines.push(` _ -> Nothing`)
73lines.push(``)
75// allCurrencies
76lines.push(`allCurrencies :: Array Currency`)
77lines.push(`allCurrencies = enumFromTo bottom top`)
78lines.push(``)
80// ReadForeign / WriteForeign
81lines.push(`instance WriteForeign Currency where`)
82lines.push(` writeImpl = toString >>> writeImpl`)
83lines.push(``)
84lines.push(`instance ReadForeign Currency where`)
85lines.push(` readImpl f = do`)
86lines.push(` let parse s = case fromString s of`)
87lines.push(` Just c -> pure c`)
88lines.push(` Nothing -> throwError $ pure $ ForeignError $ "Unknown currency: " <> s`)
89lines.push(` s <- readImpl f`)
90lines.push(` parse s`)
91lines.push(``)
93process.stdout.write(lines.join("\n") + "\n")