-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathNumber.mint
111 lines (90 loc) · 2.31 KB
/
Number.mint
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* This module provides functions for working with the `Number` type. */
module Number {
/*
Formats the number using the prefix and separating the digits by 3 with a
comma.
Number.format(1034150, "$ ") == "$ 1,034,150"
*/
fun format (number : Number, prefix : String) : String {
let string =
Number.toFixed(number, 2)
let parts =
String.split(string, ".")
let digits =
parts[0]
|> Maybe.withDefault("")
|> String.chopStart("-")
|> String.split("")
|> Array.groupsOfFromEnd(3)
|> Array.map((items : Array(String)) { String.join(items, "") })
|> String.join(",")
let decimals =
parts[1]
|> Maybe.withDefault("")
|> String.chopEnd("0")
if String.isEmpty(decimals) {
prefix + digits
} else {
prefix + digits + "." + decimals
}
}
/*
Tries to parse the string input into a number.
Number.fromString("012") == Maybe.Just(12)
Number.fromString("asd") == Maybe.Nothing
*/
fun fromString (input : String) : Maybe(Number) {
`
(() => {
if (#{input}.trim() === '') {
return #{Maybe.Nothing}
}
const value = Number(#{input})
if (Number.isNaN(value)) {
return #{Maybe.Nothing}
}
return #{Maybe.Just(`value`)}
})()
`
}
/*
Returns `true` if number is even.
Number.isEven(2) == false
Number.isEven(1) == true
*/
fun isEven (number : Number) : Bool {
`Math.abs(#{number} % 2) === 0`
}
/*
Returns `true` if number is `NaN`.
Number.isNaN(`NaN`) == true
Number.isNaN(0) == false
*/
fun isNaN (number : Number) : Bool {
`isNaN(#{number})`
}
/*
Returns `true` if number is odd.
Number.isOdd(1) == false
Number.isOdd(2) == true
*/
fun isOdd (number : Number) : Bool {
`#{number} % 2 === 1`
}
/*
Formats a number using fixed-point notation.
The last arguments specifies the number of digits to appear after the
decimal point, it can be between 0 and 20.
Number.toFixed(0.1234567, 2) == "0.12"
*/
fun toFixed (number : Number, decimalPlaces : Number) : String {
`#{number}.toFixed(#{decimalPlaces})`
}
/*
Returns the string representation of the number.
Number.toString(123) == 123
*/
fun toString (number : Number) : String {
`#{number}.toString()`
}
}