Skip to content

Commit

Permalink
Merge pull request #2 from purescript/jam/add-to-int
Browse files Browse the repository at this point in the history
Add `toInt`
  • Loading branch information
garyb authored Sep 1, 2023
2 parents c9e053d + 8fa4ea3 commit b06c9d5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"purescript-prelude": "^6.0.1",
"purescript-functions": "^6.0.0",
"purescript-integers": "^6.0.0",
"purescript-maybe": "^6.0.0",
"purescript-either": "^6.1.0",
"purescript-tuples": "^7.0.0",
Expand Down
10 changes: 9 additions & 1 deletion src/JSON.purs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module JSON
, toNull
, toBoolean
, toNumber
, toInt
, toString
, toArray
, toJArray
Expand All @@ -27,6 +28,7 @@ import Prelude
import Data.Either (Either(..))
import Data.Function.Uncurried (runFn2, runFn3, runFn7)
import Data.Maybe (Maybe(..))
import Data.Int as Int
import JSON.Internal (JArray, JObject, JSON) as Exports
import JSON.Internal (JArray, JObject, JSON)
import JSON.Internal as Internal
Expand Down Expand Up @@ -60,7 +62,7 @@ fromNumberWithDefault fallback n = runFn2 Internal._fromNumberWithDefault fallba

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

Expand Down Expand Up @@ -119,6 +121,12 @@ toBoolean json = runFn7 Internal._case fail Just fail fail fail fail json
toNumber :: JSON -> Maybe Number
toNumber json = runFn7 Internal._case fail fail Just fail fail fail json

-- | Converts a `JSON` `Number` into an `Int`.
-- |
-- | This is provided for convenience only.
toInt :: JSON -> Maybe Int
toInt = toNumber >=> Int.fromNumber

-- | Converts a `JSON` value to `String` if the `JSON` is a string.
toString :: JSON -> Maybe String
toString json = runFn7 Internal._case fail fail fail Just fail fail json
Expand Down

0 comments on commit b06c9d5

Please sign in to comment.