diff --git a/js/server/ahuacatl.js b/js/server/ahuacatl.js index 7436bf667b..2c7c1ac9b9 100644 --- a/js/server/ahuacatl.js +++ b/js/server/ahuacatl.js @@ -1235,7 +1235,6 @@ function AHUACATL_MERGE () { var element = arguments[i]; if (AHUACATL_TYPEWEIGHT(element) !== AHUACATL_TYPEWEIGHT_DOCUMENT) { - print(element); throw "expecting documents for merge"; } @@ -1251,6 +1250,32 @@ function AHUACATL_MERGE () { return result; } +//////////////////////////////////////////////////////////////////////////////// +/// @brief create the union (all) of all arguments +//////////////////////////////////////////////////////////////////////////////// + +function AHUACATL_UNION () { + var result = [ ]; + + for (var i in arguments) { + var element = arguments[i]; + + if (AHUACATL_TYPEWEIGHT(element) !== AHUACATL_TYPEWEIGHT_LIST) { + throw "expecting lists for union"; + } + + for (var k in element) { + if (!element.hasOwnProperty(k)) { + continue; + } + + result.push(element[k]); + } + } + + return result; +} + //////////////////////////////////////////////////////////////////////////////// /// @} //////////////////////////////////////////////////////////////////////////////// diff --git a/js/server/js-ahuacatl.h b/js/server/js-ahuacatl.h index 88bb6f08c8..44f3c52f2f 100644 --- a/js/server/js-ahuacatl.h +++ b/js/server/js-ahuacatl.h @@ -1236,7 +1236,6 @@ static string JS_server_ahuacatl = " var element = arguments[i];\n" "\n" " if (AHUACATL_TYPEWEIGHT(element) !== AHUACATL_TYPEWEIGHT_DOCUMENT) {\n" - " print(element);\n" " throw \"expecting documents for merge\";\n" " }\n" "\n" @@ -1253,6 +1252,32 @@ static string JS_server_ahuacatl = "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" + "/// @brief create the union (all) of all arguments\n" + "////////////////////////////////////////////////////////////////////////////////\n" + "\n" + "function AHUACATL_UNION () {\n" + " var result = [ ];\n" + "\n" + " for (var i in arguments) {\n" + " var element = arguments[i];\n" + "\n" + " if (AHUACATL_TYPEWEIGHT(element) !== AHUACATL_TYPEWEIGHT_LIST) {\n" + " throw \"expecting lists for union\";\n" + " }\n" + "\n" + " for (var k in element) {\n" + " if (!element.hasOwnProperty(k)) {\n" + " continue;\n" + " }\n" + "\n" + " result.push(element[k]);\n" + " }\n" + " }\n" + "\n" + " return result; \n" + "}\n" + "\n" + "////////////////////////////////////////////////////////////////////////////////\n" "/// @}\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" diff --git a/js/server/tests/ahuacatl-functions.js b/js/server/tests/ahuacatl-functions.js index 3db80121a5..3be7ad2226 100644 --- a/js/server/tests/ahuacatl-functions.js +++ b/js/server/tests/ahuacatl-functions.js @@ -161,6 +161,37 @@ function ahuacatlFunctionsTestSuite () { var actual = getQueryResults("FOR u IN [ { \"id\" : 100, \"name\" : \"John\", \"age\" : 15 }, { \"id\" : 101, \"name\" : \"Corey\", \"age\" : 19 } ] return MERGE(u, { \"id\" : 9 }, { \"name\" : \"foo\", \"id\" : 33 })", false); assertEqual(expected, actual); }, + +//////////////////////////////////////////////////////////////////////////////// +/// @brief test union function +//////////////////////////////////////////////////////////////////////////////// + + testUnion1 : function () { + var expected = [ [ 1, 2, 3, 1, 2, 3 ], [ 1, 2, 3, 1, 2, 3 ] ]; + var actual = getQueryResults("FOR u IN [ 1, 2 ] return UNION([ 1, 2, 3 ], [ 1, 2, 3 ])", true); + assertEqual(expected, actual); + }, + +//////////////////////////////////////////////////////////////////////////////// +/// @brief test union function +//////////////////////////////////////////////////////////////////////////////// + + testUnion2 : function () { + var expected = [ [ 1, 2, 3, 3, 2, 1 ], [ 1, 2, 3, 3, 2, 1 ] ]; + var actual = getQueryResults("FOR u IN [ 1, 2 ] return UNION([ 1, 2, 3 ], [ 3, 2, 1 ])", true); + assertEqual(expected, actual); + }, + +//////////////////////////////////////////////////////////////////////////////// +/// @brief test union function +//////////////////////////////////////////////////////////////////////////////// + + testUnion3 : function () { + var expected = [ "Fred", "John", "John", "Amy" ]; + var actual = getQueryResults("FOR u IN UNION([ \"Fred\", \"John\" ], [ \"John\", \"Amy\"]) return u", true); + assertEqual(expected, actual); + }, + }; }