git.iko.soy/rowtype-yoga/purescript-money
on master help sign in
9 files changed+1010−189
since splitsnapshots
unifiedsplit
LICENSE+29 README.md+3 bower.json+18−3 src/Data/Finance/Currency.purs+664−166 src/Data/Finance/Money.js−7 src/Data/Finance/Money.purs+111−13 src/Data/Finance/Money/Format.purs+107 test/Data/Finance/Money.purs+69 test/Main.purs+9
LICENSEnew file+29
@@ -0,0 +1,29 @@
1+BSD 3-Clause License
2+
3+Copyright (c) 2017, Tinker.Travel
4+All rights reserved.
5+
6+Redistribution and use in source and binary forms, with or without
7+modification, are permitted provided that the following conditions are met:
8+
9+* Redistributions of source code must retain the above copyright notice, this
10+ list of conditions and the following disclaimer.
11+
12+* Redistributions in binary form must reproduce the above copyright notice,
13+ this list of conditions and the following disclaimer in the documentation
14+ and/or other materials provided with the distribution.
15+
16+* Neither the name of the copyright holder nor the names of its
17+ contributors may be used to endorse or promote products derived from
18+ this software without specific prior written permission.
19+
20+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
README.mdnew file+3
@@ -0,0 +1,3 @@
1+# purescript-money
2+
3+Types and operations on monetary amounts.
bower.json+18−3
@@ -1,8 +1,23 @@
11{
22 "name": "purescript-money",
3+ "license": "BSD-3-Clause",
4+ "repository": {
5+ "type": "git",
6+ "url": "https://github.com/TinkerTravel/purescript-money.git"
7+ },
38 "dependencies": {
4− "purescript-generics": "^3.3.0",
5− "purescript-prelude": "^2.5.0",
6− "purescript-unitted": "git@github.com:TinkerTravel/purescript-unitted.git#^1.0.0"
9+ "purescript-foldable-traversable": "^3.0.0",
10+ "purescript-generics": "^4.0.0",
11+ "purescript-globals": "^3.0.0",
12+ "purescript-integers": "^3.0.0",
13+ "purescript-lists": "^4.0.1",
14+ "purescript-math": "^2.0.0",
15+ "purescript-prelude": "^3.0.0",
16+ "purescript-modules": "^3.0.0",
17+ "purescript-newtype": "^2.0.0",
18+ "purescript-rationals": "^3.0.0"
19+ },
20+ "devDependencies": {
21+ "purescript-test-unit": "^11.0.0"
722 }
823}
src/Data/Finance/Currency.purs+664−166
@@ -5,6 +5,7 @@ foreign import kind Currency
55data CProxy (c :: Currency) = CProxy
66
77class Currency (c :: Currency) where
8+ code :: CProxy c -> String
89 decimals :: CProxy c -> Int
910
1011foreign import data AED :: Currency
@@ -174,169 +175,666 @@ foreign import data ZAR :: Currency
174175foreign import data ZMW :: Currency
175176foreign import data ZWL :: Currency
176177
177−instance currencyAED :: Currency AED where decimals _ = 2
178−instance currencyAFN :: Currency AFN where decimals _ = 2
179−instance currencyALL :: Currency ALL where decimals _ = 2
180−instance currencyAMD :: Currency AMD where decimals _ = 2
181−instance currencyANG :: Currency ANG where decimals _ = 2
182−instance currencyAOA :: Currency AOA where decimals _ = 2
183−instance currencyARS :: Currency ARS where decimals _ = 2
184−instance currencyAUD :: Currency AUD where decimals _ = 2
185−instance currencyAWG :: Currency AWG where decimals _ = 2
186−instance currencyAZN :: Currency AZN where decimals _ = 2
187−instance currencyBAM :: Currency BAM where decimals _ = 2
188−instance currencyBBD :: Currency BBD where decimals _ = 2
189−instance currencyBDT :: Currency BDT where decimals _ = 2
190−instance currencyBGN :: Currency BGN where decimals _ = 2
191−instance currencyBHD :: Currency BHD where decimals _ = 3
192−instance currencyBIF :: Currency BIF where decimals _ = 0
193−instance currencyBMD :: Currency BMD where decimals _ = 2
194−instance currencyBND :: Currency BND where decimals _ = 2
195−instance currencyBOB :: Currency BOB where decimals _ = 2
196−instance currencyBOV :: Currency BOV where decimals _ = 2
197−instance currencyBRL :: Currency BRL where decimals _ = 2
198−instance currencyBSD :: Currency BSD where decimals _ = 2
199−instance currencyBTN :: Currency BTN where decimals _ = 2
200−instance currencyBWP :: Currency BWP where decimals _ = 2
201−instance currencyBYR :: Currency BYR where decimals _ = 0
202−instance currencyBZD :: Currency BZD where decimals _ = 2
203−instance currencyCAD :: Currency CAD where decimals _ = 2
204−instance currencyCDF :: Currency CDF where decimals _ = 2
205−instance currencyCHE :: Currency CHE where decimals _ = 2
206−instance currencyCHF :: Currency CHF where decimals _ = 2
207−instance currencyCHW :: Currency CHW where decimals _ = 2
208−instance currencyCLF :: Currency CLF where decimals _ = 4
209−instance currencyCLP :: Currency CLP where decimals _ = 0
210−instance currencyCNY :: Currency CNY where decimals _ = 2
211−instance currencyCOP :: Currency COP where decimals _ = 2
212−instance currencyCOU :: Currency COU where decimals _ = 2
213−instance currencyCRC :: Currency CRC where decimals _ = 2
214−instance currencyCUC :: Currency CUC where decimals _ = 2
215−instance currencyCUP :: Currency CUP where decimals _ = 2
216−instance currencyCVE :: Currency CVE where decimals _ = 2
217−instance currencyCZK :: Currency CZK where decimals _ = 2
218−instance currencyDJF :: Currency DJF where decimals _ = 0
219−instance currencyDKK :: Currency DKK where decimals _ = 2
220−instance currencyDOP :: Currency DOP where decimals _ = 2
221−instance currencyDZD :: Currency DZD where decimals _ = 2
222−instance currencyEGP :: Currency EGP where decimals _ = 2
223−instance currencyERN :: Currency ERN where decimals _ = 2
224−instance currencyETB :: Currency ETB where decimals _ = 2
225−instance currencyEUR :: Currency EUR where decimals _ = 2
226−instance currencyFJD :: Currency FJD where decimals _ = 2
227−instance currencyFKP :: Currency FKP where decimals _ = 2
228−instance currencyGBP :: Currency GBP where decimals _ = 2
229−instance currencyGEL :: Currency GEL where decimals _ = 2
230−instance currencyGHS :: Currency GHS where decimals _ = 2
231−instance currencyGIP :: Currency GIP where decimals _ = 2
232−instance currencyGMD :: Currency GMD where decimals _ = 2
233−instance currencyGNF :: Currency GNF where decimals _ = 0
234−instance currencyGTQ :: Currency GTQ where decimals _ = 2
235−instance currencyGYD :: Currency GYD where decimals _ = 2
236−instance currencyHKD :: Currency HKD where decimals _ = 2
237−instance currencyHNL :: Currency HNL where decimals _ = 2
238−instance currencyHRK :: Currency HRK where decimals _ = 2
239−instance currencyHTG :: Currency HTG where decimals _ = 2
240−instance currencyHUF :: Currency HUF where decimals _ = 2
241−instance currencyIDR :: Currency IDR where decimals _ = 2
242−instance currencyILS :: Currency ILS where decimals _ = 2
243−instance currencyINR :: Currency INR where decimals _ = 2
244−instance currencyIQD :: Currency IQD where decimals _ = 3
245−instance currencyIRR :: Currency IRR where decimals _ = 2
246−instance currencyISK :: Currency ISK where decimals _ = 0
247−instance currencyJMD :: Currency JMD where decimals _ = 2
248−instance currencyJOD :: Currency JOD where decimals _ = 3
249−instance currencyJPY :: Currency JPY where decimals _ = 0
250−instance currencyKES :: Currency KES where decimals _ = 2
251−instance currencyKGS :: Currency KGS where decimals _ = 2
252−instance currencyKHR :: Currency KHR where decimals _ = 2
253−instance currencyKMF :: Currency KMF where decimals _ = 0
254−instance currencyKPW :: Currency KPW where decimals _ = 2
255−instance currencyKRW :: Currency KRW where decimals _ = 0
256−instance currencyKWD :: Currency KWD where decimals _ = 3
257−instance currencyKYD :: Currency KYD where decimals _ = 2
258−instance currencyKZT :: Currency KZT where decimals _ = 2
259−instance currencyLAK :: Currency LAK where decimals _ = 2
260−instance currencyLBP :: Currency LBP where decimals _ = 2
261−instance currencyLKR :: Currency LKR where decimals _ = 2
262−instance currencyLRD :: Currency LRD where decimals _ = 2
263−instance currencyLSL :: Currency LSL where decimals _ = 2
264−instance currencyLTL :: Currency LTL where decimals _ = 2
265−instance currencyLYD :: Currency LYD where decimals _ = 3
266−instance currencyMAD :: Currency MAD where decimals _ = 2
267−instance currencyMDL :: Currency MDL where decimals _ = 2
268−instance currencyMGA :: Currency MGA where decimals _ = 2
269−instance currencyMKD :: Currency MKD where decimals _ = 2
270−instance currencyMMK :: Currency MMK where decimals _ = 2
271−instance currencyMNT :: Currency MNT where decimals _ = 2
272−instance currencyMOP :: Currency MOP where decimals _ = 2
273−instance currencyMRO :: Currency MRO where decimals _ = 2
274−instance currencyMUR :: Currency MUR where decimals _ = 2
275−instance currencyMVR :: Currency MVR where decimals _ = 2
276−instance currencyMWK :: Currency MWK where decimals _ = 2
277−instance currencyMXN :: Currency MXN where decimals _ = 2
278−instance currencyMXV :: Currency MXV where decimals _ = 2
279−instance currencyMYR :: Currency MYR where decimals _ = 2
280−instance currencyMZN :: Currency MZN where decimals _ = 2
281−instance currencyNAD :: Currency NAD where decimals _ = 2
282−instance currencyNGN :: Currency NGN where decimals _ = 2
283−instance currencyNIO :: Currency NIO where decimals _ = 2
284−instance currencyNOK :: Currency NOK where decimals _ = 2
285−instance currencyNPR :: Currency NPR where decimals _ = 2
286−instance currencyNZD :: Currency NZD where decimals _ = 2
287−instance currencyOMR :: Currency OMR where decimals _ = 3
288−instance currencyPAB :: Currency PAB where decimals _ = 2
289−instance currencyPEN :: Currency PEN where decimals _ = 2
290−instance currencyPGK :: Currency PGK where decimals _ = 2
291−instance currencyPHP :: Currency PHP where decimals _ = 2
292−instance currencyPKR :: Currency PKR where decimals _ = 2
293−instance currencyPLN :: Currency PLN where decimals _ = 2
294−instance currencyPYG :: Currency PYG where decimals _ = 0
295−instance currencyQAR :: Currency QAR where decimals _ = 2
296−instance currencyRON :: Currency RON where decimals _ = 2
297−instance currencyRSD :: Currency RSD where decimals _ = 2
298−instance currencyRUB :: Currency RUB where decimals _ = 2
299−instance currencyRWF :: Currency RWF where decimals _ = 0
300−instance currencySAR :: Currency SAR where decimals _ = 2
301−instance currencySBD :: Currency SBD where decimals _ = 2
302−instance currencySCR :: Currency SCR where decimals _ = 2
303−instance currencySDG :: Currency SDG where decimals _ = 2
304−instance currencySEK :: Currency SEK where decimals _ = 2
305−instance currencySGD :: Currency SGD where decimals _ = 2
306−instance currencySHP :: Currency SHP where decimals _ = 2
307−instance currencySLL :: Currency SLL where decimals _ = 2
308−instance currencySOS :: Currency SOS where decimals _ = 2
309−instance currencySRD :: Currency SRD where decimals _ = 2
310−instance currencySSP :: Currency SSP where decimals _ = 2
311−instance currencySTD :: Currency STD where decimals _ = 2
312−instance currencySVC :: Currency SVC where decimals _ = 2
313−instance currencySYP :: Currency SYP where decimals _ = 2
314−instance currencySZL :: Currency SZL where decimals _ = 2
315−instance currencyTHB :: Currency THB where decimals _ = 2
316−instance currencyTJS :: Currency TJS where decimals _ = 2
317−instance currencyTMT :: Currency TMT where decimals _ = 2
318−instance currencyTND :: Currency TND where decimals _ = 3
319−instance currencyTOP :: Currency TOP where decimals _ = 2
320−instance currencyTRY :: Currency TRY where decimals _ = 2
321−instance currencyTTD :: Currency TTD where decimals _ = 2
322−instance currencyTWD :: Currency TWD where decimals _ = 2
323−instance currencyTZS :: Currency TZS where decimals _ = 2
324−instance currencyUAH :: Currency UAH where decimals _ = 2
325−instance currencyUGX :: Currency UGX where decimals _ = 0
326−instance currencyUSD :: Currency USD where decimals _ = 2
327−instance currencyUSN :: Currency USN where decimals _ = 2
328−instance currencyUYI :: Currency UYI where decimals _ = 0
329−instance currencyUYU :: Currency UYU where decimals _ = 2
330−instance currencyUZS :: Currency UZS where decimals _ = 2
331−instance currencyVEF :: Currency VEF where decimals _ = 2
332−instance currencyVND :: Currency VND where decimals _ = 0
333−instance currencyVUV :: Currency VUV where decimals _ = 0
334−instance currencyWST :: Currency WST where decimals _ = 2
335−instance currencyXAF :: Currency XAF where decimals _ = 0
336−instance currencyXCD :: Currency XCD where decimals _ = 2
337−instance currencyXOF :: Currency XOF where decimals _ = 0
338−instance currencyXPF :: Currency XPF where decimals _ = 0
339−instance currencyYER :: Currency YER where decimals _ = 2
340−instance currencyZAR :: Currency ZAR where decimals _ = 2
341−instance currencyZMW :: Currency ZMW where decimals _ = 2
342−instance currencyZWL :: Currency ZWL where decimals _ = 2
178+instance currencyAED :: Currency AED where
179+ code _ = "AED"
180+ decimals _ = 2
181+
182+instance currencyAFN :: Currency AFN where
183+ code _ = "AFN"
184+ decimals _ = 2
185+
186+instance currencyALL :: Currency ALL where
187+ code _ = "ALL"
188+ decimals _ = 2
189+
190+instance currencyAMD :: Currency AMD where
191+ code _ = "AMD"
192+ decimals _ = 2
193+
194+instance currencyANG :: Currency ANG where
195+ code _ = "ANG"
196+ decimals _ = 2
197+
198+instance currencyAOA :: Currency AOA where
199+ code _ = "AOA"
200+ decimals _ = 2
201+
202+instance currencyARS :: Currency ARS where
203+ code _ = "ARS"
204+ decimals _ = 2
205+
206+instance currencyAUD :: Currency AUD where
207+ code _ = "AUD"
208+ decimals _ = 2
209+
210+instance currencyAWG :: Currency AWG where
211+ code _ = "AWG"
212+ decimals _ = 2
213+
214+instance currencyAZN :: Currency AZN where
215+ code _ = "AZN"
216+ decimals _ = 2
217+
218+instance currencyBAM :: Currency BAM where
219+ code _ = "BAM"
220+ decimals _ = 2
221+
222+instance currencyBBD :: Currency BBD where
223+ code _ = "BBD"
224+ decimals _ = 2
225+
226+instance currencyBDT :: Currency BDT where
227+ code _ = "BDT"
228+ decimals _ = 2
229+
230+instance currencyBGN :: Currency BGN where
231+ code _ = "BGN"
232+ decimals _ = 2
233+
234+instance currencyBHD :: Currency BHD where
235+ code _ = "BHD"
236+ decimals _ = 3
237+
238+instance currencyBIF :: Currency BIF where
239+ code _ = "BIF"
240+ decimals _ = 0
241+
242+instance currencyBMD :: Currency BMD where
243+ code _ = "BMD"
244+ decimals _ = 2
245+
246+instance currencyBND :: Currency BND where
247+ code _ = "BND"
248+ decimals _ = 2
249+
250+instance currencyBOB :: Currency BOB where
251+ code _ = "BOB"
252+ decimals _ = 2
253+
254+instance currencyBOV :: Currency BOV where
255+ code _ = "BOV"
256+ decimals _ = 2
257+
258+instance currencyBRL :: Currency BRL where
259+ code _ = "BRL"
260+ decimals _ = 2
261+
262+instance currencyBSD :: Currency BSD where
263+ code _ = "BSD"
264+ decimals _ = 2
265+
266+instance currencyBTN :: Currency BTN where
267+ code _ = "BTN"
268+ decimals _ = 2
269+
270+instance currencyBWP :: Currency BWP where
271+ code _ = "BWP"
272+ decimals _ = 2
273+
274+instance currencyBYR :: Currency BYR where
275+ code _ = "BYR"
276+ decimals _ = 0
277+
278+instance currencyBZD :: Currency BZD where
279+ code _ = "BZD"
280+ decimals _ = 2
281+
282+instance currencyCAD :: Currency CAD where
283+ code _ = "CAD"
284+ decimals _ = 2
285+
286+instance currencyCDF :: Currency CDF where
287+ code _ = "CDF"
288+ decimals _ = 2
289+
290+instance currencyCHE :: Currency CHE where
291+ code _ = "CHE"
292+ decimals _ = 2
293+
294+instance currencyCHF :: Currency CHF where
295+ code _ = "CHF"
296+ decimals _ = 2
297+
298+instance currencyCHW :: Currency CHW where
299+ code _ = "CHW"
300+ decimals _ = 2
301+
302+instance currencyCLF :: Currency CLF where
303+ code _ = "CLF"
304+ decimals _ = 4
305+
306+instance currencyCLP :: Currency CLP where
307+ code _ = "CLP"
308+ decimals _ = 0
309+
310+instance currencyCNY :: Currency CNY where
311+ code _ = "CNY"
312+ decimals _ = 2
313+
314+instance currencyCOP :: Currency COP where
315+ code _ = "COP"
316+ decimals _ = 2
317+
318+instance currencyCOU :: Currency COU where
319+ code _ = "COU"
320+ decimals _ = 2
321+
322+instance currencyCRC :: Currency CRC where
323+ code _ = "CRC"
324+ decimals _ = 2
325+
326+instance currencyCUC :: Currency CUC where
327+ code _ = "CUC"
328+ decimals _ = 2
329+
330+instance currencyCUP :: Currency CUP where
331+ code _ = "CUP"
332+ decimals _ = 2
333+
334+instance currencyCVE :: Currency CVE where
335+ code _ = "CVE"
336+ decimals _ = 2
337+
338+instance currencyCZK :: Currency CZK where
339+ code _ = "CZK"
340+ decimals _ = 2
341+
342+instance currencyDJF :: Currency DJF where
343+ code _ = "DJF"
344+ decimals _ = 0
345+
346+instance currencyDKK :: Currency DKK where
347+ code _ = "DKK"
348+ decimals _ = 2
349+
350+instance currencyDOP :: Currency DOP where
351+ code _ = "DOP"
352+ decimals _ = 2
353+
354+instance currencyDZD :: Currency DZD where
355+ code _ = "DZD"
356+ decimals _ = 2
357+
358+instance currencyEGP :: Currency EGP where
359+ code _ = "EGP"
360+ decimals _ = 2
361+
362+instance currencyERN :: Currency ERN where
363+ code _ = "ERN"
364+ decimals _ = 2
365+
366+instance currencyETB :: Currency ETB where
367+ code _ = "ETB"
368+ decimals _ = 2
369+
370+instance currencyEUR :: Currency EUR where
371+ code _ = "EUR"
372+ decimals _ = 2
373+
374+instance currencyFJD :: Currency FJD where
375+ code _ = "FJD"
376+ decimals _ = 2
377+
378+instance currencyFKP :: Currency FKP where
379+ code _ = "FKP"
380+ decimals _ = 2
381+
382+instance currencyGBP :: Currency GBP where
383+ code _ = "GBP"
384+ decimals _ = 2
385+
386+instance currencyGEL :: Currency GEL where
387+ code _ = "GEL"
388+ decimals _ = 2
389+
390+instance currencyGHS :: Currency GHS where
391+ code _ = "GHS"
392+ decimals _ = 2
393+
394+instance currencyGIP :: Currency GIP where
395+ code _ = "GIP"
396+ decimals _ = 2
397+
398+instance currencyGMD :: Currency GMD where
399+ code _ = "GMD"
400+ decimals _ = 2
401+
402+instance currencyGNF :: Currency GNF where
403+ code _ = "GNF"
404+ decimals _ = 0
405+
406+instance currencyGTQ :: Currency GTQ where
407+ code _ = "GTQ"
408+ decimals _ = 2
409+
410+instance currencyGYD :: Currency GYD where
411+ code _ = "GYD"
412+ decimals _ = 2
413+
414+instance currencyHKD :: Currency HKD where
415+ code _ = "HKD"
416+ decimals _ = 2
417+
418+instance currencyHNL :: Currency HNL where
419+ code _ = "HNL"
420+ decimals _ = 2
421+
422+instance currencyHRK :: Currency HRK where
423+ code _ = "HRK"
424+ decimals _ = 2
425+
426+instance currencyHTG :: Currency HTG where
427+ code _ = "HTG"
428+ decimals _ = 2
429+
430+instance currencyHUF :: Currency HUF where
431+ code _ = "HUF"
432+ decimals _ = 2
433+
434+instance currencyIDR :: Currency IDR where
435+ code _ = "IDR"
436+ decimals _ = 2
437+
438+instance currencyILS :: Currency ILS where
439+ code _ = "ILS"
440+ decimals _ = 2
441+
442+instance currencyINR :: Currency INR where
443+ code _ = "INR"
444+ decimals _ = 2
445+
446+instance currencyIQD :: Currency IQD where
447+ code _ = "IQD"
448+ decimals _ = 3
449+
450+instance currencyIRR :: Currency IRR where
451+ code _ = "IRR"
452+ decimals _ = 2
453+
454+instance currencyISK :: Currency ISK where
455+ code _ = "ISK"
456+ decimals _ = 0
457+
458+instance currencyJMD :: Currency JMD where
459+ code _ = "JMD"
460+ decimals _ = 2
461+
462+instance currencyJOD :: Currency JOD where
463+ code _ = "JOD"
464+ decimals _ = 3
465+
466+instance currencyJPY :: Currency JPY where
467+ code _ = "JPY"
468+ decimals _ = 0
469+
470+instance currencyKES :: Currency KES where
471+ code _ = "KES"
472+ decimals _ = 2
473+
474+instance currencyKGS :: Currency KGS where
475+ code _ = "KGS"
476+ decimals _ = 2
477+
478+instance currencyKHR :: Currency KHR where
479+ code _ = "KHR"
480+ decimals _ = 2
481+
482+instance currencyKMF :: Currency KMF where
483+ code _ = "KMF"
484+ decimals _ = 0
485+
486+instance currencyKPW :: Currency KPW where
487+ code _ = "KPW"
488+ decimals _ = 2
489+
490+instance currencyKRW :: Currency KRW where
491+ code _ = "KRW"
492+ decimals _ = 0
493+
494+instance currencyKWD :: Currency KWD where
495+ code _ = "KWD"
496+ decimals _ = 3
497+
498+instance currencyKYD :: Currency KYD where
499+ code _ = "KYD"
500+ decimals _ = 2
501+
502+instance currencyKZT :: Currency KZT where
503+ code _ = "KZT"
504+ decimals _ = 2
505+
506+instance currencyLAK :: Currency LAK where
507+ code _ = "LAK"
508+ decimals _ = 2
509+
510+instance currencyLBP :: Currency LBP where
511+ code _ = "LBP"
512+ decimals _ = 2
513+
514+instance currencyLKR :: Currency LKR where
515+ code _ = "LKR"
516+ decimals _ = 2
517+
518+instance currencyLRD :: Currency LRD where
519+ code _ = "LRD"
520+ decimals _ = 2
521+
522+instance currencyLSL :: Currency LSL where
523+ code _ = "LSL"
524+ decimals _ = 2
525+
526+instance currencyLTL :: Currency LTL where
527+ code _ = "LTL"
528+ decimals _ = 2
529+
530+instance currencyLYD :: Currency LYD where
531+ code _ = "LYD"
532+ decimals _ = 3
533+
534+instance currencyMAD :: Currency MAD where
535+ code _ = "MAD"
536+ decimals _ = 2
537+
538+instance currencyMDL :: Currency MDL where
539+ code _ = "MDL"
540+ decimals _ = 2
541+
542+instance currencyMGA :: Currency MGA where
543+ code _ = "MGA"
544+ decimals _ = 2
545+
546+instance currencyMKD :: Currency MKD where
547+ code _ = "MKD"
548+ decimals _ = 2
549+
550+instance currencyMMK :: Currency MMK where
551+ code _ = "MMK"
552+ decimals _ = 2
553+
554+instance currencyMNT :: Currency MNT where
555+ code _ = "MNT"
556+ decimals _ = 2
557+
558+instance currencyMOP :: Currency MOP where
559+ code _ = "MOP"
560+ decimals _ = 2
561+
562+instance currencyMRO :: Currency MRO where
563+ code _ = "MRO"
564+ decimals _ = 2
565+
566+instance currencyMUR :: Currency MUR where
567+ code _ = "MUR"
568+ decimals _ = 2
569+
570+instance currencyMVR :: Currency MVR where
571+ code _ = "MVR"
572+ decimals _ = 2
573+
574+instance currencyMWK :: Currency MWK where
575+ code _ = "MWK"
576+ decimals _ = 2
577+
578+instance currencyMXN :: Currency MXN where
579+ code _ = "MXN"
580+ decimals _ = 2
581+
582+instance currencyMXV :: Currency MXV where
583+ code _ = "MXV"
584+ decimals _ = 2
585+
586+instance currencyMYR :: Currency MYR where
587+ code _ = "MYR"
588+ decimals _ = 2
589+
590+instance currencyMZN :: Currency MZN where
591+ code _ = "MZN"
592+ decimals _ = 2
593+
594+instance currencyNAD :: Currency NAD where
595+ code _ = "NAD"
596+ decimals _ = 2
597+
598+instance currencyNGN :: Currency NGN where
599+ code _ = "NGN"
600+ decimals _ = 2
601+
602+instance currencyNIO :: Currency NIO where
603+ code _ = "NIO"
604+ decimals _ = 2
605+
606+instance currencyNOK :: Currency NOK where
607+ code _ = "NOK"
608+ decimals _ = 2
609+
610+instance currencyNPR :: Currency NPR where
611+ code _ = "NPR"
612+ decimals _ = 2
613+
614+instance currencyNZD :: Currency NZD where
615+ code _ = "NZD"
616+ decimals _ = 2
617+
618+instance currencyOMR :: Currency OMR where
619+ code _ = "OMR"
620+ decimals _ = 3
621+
622+instance currencyPAB :: Currency PAB where
623+ code _ = "PAB"
624+ decimals _ = 2
625+
626+instance currencyPEN :: Currency PEN where
627+ code _ = "PEN"
628+ decimals _ = 2
629+
630+instance currencyPGK :: Currency PGK where
631+ code _ = "PGK"
632+ decimals _ = 2
633+
634+instance currencyPHP :: Currency PHP where
635+ code _ = "PHP"
636+ decimals _ = 2
637+
638+instance currencyPKR :: Currency PKR where
639+ code _ = "PKR"
640+ decimals _ = 2
641+
642+instance currencyPLN :: Currency PLN where
643+ code _ = "PLN"
644+ decimals _ = 2
645+
646+instance currencyPYG :: Currency PYG where
647+ code _ = "PYG"
648+ decimals _ = 0
649+
650+instance currencyQAR :: Currency QAR where
651+ code _ = "QAR"
652+ decimals _ = 2
653+
654+instance currencyRON :: Currency RON where
655+ code _ = "RON"
656+ decimals _ = 2
657+
658+instance currencyRSD :: Currency RSD where
659+ code _ = "RSD"
660+ decimals _ = 2
661+
662+instance currencyRUB :: Currency RUB where
663+ code _ = "RUB"
664+ decimals _ = 2
665+
666+instance currencyRWF :: Currency RWF where
667+ code _ = "RWF"
668+ decimals _ = 0
669+
670+instance currencySAR :: Currency SAR where
671+ code _ = "SAR"
672+ decimals _ = 2
673+
674+instance currencySBD :: Currency SBD where
675+ code _ = "SBD"
676+ decimals _ = 2
677+
678+instance currencySCR :: Currency SCR where
679+ code _ = "SCR"
680+ decimals _ = 2
681+
682+instance currencySDG :: Currency SDG where
683+ code _ = "SDG"
684+ decimals _ = 2
685+
686+instance currencySEK :: Currency SEK where
687+ code _ = "SEK"
688+ decimals _ = 2
689+
690+instance currencySGD :: Currency SGD where
691+ code _ = "SGD"
692+ decimals _ = 2
693+
694+instance currencySHP :: Currency SHP where
695+ code _ = "SHP"
696+ decimals _ = 2
697+
698+instance currencySLL :: Currency SLL where
699+ code _ = "SLL"
700+ decimals _ = 2
701+
702+instance currencySOS :: Currency SOS where
703+ code _ = "SOS"
704+ decimals _ = 2
705+
706+instance currencySRD :: Currency SRD where
707+ code _ = "SRD"
708+ decimals _ = 2
709+
710+instance currencySSP :: Currency SSP where
711+ code _ = "SSP"
712+ decimals _ = 2
713+
714+instance currencySTD :: Currency STD where
715+ code _ = "STD"
716+ decimals _ = 2
717+
718+instance currencySVC :: Currency SVC where
719+ code _ = "SVC"
720+ decimals _ = 2
721+
722+instance currencySYP :: Currency SYP where
723+ code _ = "SYP"
724+ decimals _ = 2
725+
726+instance currencySZL :: Currency SZL where
727+ code _ = "SZL"
728+ decimals _ = 2
729+
730+instance currencyTHB :: Currency THB where
731+ code _ = "THB"
732+ decimals _ = 2
733+
734+instance currencyTJS :: Currency TJS where
735+ code _ = "TJS"
736+ decimals _ = 2
737+
738+instance currencyTMT :: Currency TMT where
739+ code _ = "TMT"
740+ decimals _ = 2
741+
742+instance currencyTND :: Currency TND where
743+ code _ = "TND"
744+ decimals _ = 3
745+
746+instance currencyTOP :: Currency TOP where
747+ code _ = "TOP"
748+ decimals _ = 2
749+
750+instance currencyTRY :: Currency TRY where
751+ code _ = "TRY"
752+ decimals _ = 2
753+
754+instance currencyTTD :: Currency TTD where
755+ code _ = "TTD"
756+ decimals _ = 2
757+
758+instance currencyTWD :: Currency TWD where
759+ code _ = "TWD"
760+ decimals _ = 2
761+
762+instance currencyTZS :: Currency TZS where
763+ code _ = "TZS"
764+ decimals _ = 2
765+
766+instance currencyUAH :: Currency UAH where
767+ code _ = "UAH"
768+ decimals _ = 2
769+
770+instance currencyUGX :: Currency UGX where
771+ code _ = "UGX"
772+ decimals _ = 0
773+
774+instance currencyUSD :: Currency USD where
775+ code _ = "USD"
776+ decimals _ = 2
777+
778+instance currencyUSN :: Currency USN where
779+ code _ = "USN"
780+ decimals _ = 2
781+
782+instance currencyUYI :: Currency UYI where
783+ code _ = "UYI"
784+ decimals _ = 0
785+
786+instance currencyUYU :: Currency UYU where
787+ code _ = "UYU"
788+ decimals _ = 2
789+
790+instance currencyUZS :: Currency UZS where
791+ code _ = "UZS"
792+ decimals _ = 2
793+
794+instance currencyVEF :: Currency VEF where
795+ code _ = "VEF"
796+ decimals _ = 2
797+
798+instance currencyVND :: Currency VND where
799+ code _ = "VND"
800+ decimals _ = 0
801+
802+instance currencyVUV :: Currency VUV where
803+ code _ = "VUV"
804+ decimals _ = 0
805+
806+instance currencyWST :: Currency WST where
807+ code _ = "WST"
808+ decimals _ = 2
809+
810+instance currencyXAF :: Currency XAF where
811+ code _ = "XAF"
812+ decimals _ = 0
813+
814+instance currencyXCD :: Currency XCD where
815+ code _ = "XCD"
816+ decimals _ = 2
817+
818+instance currencyXOF :: Currency XOF where
819+ code _ = "XOF"
820+ decimals _ = 0
821+
822+instance currencyXPF :: Currency XPF where
823+ code _ = "XPF"
824+ decimals _ = 0
825+
826+instance currencyYER :: Currency YER where
827+ code _ = "YER"
828+ decimals _ = 2
829+
830+instance currencyZAR :: Currency ZAR where
831+ code _ = "ZAR"
832+ decimals _ = 2
833+
834+instance currencyZMW :: Currency ZMW where
835+ code _ = "ZMW"
836+ decimals _ = 2
837+
838+instance currencyZWL :: Currency ZWL where
839+ code _ = "ZWL"
840+ decimals _ = 2
src/Data/Finance/Money.jsdeleted−7
@@ -1,7 +0,0 @@
1−'use strict';
2−
3−exports['showDiscrete\''] = function(n) {
4− return function(d) {
5− return (n / Math.pow(10, d)).toFixed(d);
6− };
7−};
src/Data/Finance/Money.purs+111−13
@@ -1,28 +1,126 @@
11module Data.Finance.Money
22 ( Discrete(..)
3+ , formatDiscrete
34 , showDiscrete
5+
6+ , Dense(..)
7+ , formatDense
8+
9+ , Rounding(..)
10+ , fromDiscrete
11+ , fromDense
412 ) where
513
6−import Data.Finance.Currency (kind Currency, class Currency, CProxy(..), decimals)
7−import Data.Generic (class Generic)
8−import Data.UnittedSemiring (class UnittedSemiring)
14+import Data.Finance.Currency (kind Currency, class Currency, CProxy(..))
15+import Data.Finance.Currency as Currency
16+import Data.Finance.Money.Format (Format, FormatF(..), absolute, ifNegative, literal)
17+import Data.Foldable (foldMap)
18+import Data.Generic (class Generic, gShow)
19+import Data.Int as Int
20+import Data.Module (class LeftModule, class RightModule)
21+import Data.Newtype (class Newtype)
22+import Data.Ord (abs)
23+import Data.Rational (Rational, (%))
24+import Data.Rational as Rational
25+import Global.Unsafe (unsafeToFixed)
26+import Math (pow)
927import Prelude
1028
29+--------------------------------------------------------------------------------
30+
1131-- | An amount of money in the smallest discrete unit of a particular currency.
12−-- | For example, `256 :: Discrete GBP` would represent £2.56, whereas
13−-- | `256 :: Discrete JPY` would represent ¥256.
32+-- | For example, `wrap 256 :: Discrete GBP` would represent £2.56, whereas
33+-- | `wrap 256 :: Discrete JPY` would represent ¥256. If you want to work with
34+-- | higher granularity, you can define your own currency type.
1435newtype Discrete (c :: Currency) = Discrete Int
1536derive newtype instance eqDiscrete :: Eq (Discrete c)
1637derive newtype instance ordDiscrete :: Ord (Discrete c)
1738derive instance genericDiscrete :: Generic (Discrete c)
39+derive instance newtypeDiscrete :: Newtype (Discrete c) _
40+instance showDiscreteInstance :: Show (Discrete c) where show = gShow
41+
42+instance leftModuleDiscrete :: LeftModule (Discrete c) Int where
43+ mzeroL = Discrete 0
44+ maddL (Discrete a) (Discrete b) = Discrete (a + b)
45+ msubL (Discrete a) (Discrete b) = Discrete (a - b)
46+ mmulL a (Discrete b) = Discrete (a * b)
47+
48+instance rightModuleDiscrete :: RightModule (Discrete c) Int where
49+ mzeroR = Discrete 0
50+ maddR (Discrete a) (Discrete b) = Discrete (a + b)
51+ msubR (Discrete a) (Discrete b) = Discrete (a - b)
52+ mmulR (Discrete a) b = Discrete (a * b)
53+
54+-- | Apply a format to a discrete amount.
55+formatDiscrete :: ∀ c. Currency c => Format -> Discrete c -> String
56+formatDiscrete f (Discrete n) = foldMap go f
57+ where
58+ go (IfNegative s) = if n < 0 then foldMap go s else ""
59+ go (Literal s) = s
60+ go CurrencyCode = Currency.code (CProxy :: CProxy c)
61+ go Absolute =
62+ unsafeToFixed d (n' / pow 10.0 (Int.toNumber d))
63+ where
64+ n' = abs $ Int.toNumber n
65+ d = Currency.decimals (CProxy :: CProxy c)
66+
67+-- | DEPRECATED: use `formatDiscrete` instead.
68+-- |
69+-- | Show the discrete value with the correct number of decimals. Will not
70+-- | prepend the currency sign. Negative amounts are prefixed with a
71+-- | hyphen-minus.
72+-- |
73+-- | Examples:
74+-- |
75+-- | - `showDiscrete (Discrete 256 :: Discrete GBP) == "2.56"`
76+-- | - `showDiscrete (Discrete (-256) :: Discrete GBP) == "-2.56"`
77+showDiscrete :: ∀ c. Currency c => Discrete c -> String
78+showDiscrete = formatDiscrete $ ifNegative (literal "-") <> absolute
79+
80+--------------------------------------------------------------------------------
81+
82+-- | Amounts of money with exact arithmetic.
83+newtype Dense (c :: Currency) = Dense Rational
84+derive newtype instance eqDense :: Eq (Dense c)
85+derive newtype instance ordDense :: Ord (Dense c)
86+derive instance newtypeDense :: Newtype (Dense c) _
87+instance showDense :: Show (Dense c) where
88+ show (Dense r) = "(Dense " <> show r <> ")"
89+
90+instance leftModuleDense :: LeftModule (Dense c) Rational where
91+ mzeroL = Dense zero
92+ maddL (Dense a) (Dense b) = Dense (a + b)
93+ msubL (Dense a) (Dense b) = Dense (a - b)
94+ mmulL a (Dense b) = Dense (a * b)
95+
96+instance rightModuleDense :: RightModule (Dense c) Rational where
97+ mzeroR = Dense zero
98+ maddR (Dense a) (Dense b) = Dense (a + b)
99+ msubR (Dense a) (Dense b) = Dense (a - b)
100+ mmulR (Dense a) b = Dense (a * b)
101+
102+formatDense :: ∀ c. Currency c => Rounding -> Format -> Dense c -> String
103+formatDense r f d = formatDiscrete f $ fromDense r d
104+
105+--------------------------------------------------------------------------------
18106
19−instance unittedSemiringDiscrete :: UnittedSemiring (Discrete c) Int where
20− uzero = Discrete 0
21− uone = 1
22− uadd (Discrete a) (Discrete b) = Discrete (a + b)
23− umul (Discrete a) b = Discrete (a * b)
107+-- | Rounding policy.
108+data Rounding = Up | Down | ToZero | FromZero | Nearest
109+derive instance eqRounding :: Eq Rounding
110+derive instance ordRounding :: Ord Rounding
24111
25−showDiscrete :: ∀ c. (Currency c) => Discrete c -> String
26−showDiscrete (Discrete n) = showDiscrete' n (decimals (CProxy :: CProxy c))
112+-- | Convert a discrete amount to a dense amount.
113+fromDiscrete :: ∀ c. Currency c => Discrete c -> Dense c
114+fromDiscrete (Discrete n) = Dense $ n % Int.pow 10 d
115+ where d = Currency.decimals (CProxy :: CProxy c)
27116
28−foreign import showDiscrete' :: Int -> Int -> String
117+-- | Convert a dense amount to a discrete amount, rounding accordingly.
118+fromDense :: ∀ c. Currency c => Rounding -> Dense c -> Discrete c
119+fromDense r (Dense n) = Discrete case r of
120+ Up -> Int.ceil n'
121+ Down -> Int.floor n'
122+ ToZero -> (if n < zero then Int.ceil else Int.floor) n'
123+ FromZero -> (if n > zero then Int.ceil else Int.floor) n'
124+ Nearest -> Int.round n'
125+ where n' = Rational.toNumber $ n * (Int.pow 10 d % 1)
126+ d = Currency.decimals (CProxy :: CProxy c)
src/Data/Finance/Money/Format.pursnew file+107
@@ -0,0 +1,107 @@
1+module Data.Finance.Money.Format
2+ ( Format
3+ , FormatF(..)
4+ , ifNegative
5+ , literal
6+ , currencyCode
7+ , absolute
8+
9+ , sbind
10+
11+ , numeric
12+ , numericC
13+ , accountant
14+ , accountantC
15+ ) where
16+
17+import Data.List (List)
18+import Prelude
19+
20+--------------------------------------------------------------------------------
21+
22+-- | Format specification for amounts.
23+type Format = List FormatF
24+
25+-- | Part of a format specification for amounts.
26+data FormatF
27+ = IfNegative Format
28+ | Literal String
29+ | CurrencyCode
30+ | Absolute
31+
32+-- | Perform a format only if the amount is negative.
33+ifNegative :: Format -> Format
34+ifNegative = pure <<< IfNegative
35+
36+-- | Insert a literal string.
37+literal :: String -> Format
38+literal = pure <<< Literal
39+
40+-- | Insert the currency code.
41+currencyCode :: Format
42+currencyCode = pure CurrencyCode
43+
44+-- | Insert the absolute value of the amount in decimal.
45+absolute :: Format
46+absolute = pure Absolute
47+
48+--------------------------------------------------------------------------------
49+
50+-- | Utility function so you can define formats with do notation. For example:
51+-- |
52+-- | ```purescript
53+-- | myFormat = do
54+-- | ifNegative $ literal "("
55+-- | currencyCode
56+-- | literal space
57+-- | amount
58+-- | ifNegative $ literal ")"
59+-- | where discard = sbind
60+-- | ```
61+sbind :: ∀ a. Semigroup a => a -> (Unit -> a) -> a
62+sbind = flip $ flip append <<< (_ $ unit)
63+
64+--------------------------------------------------------------------------------
65+
66+-- | Numeric format, with a minus sign for negative amounts, and no currency
67+-- | indicator.
68+numeric :: Format
69+numeric = ifNegative (literal minus) <> absolute
70+
71+-- | Numeric format, with a minus sign for negative amounts, and a currency
72+-- | code.
73+numericC :: Format
74+numericC = do
75+ ifNegative $ literal minus
76+ currencyCode
77+ literal space
78+ absolute
79+ where discard = sbind
80+
81+-- | Accountant format, with negative amounts parenthesized, and no currency
82+-- | indicator.
83+accountant :: Format
84+accountant = do
85+ ifNegative $ literal "("
86+ absolute
87+ ifNegative $ literal ")"
88+ where discard = sbind
89+
90+-- | Accountant format, with negative amounts parenthesized, and a currency
91+-- | code.
92+accountantC :: Format
93+accountantC = do
94+ ifNegative $ literal "("
95+ currencyCode
96+ literal space
97+ absolute
98+ ifNegative $ literal ")"
99+ where discard = sbind
100+
101+--------------------------------------------------------------------------------
102+
103+minus :: String
104+minus = "\x2212"
105+
106+space :: String
107+space = " "
test/Data/Finance/Money.pursnew file+69
@@ -0,0 +1,69 @@
1+module Test.Data.Finance.Money
2+ ( main
3+ ) where
4+
5+import Data.Finance.Currency (GBP, JPY)
6+import Data.Finance.Money (Dense(..), Discrete(..), Rounding(..), fromDense, fromDiscrete, formatDense, formatDiscrete)
7+import Data.Finance.Money.Format (numeric)
8+import Data.Rational ((%))
9+import Prelude
10+import Test.Unit (suite, test)
11+import Test.Unit.Assert as Assert
12+
13+type IGBP = Discrete GBP
14+type IJPY = Discrete JPY
15+
16+type EGBP = Dense GBP
17+type EJPY = Dense JPY
18+
19+main = suite "Date.Finance.Money" do
20+ test "formatDiscrete" do
21+ let gbp s n = Assert.equal s $ formatDiscrete numeric (Discrete n :: IGBP)
22+ let jpy s n = Assert.equal s $ formatDiscrete numeric (Discrete n :: IJPY)
23+
24+ gbp "0.00" 0
25+ gbp "2.56" 256
26+ gbp "−2.56" (-256)
27+ gbp "21474836.47" top
28+ gbp "−21474836.48" bottom
29+
30+ jpy "0" 0
31+ jpy "256" 256
32+ jpy "−256" (-256)
33+ jpy "2147483647" top
34+ jpy "−2147483648" bottom
35+
36+ test "formatDense" do
37+ let gbp s n = Assert.equal s $ formatDense Nearest numeric (Dense n :: EGBP)
38+ let jpy s n = Assert.equal s $ formatDense Nearest numeric (Dense n :: EJPY)
39+
40+ gbp "0.00" (0 % 1)
41+ gbp "2.56" (256 % 100)
42+ gbp "−2.56" ((-256) % 100)
43+
44+ jpy "0" (0 % 1)
45+ jpy "3" (256 % 100)
46+ jpy "−3" ((-256) % 100)
47+
48+ test "fromDiscrete" do
49+ Assert.equal (Dense $ 1 % 2) (fromDiscrete $ Discrete 50 :: IGBP)
50+ Assert.equal (Dense $ 50 % 1) (fromDiscrete $ Discrete 50 :: IJPY)
51+
52+ test "fromDense" do
53+ Assert.equal (Discrete 34 :: IGBP) (fromDense Up <<< Dense $ 1 % 3)
54+ Assert.equal (Discrete 33 :: IGBP) (fromDense Down <<< Dense $ 1 % 3)
55+ Assert.equal (Discrete 33 :: IGBP) (fromDense ToZero <<< Dense $ 1 % 3)
56+ Assert.equal (Discrete 34 :: IGBP) (fromDense FromZero <<< Dense $ 1 % 3)
57+ Assert.equal (Discrete (-33) :: IGBP) (fromDense ToZero <<< Dense $ (-1) % 3)
58+ Assert.equal (Discrete (-34) :: IGBP) (fromDense FromZero <<< Dense $ (-1) % 3)
59+ Assert.equal (Discrete 33 :: IGBP) (fromDense Nearest <<< Dense $ 1 % 3)
60+ Assert.equal (Discrete (-33) :: IGBP) (fromDense Nearest <<< Dense $ (-1) % 3)
61+
62+ Assert.equal (Discrete 1 :: IJPY) (fromDense Up <<< Dense $ 1 % 3)
63+ Assert.equal (Discrete 0 :: IJPY) (fromDense Down <<< Dense $ 1 % 3)
64+ Assert.equal (Discrete 0 :: IJPY) (fromDense ToZero <<< Dense $ 1 % 3)
65+ Assert.equal (Discrete 1 :: IJPY) (fromDense FromZero <<< Dense $ 1 % 3)
66+ Assert.equal (Discrete (-0) :: IJPY) (fromDense ToZero <<< Dense $ (-1) % 3)
67+ Assert.equal (Discrete (-1) :: IJPY) (fromDense FromZero <<< Dense $ (-1) % 3)
68+ Assert.equal (Discrete 0 :: IJPY) (fromDense Nearest <<< Dense $ 1 % 3)
69+ Assert.equal (Discrete (-0) :: IJPY) (fromDense Nearest <<< Dense $ (-1) % 3)
test/Main.pursnew file+9
@@ -0,0 +1,9 @@
1+module Test.Main
2+ ( main
3+ ) where
4+
5+import Test.Data.Finance.Money as Data.Finance.Money
6+import Test.Unit.Main (runTest)
7+
8+main = runTest do
9+ Data.Finance.Money.main