Skip to content

Commit

Permalink
Add withJsonPatch
Browse files Browse the repository at this point in the history
  • Loading branch information
jhbertra committed Oct 27, 2021
1 parent 463baf8 commit 24427cd
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
@@ -1,6 +1,7 @@
{
"name": "web-common",
"dependencies": {
"big-integer": "^1.6.50"
"big-integer": "^1.6.50",
"json-bigint": "^1.0.0"
}
}
16 changes: 15 additions & 1 deletion src/Data/BigInt/Argonaut.js
@@ -1,6 +1,20 @@
"use strict";

const bigInt = require("big-integer");
var bigInt = require("big-integer");
var JSONbig = require("json-bigint");

exports.patchJson = function () {
var stringify = JSON.stringify;
var parse = JSON.parse;
JSON.stringify = JSONbig.stringify;
JSON.parse = JSONbig.parse;
return { stringify: stringify, parse: parse };
}

exports.restoreJson = function (original) {
JSON.stringify = original.stringify;
JSON.parse = original.parse;
}

exports.decodeBigInt = function (fail, succ, json) {
if (typeof json === "number" || typeof json === "bigint") {
Expand Down
14 changes: 14 additions & 0 deletions src/Data/BigInt/Argonaut.purs
Expand Up @@ -23,10 +23,12 @@ module Data.BigInt.Argonaut
, toNonEmptyString
, toNumber
, toString
, withJsonPatch
, xor
) where

import Prologue

import Data.Argonaut.Aeson (maybeToEither)
import Data.Argonaut.Core (Json)
import Data.Argonaut.Decode (class DecodeJson, JsonDecodeError(..))
Expand All @@ -37,6 +39,10 @@ import Data.Function.Uncurried (Fn3, runFn3)
import Data.Generic.Rep (class Generic)
import Data.Newtype (class Newtype, over, unwrap)
import Data.String.NonEmpty (NonEmptyString)
import Effect (Effect)
import Effect.Aff (Aff, bracket)
import Effect.Class (liftEffect)
import Foreign (Foreign)

newtype BigInt
= BigInt BI.BigInt
Expand All @@ -63,6 +69,14 @@ foreign import decodeBigInt :: forall a. Fn3 a (BI.BigInt -> a) Json a

foreign import encodeBigInt :: BI.BigInt -> Json

foreign import patchJson :: Effect Foreign

foreign import restoreJson :: Foreign -> Effect Unit

withJsonPatch :: forall a. Aff a -> Aff a
withJsonPatch =
bracket (liftEffect patchJson) (liftEffect <<< restoreJson) <<< const

instance decodeJsonBigInt :: DecodeJson BigInt where
decodeJson =
maybeToEither (TypeMismatch "BigInt or Number")
Expand Down
9 changes: 6 additions & 3 deletions test/Main.purs
@@ -1,10 +1,12 @@
module Test.Main where

import Prologue

import Data.Array.Extra.Spec (arrayExtraSpec)
import Data.BigInt.Argonaut (withJsonPatch)
import Data.Cursor.Spec (cursorSpec)
import Data.Foldable.Extra.Spec (foldableExtraSpec)
import Data.String.Extra.Spec (stringExtraSpec)
import Data.Cursor.Spec (cursorSpec)
import Effect (Effect)
import Effect.Aff (launchAff_)
import PlutusTx.AssocMap.Spec (assocMapSpec)
Expand All @@ -13,8 +15,9 @@ import Test.Spec.Runner (runSpec)

main :: Effect Unit
main =
launchAff_
$ runSpec [ consoleReporter ] do
launchAff_ do
withJsonPatch do
runSpec [ consoleReporter ] do
cursorSpec
arrayExtraSpec
foldableExtraSpec
Expand Down

0 comments on commit 24427cd

Please sign in to comment.