Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toInt #2

Merged
merged 3 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading