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

Simplified regex in removeJsonQuotes #419

Merged
merged 9 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
88 changes: 88 additions & 0 deletions src/lib/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@

// import * as Fetch from './fetch.js';

// TODO: Import './fetch.ts'. Can't get Jest to play nicely
// This is a C&P from fetch.ts
function removeJsonQuotes(json: string) {
let cleaned = JSON.stringify(JSON.parse(json), null, 2);
return cleaned.replace(/\"(\S+)\"\s*:/gm, "$1 :");
}

// function removeJsonQuotes(json: string) {
// let cleaned = JSON.stringify(JSON.parse(json), null, 2);
// return cleaned.replace(/\"([\w-]+)\"\s*:/gm, "$1 :");
// }

describe('fetch helper functions', () => {
Trivo25 marked this conversation as resolved.
Show resolved Hide resolved

it('removes quotes from JSON keys', () => {

const input = `{
"FirstName" :"abc",
"Email" : "a@a.com",
"Id" : "1",
"Phone" : "1234567890",
"Date" : "2 May 2016 23:59:59"
}`;

const expected = `{
FirstName : "abc",
Email : "a@a.com",
Id : "1",
Phone : "1234567890",
Date : "2 May 2016 23:59:59"
}`;
const actual = removeJsonQuotes(input);

expect(actual).toEqual(expected);
});


it('doesn\'t care about spacing around colon', () => {

const input = `{
"FirstName":"abc",
"Email" : "a@a.com",
"Id" : "1",
"Phone" :"1234567890",
"Date":
"2 May 2016 23:59:59"
}`;

const expected = `{
FirstName : "abc",
Email : "a@a.com",
Id : "1",
Phone : "1234567890",
Date : "2 May 2016 23:59:59"
}`;
const actual = removeJsonQuotes(input);

expect(actual).toEqual(expected);
});

it('parses keys with dashes', () => {

const input = `{
"First-Name":"abc",
"Email" : "a@a.com",
"Id" : "1",
"Phone" :"1234567890",
"Date":
"2 May 2016 23:59:59"
}`;

const expected = `{
First-Name : "abc",
Email : "a@a.com",
Id : "1",
Phone : "1234567890",
Date : "2 May 2016 23:59:59"
}`;

const actual = removeJsonQuotes(input);

expect(actual).toEqual(expected);
});

});
7 changes: 3 additions & 4 deletions src/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export {
setGraphqlEndpoint,
sendZkappQuery,
sendZkapp,
removeJsonQuotes,
};

export { Account };

let defaultGraphqlEndpoint = 'none';
Expand Down Expand Up @@ -575,11 +577,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]+(?<!\\)":/gm, (match) =>
match.replace(/"/g, '')
);
return cleaned.replace(/\"(\S+)\"\s*:/gm, "$1 :");
}

// TODO it seems we're not actually catching most errors here
Expand Down