Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into bhart/extend-tx-type
Browse files Browse the repository at this point in the history
  • Loading branch information
ngua committed Jan 26, 2022
2 parents 8e11931 + c7762c2 commit 352ffdd
Show file tree
Hide file tree
Showing 21 changed files with 535 additions and 134 deletions.
@@ -0,0 +1 @@
["0","12","-1","1.5","-1.431","1123098123928347023894729384","1\"k\"0","10","10E-20","-2.5324e-23",null,false,true,"10"]
@@ -0,0 +1 @@
{"10":"10.5","1 0":null,"1\"0":"10","2\"0":"-20e-20","nest":[{"a":"10e-20"},{"b":false}]}
@@ -0,0 +1 @@
{"a":"\\\\","b":"123"}
1 change: 1 addition & 0 deletions fixtures/test/parsing/json_stringify_numbers/input/t1.json
@@ -0,0 +1 @@
[0,12,-1,1.5,-1.431,1123098123928347023894729384,"1\"k\"0","10",10E-20,-2.5324e-23,null,false,true,10]
10 changes: 10 additions & 0 deletions fixtures/test/parsing/json_stringify_numbers/input/t2.json
@@ -0,0 +1,10 @@
{
"10": 10.5,
"1 0": null,
"1\"0": 10,
"2\"0": "-20e-20",
"nest": [
{"a":10e-20},
{"b":false}
]
}
1 change: 1 addition & 0 deletions fixtures/test/parsing/json_stringify_numbers/input/t3.json
@@ -0,0 +1 @@
{"a":"\\\\","b":123}
1 change: 1 addition & 0 deletions nix/dev-shell.nix
Expand Up @@ -18,6 +18,7 @@ pkgs.mkShell {
spago
purescript-language-server
purty
pscid
spago2nix
pkgs.nodePackages.node2nix
nodejs
Expand Down
29 changes: 16 additions & 13 deletions node-packages.nix

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

158 changes: 80 additions & 78 deletions package-lock.json

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

15 changes: 8 additions & 7 deletions spago.dhall
Expand Up @@ -3,30 +3,34 @@ Welcome to a Spago project!
You can edit this file as you like.
-}
{ name = "my-project"
, dependencies =
, dependencies =
[ "aff"
, "argonaut"
, "arraybuffer-types"
, "arrays"
, "argonaut"
, "bigints"
, "console"
, "const"
, "control"
, "debug"
, "either"
, "effect"
, "either"
, "exceptions"
, "foldable-traversable"
, "foreign-object"
, "gen"
, "identity"
, "maybe"
, "medea"
, "mote"
, "node-buffer"
, "node-fs-aff"
, "node-path"
, "nonempty"
, "ordered-collections"
, "prelude"
, "psci-support"
, "quickcheck"
, "rationals"
, "refs"
, "spec"
Expand All @@ -38,8 +42,5 @@ You can edit this file as you like.
, "unordered-collections"
]
, packages = ./packages.dhall
, sources =
[ "src/**/*.purs"
, "test/**/*.purs"
]
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
}
53 changes: 53 additions & 0 deletions src/Helpers.js
@@ -0,0 +1,53 @@

// jsonTurnNumbersToStrings :: String -> String
exports.jsonTurnNumbersToStrings = (str) => {
const s = String(str);

var arr = [];
var prev_in_number = false;
var in_number = false;
var in_string = false;
var escaped = -1;

for (var i = 0, n = s.length; i < n; ++i) {
const c = s[i];
// set the escape flag
if (in_string && escaped!=i){
if (c == "\\"){
escaped = i+1;
}
}
// set in_string flag
if (c == '"' && escaped!=i) {
in_string = !in_string;
}
// set in_number flag and quote numbers
if(!in_string){
if (c >= '0' && c <= '9' || c == '-') {
in_number=true;
}
// assuming a number can only end with:
if (c == ',' || c == '}' || c == ']' || /\s/.test(c)){
if(in_number){
arr.push("\"");
}
in_number=false;
}
if(in_number){
if(!prev_in_number){
arr.push("\"");
}
}
prev_in_number = in_number;
}
// push char if in string or is not a whitespace
if(in_string || !/\s/.test(c)){
arr.push(c);
}
}
// simple number case - close with quote
if (!in_string && in_number){
arr.push("\"");
}
return arr.join('');
};
19 changes: 19 additions & 0 deletions src/Helpers.purs
@@ -0,0 +1,19 @@
module Helpers(parseJsonStringifyNumbers, jsonTurnNumbersToStrings) where

import Data.Either (Either)
import Data.Argonaut (JsonDecodeError, Json, parseJson)

import Prelude

-- | Assuming a valid JSON string in its input, the function will quote each
-- | value that would otherwise be parsed by JSON.parse() as a number.
-- | NOTE it discards whitespaces outside of the would be json strings
foreign import jsonTurnNumbersToStrings :: String -> String


-- | Parse JSON from string. It parses numbers as strings.
parseJsonStringifyNumbers :: String -> Either JsonDecodeError Json
parseJsonStringifyNumbers s = do
_ <- parseJson s
-- valid json ensured at this point
parseJson $ jsonTurnNumbersToStrings s
3 changes: 2 additions & 1 deletion src/Ogmios.purs
Expand Up @@ -16,6 +16,7 @@ import Effect.Console (log)
import Effect.Exception (Error, error)
import Effect.Ref as Ref
import Types.JsonWsp (Address, JsonWspResponse, UtxoQR, mkUtxosAtQuery, parseJsonWspResponse)
import Helpers as Helpers

-- This module defines an Aff interface for Ogmios Websocket Queries
-- Since WebSockets do not define a mechanism for linking request/response
Expand Down Expand Up @@ -190,7 +191,7 @@ utxoQueryDispatch
-> String
-> Effect (Either Json.JsonDecodeError (Effect Unit))
utxoQueryDispatch ref str = do
let parsed' = parseJsonWspResponse =<< Json.parseJson str
let parsed' = parseJsonWspResponse =<< Helpers.parseJsonStringifyNumbers str
case parsed' of
(Left err) -> pure $ Left err
(Right res) -> afterParse res
Expand Down
1 change: 1 addition & 0 deletions src/Types/JsonWsp.js
Expand Up @@ -3,3 +3,4 @@ const uniqid = require ("uniqid");
// _uniqueId :: String -> Effect String
exports._uniqueId = str => () => uniqid(str)

exports.emptyUint8Array = new Uint8Array();

0 comments on commit 352ffdd

Please sign in to comment.