Skip to content

Commit

Permalink
Improve json string parser abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
wwahammy committed Oct 13, 2023
1 parent f03c455 commit 67fbe24
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,43 @@
import JsonStringParser from './JsonStringParser';

describe('JsonStringParser', () => {
it("when an empty array", () => {
expect(new JsonStringParser("[]").results).toStrictEqual([]);
});
it("when invalid with bracket", () => {
const parser = new JsonStringParser("[")
expect(parser.results).toStrictEqual([]);
});

it("when invalid with brace", () => {
const parser = new JsonStringParser("[{]")
expect(parser.results).toStrictEqual([]);
describe.each([
["with bracket", "["],
["with brace", "[{]"],
["with non-custom-field-description", "[{name:'name'}]"]
])("when invalid %s", (_n, input)=> {
const parser = new JsonStringParser(input)
it('has correct result', () => {
expect(parser.results).toStrictEqual([]);
});

it('has error', () => {
expect(parser.errors).not.toBeEmpty();
});

it('is marked not valid', () => {
expect(parser.isValid).toBe(false)
});
});

it('when invalid with non-custom-field-description', () => {
const parser = new JsonStringParser("[{name:'name'}]")
expect(parser.results).toStrictEqual([]);
})
describe.each([
['with non-custom-field-description', "[{name:'name', label: 'another'}]", [{name: 'name', label: 'another'}]],
['with different json quote', '[{name:"name", label: "another"}]', [{name: 'name', label: 'another'}]],
['when an empty array', '[]', []],
])("when valid %s", (_name, input, result) => {
const parser = new JsonStringParser(input);

it('when valid with non-custom-field-description', () => {
const parser = new JsonStringParser("[{name:'name', label: 'another'}]");
expect(parser.results).toStrictEqual([{name: 'name', label: 'another'}]);
});
it('has no errors', () => {
expect(parser.errors).toBeEmpty();
});

it('when valid with different json quote', () => {
const parser = new JsonStringParser('[{name:"name", label: "another"}]');
expect(parser.results).toStrictEqual([{name: 'name', label: 'another'}]);
});
it('has is marked valid', () => {
expect(parser.isValid).toStrictEqual(true);
});

it('matches expected result', () => {
expect(parser.results).toStrictEqual(result);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,37 @@ function isCustomFieldDesc(item:unknown) : item is CustomFieldDescription {
return typeof item == 'object' && has(item, 'name') && has(item, 'label');
}
export default class JsonStringParser {
public error:SyntaxError = null;
public errors:SyntaxError[] = [];
public readonly results: CustomFieldDescription[] = [];
constructor(public readonly fieldsString:string) {
this._parse();
}

get isValid(): boolean {
return !!this.error
return this.errors.length == 0;
}


private _parse = (): void => {
try {
const result = parse(this.fieldsString)
if (result instanceof Array) {
result.filter(isCustomFieldDesc).forEach((i) => {
this.results.push({ ...i});
result.forEach((i) => {
if (isCustomFieldDesc(i)) {
this.results.push({ ...i});
}

else {
this.errors.push(new SyntaxError(JSON.stringify(i) + " is not a valid custom field description"))
}
});
}
else {
this.errors.push(new SyntaxError("Input did not parse to an array"))
}
}
catch(e:any) {
this.error = e;
this.errors.push(e)
}
}
}
Expand Down

0 comments on commit 67fbe24

Please sign in to comment.