Skip to content

Commit b06c9d5

Browse files
authored
Merge pull request #2 from purescript/jam/add-to-int
Add `toInt`
2 parents c9e053d + 8fa4ea3 commit b06c9d5

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"dependencies": {
2323
"purescript-prelude": "^6.0.1",
2424
"purescript-functions": "^6.0.0",
25+
"purescript-integers": "^6.0.0",
2526
"purescript-maybe": "^6.0.0",
2627
"purescript-either": "^6.1.0",
2728
"purescript-tuples": "^7.0.0",

src/JSON.purs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module JSON
1313
, toNull
1414
, toBoolean
1515
, toNumber
16+
, toInt
1617
, toString
1718
, toArray
1819
, toJArray
@@ -27,6 +28,7 @@ import Prelude
2728
import Data.Either (Either(..))
2829
import Data.Function.Uncurried (runFn2, runFn3, runFn7)
2930
import Data.Maybe (Maybe(..))
31+
import Data.Int as Int
3032
import JSON.Internal (JArray, JObject, JSON) as Exports
3133
import JSON.Internal (JArray, JObject, JSON)
3234
import JSON.Internal as Internal
@@ -60,7 +62,7 @@ fromNumberWithDefault fallback n = runFn2 Internal._fromNumberWithDefault fallba
6062

6163
-- | Converts an `Int` into `JSON`.
6264
-- |
63-
-- | There is no corresponding `toInt` as JSON doesn't have a concept of integers - this is provided
65+
-- | Note: JSON doesn't have a concept of integers. This is provided
6466
-- | as a convenience to avoid having to convert `Int` to `Number` before creating a `JSON` value.
6567
foreign import fromInt :: Int -> JSON
6668

@@ -119,6 +121,12 @@ toBoolean json = runFn7 Internal._case fail Just fail fail fail fail json
119121
toNumber :: JSON -> Maybe Number
120122
toNumber json = runFn7 Internal._case fail fail Just fail fail fail json
121123

124+
-- | Converts a `JSON` `Number` into an `Int`.
125+
-- |
126+
-- | This is provided for convenience only.
127+
toInt :: JSON -> Maybe Int
128+
toInt = toNumber >=> Int.fromNumber
129+
122130
-- | Converts a `JSON` value to `String` if the `JSON` is a string.
123131
toString :: JSON -> Maybe String
124132
toString json = runFn7 Internal._case fail fail fail Just fail fail json

0 commit comments

Comments
 (0)