diff --git a/src/lib/fetch.ts b/src/lib/fetch.ts index b23ef3659a..3f018550da 100644 --- a/src/lib/fetch.ts +++ b/src/lib/fetch.ts @@ -26,7 +26,9 @@ export { setGraphqlEndpoint, sendZkappQuery, sendZkapp, + removeJsonQuotes, }; + export { Account }; let defaultGraphqlEndpoint = 'none'; @@ -574,11 +576,8 @@ function sendZkappQuery(json: string) { // removes the quotes on JSON keys function removeJsonQuotes(json: string) { - // source: https://stackoverflow.com/a/65443215 let cleaned = JSON.stringify(JSON.parse(json), null, 2); - return cleaned.replace(/^[\t ]*"[^:\n\r]+(? - match.replace(/"/g, '') - ); + return cleaned.replace(/\"(\S+)\"\s*:/gm, '$1:'); } // TODO it seems we're not actually catching most errors here diff --git a/src/lib/fetch.unit-test.ts b/src/lib/fetch.unit-test.ts new file mode 100644 index 0000000000..49f0c22120 --- /dev/null +++ b/src/lib/fetch.unit-test.ts @@ -0,0 +1,122 @@ +import { shutdown } from '../index.js'; +import * as Fetch from './fetch.js'; +import { expect } from 'expect'; + +console.log('testing regex helpers'); + +let input, actual, expected; + +input = `{ + "array": [ + {"data": "string with \\"escaped quotes\\": 1"}, { "otherdata": "otherstrin\\"g"}] +}`; + +expected = `{ + array: [ + { + data: "string with \\"escaped quotes\\": 1" + }, + { + otherdata: "otherstrin\\"g" + } + ] +}`; + +actual = Fetch.removeJsonQuotes(input); +expect(actual).toEqual(expected); + +input = `{ + "array": [ + { + "data" : "x" + }, + { + "data": 1},{ + "data": { + "otherData": "x" + } + } + ] +}`; + +expected = `{ + array: [ + { + data: "x" + }, + { + data: 1 + }, + { + data: { + otherData: "x" + } + } + ] +}`; + +actual = Fetch.removeJsonQuotes(input); +expect(actual).toEqual(expected); + +input = `{ + "FirstName" :"abc", + "Email" : "a@a.com", + "Id" : "1", + "Phone" : "1234567890", + "Date" : "2 May 2016 23:59:59" +}`; + +expected = `{ + FirstName: "abc", + Email: "a@a.com", + Id: "1", + Phone: "1234567890", + Date: "2 May 2016 23:59:59" +}`; + +actual = Fetch.removeJsonQuotes(input); +expect(actual).toEqual(expected); + +input = `{ +"FirstName":"abc", +"Email" : "a@a.com", +"Id" : "1", +"Phone" :"1234567890", +"Date": + "2 May 2016 23:59:59" +}`; + +expected = `{ + FirstName: "abc", + Email: "a@a.com", + Id: "1", + Phone: "1234567890", + Date: "2 May 2016 23:59:59" +}`; +actual = Fetch.removeJsonQuotes(input); + +expect(actual).toEqual(expected); + +input = `{ + "First-Name":"abc", + "Email" : "a@a.com", + "Id" : "1", + "Phone" :"1234567890", + "Date": + "2 May 2016 23:59:59" +}`; + +expected = `{ + First-Name: "abc", + Email: "a@a.com", + Id: "1", + Phone: "1234567890", + Date: "2 May 2016 23:59:59" +}`; + +actual = Fetch.removeJsonQuotes(input); + +expect(actual).toEqual(expected); + +console.log('regex tests complete 🎉'); +shutdown();