Skip to content

Commit

Permalink
Implement nwr shorthand
Browse files Browse the repository at this point in the history
node/way/relation
  • Loading branch information
simon04 authored and erictheise committed Dec 31, 2023
1 parent cc30959 commit a619ca7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 144 deletions.
4 changes: 4 additions & 0 deletions js/ffs.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ ffs.construct_query = function (search, comment, callback) {

// construct query
query_parts.push(" // query part for: “" + clauses_str + "”");
if (types.length === 3) {
types = ["nwr"];
query_parts.push(" // nwr is short for node/way/relation");
}
for (var t = 0; t < types.length; t++) {
var buffer = " " + types[t];
for (var c = 0; c < clauses.length; c++) buffer += clauses[c];
Expand Down
173 changes: 29 additions & 144 deletions tests/test.ffs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ describe("ide.ffs", function () {
var search = "foo=*";
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["foo"](bbox);' +
'way["foo"](bbox);' +
'relation["foo"](bbox);' +
");" +
out_str
"(" + 'nwr["foo"](bbox);' + ");" + out_str
);
});
});
Expand All @@ -40,12 +35,7 @@ describe("ide.ffs", function () {
var search = "foo!=*";
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["foo"!~".*"](bbox);' +
'way["foo"!~".*"](bbox);' +
'relation["foo"!~".*"](bbox);' +
");" +
out_str
"(" + 'nwr["foo"!~".*"](bbox);' + ");" + out_str
);
});
});
Expand All @@ -54,12 +44,7 @@ describe("ide.ffs", function () {
var search = "foo=bar";
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["foo"="bar"](bbox);' +
'way["foo"="bar"](bbox);' +
'relation["foo"="bar"](bbox);' +
");" +
out_str
"(" + 'nwr["foo"="bar"](bbox);' + ");" + out_str
);
});
});
Expand All @@ -68,12 +53,7 @@ describe("ide.ffs", function () {
var search = "foo!=bar";
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["foo"!="bar"](bbox);' +
'way["foo"!="bar"](bbox);' +
'relation["foo"!="bar"](bbox);' +
");" +
out_str
"(" + 'nwr["foo"!="bar"](bbox);' + ");" + out_str
);
});
});
Expand All @@ -82,12 +62,7 @@ describe("ide.ffs", function () {
var search = "foo~bar";
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["foo"~"bar"](bbox);' +
'way["foo"~"bar"](bbox);' +
'relation["foo"~"bar"](bbox);' +
");" +
out_str
"(" + 'nwr["foo"~"bar"](bbox);' + ");" + out_str
);
});
});
Expand All @@ -96,12 +71,7 @@ describe("ide.ffs", function () {
var search = "~foo~bar";
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node[~"foo"~"bar"](bbox);' +
'way[~"foo"~"bar"](bbox);' +
'relation[~"foo"~"bar"](bbox);' +
");" +
out_str
"(" + 'nwr[~"foo"~"bar"](bbox);' + ");" + out_str
);
});
});
Expand All @@ -110,12 +80,7 @@ describe("ide.ffs", function () {
var search = "foo!~bar";
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["foo"!~"bar"](bbox);' +
'way["foo"!~"bar"](bbox);' +
'relation["foo"!~"bar"](bbox);' +
");" +
out_str
"(" + 'nwr["foo"!~"bar"](bbox);' + ");" + out_str
);
});
});
Expand All @@ -125,24 +90,14 @@ describe("ide.ffs", function () {
var search = "foo:bar";
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["foo"~"bar"](bbox);' +
'way["foo"~"bar"](bbox);' +
'relation["foo"~"bar"](bbox);' +
");" +
out_str
"(" + 'nwr["foo"~"bar"](bbox);' + ");" + out_str
);
});
// but also escape special characters
search = "foo:'*'";
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["foo"~"\\\\*"](bbox);' +
'way["foo"~"\\\\*"](bbox);' +
'relation["foo"~"\\\\*"](bbox);' +
");" +
out_str
"(" + 'nwr["foo"~"\\\\*"](bbox);' + ");" + out_str
);
});
});
Expand All @@ -158,12 +113,7 @@ describe("ide.ffs", function () {
search = '"a key"="a value"';
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["a key"="a value"](bbox);' +
'way["a key"="a value"](bbox);' +
'relation["a key"="a value"](bbox);' +
");" +
out_str
"(" + 'nwr["a key"="a value"](bbox);' + ");" + out_str
);
});
});
Expand All @@ -173,38 +123,23 @@ describe("ide.ffs", function () {
search = "'foo bar'='asd fasd'";
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["foo bar"="asd fasd"](bbox);' +
'way["foo bar"="asd fasd"](bbox);' +
'relation["foo bar"="asd fasd"](bbox);' +
");" +
out_str
"(" + 'nwr["foo bar"="asd fasd"](bbox);' + ");" + out_str
);
});
});
it("quoted unicode string", function () {
var search = "name='بیجنگ'";
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["name"="بیجنگ"](bbox);' +
'way["name"="بیجنگ"](bbox);' +
'relation["name"="بیجنگ"](bbox);' +
");" +
out_str
"(" + 'nwr["name"="بیجنگ"](bbox);' + ");" + out_str
);
});
});
it("unicode string", function () {
var search = "name=Béziers";
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["name"="Béziers"](bbox);' +
'way["name"="Béziers"](bbox);' +
'relation["name"="Béziers"](bbox);' +
");" +
out_str
"(" + 'nwr["name"="Béziers"](bbox);' + ");" + out_str
);
});
});
Expand All @@ -216,24 +151,14 @@ describe("ide.ffs", function () {
search = "foo~/bar/";
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["foo"~"bar"](bbox);' +
'way["foo"~"bar"](bbox);' +
'relation["foo"~"bar"](bbox);' +
");" +
out_str
"(" + 'nwr["foo"~"bar"](bbox);' + ");" + out_str
);
});
// simple regex with modifier
search = "foo~/bar/i";
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["foo"~"bar",i](bbox);' +
'way["foo"~"bar",i](bbox);' +
'relation["foo"~"bar",i](bbox);' +
");" +
out_str
"(" + 'nwr["foo"~"bar",i](bbox);' + ");" + out_str
);
});
});
Expand All @@ -246,38 +171,23 @@ describe("ide.ffs", function () {
var search = "foo=bar and asd=fasd";
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["foo"="bar"]["asd"="fasd"](bbox);' +
'way["foo"="bar"]["asd"="fasd"](bbox);' +
'relation["foo"="bar"]["asd"="fasd"](bbox);' +
");" +
out_str
"(" + 'nwr["foo"="bar"]["asd"="fasd"](bbox);' + ");" + out_str
);
});
});
it("logical and (& operator)", function () {
var search = "foo=bar & asd=fasd";
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["foo"="bar"]["asd"="fasd"](bbox);' +
'way["foo"="bar"]["asd"="fasd"](bbox);' +
'relation["foo"="bar"]["asd"="fasd"](bbox);' +
");" +
out_str
"(" + 'nwr["foo"="bar"]["asd"="fasd"](bbox);' + ");" + out_str
);
});
});
it("logical and (&& operator)", function () {
var search = "foo=bar && asd=fasd";
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["foo"="bar"]["asd"="fasd"](bbox);' +
'way["foo"="bar"]["asd"="fasd"](bbox);' +
'relation["foo"="bar"]["asd"="fasd"](bbox);' +
");" +
out_str
"(" + 'nwr["foo"="bar"]["asd"="fasd"](bbox);' + ");" + out_str
);
});
});
Expand All @@ -287,12 +197,8 @@ describe("ide.ffs", function () {
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["foo"="bar"](bbox);' +
'way["foo"="bar"](bbox);' +
'relation["foo"="bar"](bbox);' +
'node["asd"="fasd"](bbox);' +
'way["asd"="fasd"](bbox);' +
'relation["asd"="fasd"](bbox);' +
'nwr["foo"="bar"](bbox);' +
'nwr["asd"="fasd"](bbox);' +
");" +
out_str
);
Expand All @@ -303,12 +209,8 @@ describe("ide.ffs", function () {
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["foo"="bar"](bbox);' +
'way["foo"="bar"](bbox);' +
'relation["foo"="bar"](bbox);' +
'node["asd"="fasd"](bbox);' +
'way["asd"="fasd"](bbox);' +
'relation["asd"="fasd"](bbox);' +
'nwr["foo"="bar"](bbox);' +
'nwr["asd"="fasd"](bbox);' +
");" +
out_str
);
Expand All @@ -319,12 +221,8 @@ describe("ide.ffs", function () {
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["foo"="bar"](bbox);' +
'way["foo"="bar"](bbox);' +
'relation["foo"="bar"](bbox);' +
'node["asd"="fasd"](bbox);' +
'way["asd"="fasd"](bbox);' +
'relation["asd"="fasd"](bbox);' +
'nwr["foo"="bar"](bbox);' +
'nwr["asd"="fasd"](bbox);' +
");" +
out_str
);
Expand All @@ -336,18 +234,10 @@ describe("ide.ffs", function () {
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
'node["foo"]["asd"](bbox);' +
'way["foo"]["asd"](bbox);' +
'relation["foo"]["asd"](bbox);' +
'node["foo"]["fasd"](bbox);' +
'way["foo"]["fasd"](bbox);' +
'relation["foo"]["fasd"](bbox);' +
'node["bar"]["asd"](bbox);' +
'way["bar"]["asd"](bbox);' +
'relation["bar"]["asd"](bbox);' +
'node["bar"]["fasd"](bbox);' +
'way["bar"]["fasd"](bbox);' +
'relation["bar"]["fasd"](bbox);' +
'nwr["foo"]["asd"](bbox);' +
'nwr["foo"]["fasd"](bbox);' +
'nwr["bar"]["asd"](bbox);' +
'nwr["bar"]["fasd"](bbox);' +
");" +
out_str
);
Expand Down Expand Up @@ -430,12 +320,7 @@ describe("ide.ffs", function () {
search = "id:123";
ffs.construct_query(search, undefined, function (err, result) {
expect(compact(result)).to.equal(
"(" +
"node(123)(bbox);" +
"way(123)(bbox);" +
"relation(123)(bbox);" +
");" +
out_str
"(" + "nwr(123)(bbox);" + ");" + out_str
);
});
});
Expand Down

0 comments on commit a619ca7

Please sign in to comment.