Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
refactor(gql): extract ajax from parseList
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane Wilson committed Jun 11, 2015
1 parent e1b57ad commit 55105cf
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 20 deletions.
38 changes: 30 additions & 8 deletions app/scripts/components/gql/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,23 @@ module ngApp.components.gql {
});
}

parseList(left: string, right: string): ng.IPromise<IDdItem[]> {
getLastComma(s: string): number {
return s.lastIndexOf(this.GqlTokens.COMMA) + 1;
}

getFirstComma(s: string): number {
return s.indexOf(this.GqlTokens.COMMA);
}

getListContent(left: string, listStart: number, right: string): string {
var lComma = this.getLastComma(left);
lComma = lComma === 0 ? listStart : lComma;
var rComma = this.getFirstComma(right);
var listEnd = this.getEndOfList(right);
return left.substring(listStart, lComma) + right.substring(rComma + 1, listEnd);
}

parseList(left: string, right: string): { parts: IParts; listValues: string[] } {
/*
* ... FIELD OP [vvv, vvv, nnn|xxx, vvv, vvv] ...
* FIELD = field searching on
Expand All @@ -194,15 +210,19 @@ module ngApp.components.gql {
*/
// Get the beginning of the list
var listStart = this.getStartOfList(left);
// Get the end of the list
var listEnd = this.getEndOfList(right)
// Get the values of the list
var listContent = left.substring(listStart) + right.substring(listEnd);
var listContent = this.getListContent(left, listStart, right);
// Get array of list values
var listValues = this.getValuesOfList(listContent);
// Get all the fields needed for Ajax
var parts = this.getComplexParts(left, listStart);

return {
parts: this.getComplexParts(left, listStart),
listValues: listValues
}
}

ajaxList(parts: IParts, listValues): ng.IPromise<IDdItem[]> {
// Autocomplete suggestions
return this.ajaxRequest(parts.field).then((d) => {
return _.take(_.filter(d, (m) => {
Expand Down Expand Up @@ -322,7 +342,6 @@ module ngApp.components.gql {
$scope.active = INACTIVE;

$scope.onChange = function() {
console.log('on chage');
gqlParse();
var index = GqlService.getPos(element[0]);
$scope.left = $scope.query.substring(0, index);
Expand All @@ -348,7 +367,8 @@ module ngApp.components.gql {
GqlService.isUnbalanced(left, T.LBRACKET, T.RBRACKET)) {
// in_list_of_values
$scope.mode = Mode.List;
GqlService.parseList(left, right).then((d) => {
var ret: { parts: IParts; listValues: string[] } = GqlService.parseList(left, right);
GqlService.ajaxList(ret.parts, ret.listValues).then((d) => {
$scope.ddItems = d;
});
} else if (GqlService.isCountOdd(left, T.QUOTE)) {
Expand Down Expand Up @@ -574,13 +594,15 @@ module ngApp.components.gql {
clean(e: string): boolean;
getStartOfList(s: string): number;
getEndOfList(s: string): number;
getListContent(left: string, listStart: number, right: string): string;
getValuesOfList(s: string): string[];
getNeedleFromList(s: string): string;
getComplexParts(s: string, n: number): IParts;
splitField(s: string): IFieldParts;
getParts(s: string): IParts;
parseGrammarError(left: string, error: IGqlSyntaxError): IDdItem[];
parseList(left: string, right: string): ng.IPromise<IDdItem[]>;
parseList(left: string, right: string): { parts: IParts; listValues: string[] };
ajaxList(parts: IParts, listValues: string[]): ng.IPromise<IDdItem[]>;
parseQuoted(left: string): IParts;
ajaxQuoted(parts: IParts): ng.IPromise<IDdItem[]>;
ajaxRequest(field: string): ng.IPromise<IDdItem[]>;
Expand Down
62 changes: 50 additions & 12 deletions app/scripts/components/gql/tests/gql.service.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,44 @@ describe("GQL Parser", function() {
it("handles no match", inject(function (GqlService) {
expect(GqlService.getEndOfList("this is a phrase")).to.eq(16);
}));
it("handles empty string", inject(function (GqlService) {
expect(GqlService.getEndOfList("")).to.eq(0);
}));
it("handles empty list", inject(function (GqlService) {
expect(GqlService.getEndOfList("[]")).to.eq(1);
}));
});
describe("getLastComma", function() {
it("returns index of last ,", inject(function (GqlService) {
expect(GqlService.getLastComma("this is a ,phrase")).to.eq(11);
expect(GqlService.getLastComma("this ,is, a ,phrase")).to.eq(13);
}));
it("return 0 if no ,", inject(function (GqlService) {
expect(GqlService.getLastComma("this is a phrase")).to.eq(0);
}));
});
describe("getFirstComma", function() {
it("returns index of first ,", inject(function (GqlService) {
expect(GqlService.getFirstComma("this is a phrase,")).to.eq(16);
expect(GqlService.getFirstComma("this is, a phrase,")).to.eq(7);
}));
it("handles no match", inject(function (GqlService) {
expect(GqlService.getEndOfList("this is a phrase")).to.eq(16);
}));
});
describe("getListContent", function() {
it("returns string between [ in first string and ] in the second", inject(function (GqlService) {
expect(GqlService.getListContent("field in [one,two,three", 10, ",four,five]"))
.to.eq("one,two,four,five");
}));
it("handle no closing [", inject(function (GqlService) {
expect(GqlService.getListContent("field in [one,two,three", 10, ",four,five"))
.to.eq("one,two,four,five");
}));
it("handle empty list", inject(function (GqlService) {
expect(GqlService.getListContent("field in [", 10, "]")).to.eq("");
expect(GqlService.getListContent("field in [", 10, "")).to.eq("");
}));
});
describe("getValuesOfList", function() {
it("returns array of values", inject(function (GqlService) {
Expand Down Expand Up @@ -253,40 +291,40 @@ describe("GQL Parser", function() {
});
describe("parseList", function() {
it("handles list", inject(function (GqlService) {
sinon.spy(GqlService, 'getComplexParts');
GqlService.parseList("FIELD IN [one","]");
expect(GqlService.getComplexParts).to.have.returned({
var ret = GqlService.parseList("FIELD IN [one","]");
expect(ret.parts).to.eql({
field: "FIELD",
op: "IN",
needle: "one"
});
expect(ret.listValues).to.eql([""]);
}));
it("handles list with multiple items", inject(function (GqlService) {
sinon.spy(GqlService, 'getComplexParts');
GqlService.parseList("FIELD IN [one,two,three",",four,five]");
expect(GqlService.getComplexParts).to.have.returned({
var ret = GqlService.parseList("FIELD IN [one,two,three",",four,five]");
expect(ret.parts).to.eql({
field: "FIELD",
op: "IN",
needle: "three"
});
expect(ret.listValues).to.eql(["one","two","four","five"]);
}));
it("handles unfinished list", inject(function (GqlService) {
sinon.spy(GqlService, 'getComplexParts');
GqlService.parseList("FIELD IN [one,two,three",",four,five");
expect(GqlService.getComplexParts).to.have.returned({
var ret = GqlService.parseList("FIELD IN [one,two,three",",four,five");
expect(ret.parts).to.eql({
field: "FIELD",
op: "IN",
needle: "three"
});
expect(ret.listValues).to.eql(["one","two","four","five"]);
}));
it("handles cursor inside a value", inject(function (GqlService) {
sinon.spy(GqlService, 'getComplexParts');
GqlService.parseList("FIELD IN [one,two,thr","ee,four,five");
expect(GqlService.getComplexParts).to.have.returned({
var ret = GqlService.parseList("FIELD IN [one,two,thr","ee,four,five");
expect(ret.parts).to.eql({
field: "FIELD",
op: "IN",
needle: "thr"
});
expect(ret.listValues).to.eql(["one","two","four","five"]);
}));
});
describe("lhsTokenField", function () {
Expand Down

0 comments on commit 55105cf

Please sign in to comment.