mirror of https://gitee.com/bigwinds/arangodb
issue #1033: added SPLIT function
This commit is contained in:
parent
8f34b15625
commit
9ccf7b85f4
|
@ -30,6 +30,7 @@ For string processing, AQL offers the following functions:
|
|||
- *TRIM(value, type)*: Returns the string *value* with whitespace stripped
|
||||
from the start and/or end. The optional *type* parameter specifies from which parts
|
||||
of the string the whitespace is stripped:
|
||||
|
||||
- *type* 0 will strip whitespace from the start and end of the string
|
||||
- *type* 1 will strip whitespace from the start of the string only
|
||||
- *type* 2 will strip whitespace from the end of the string only
|
||||
|
@ -46,6 +47,14 @@ For string processing, AQL offers the following functions:
|
|||
from the end only. The optional *chars* parameter can be used to override the
|
||||
characters that should be removed from the string. It defaults to `\r\n\t `.
|
||||
|
||||
- *SPLIT(value, separator, limit)*: Splits the given string *value* into a list of
|
||||
strings, using the *separator*. The *separator* can either be a string or a
|
||||
list of strings. If the *separator* is the empty string, *value* will be split
|
||||
into a list of characters. If no *separator* is specified, *value* will be
|
||||
returned inside a list.
|
||||
The optional parameter *limit* can be used to limit the number of split values in
|
||||
the result. If no *limit* is given, the number of splits returned is not bounded.
|
||||
|
||||
- *REVERSE(value)*: Returns the reverse of the string *value*
|
||||
|
||||
- *CONTAINS(text, search, return-index)*: Checks whether the string
|
||||
|
|
|
@ -119,6 +119,7 @@ std::unordered_map<std::string, Function const> const Executor::FunctionNames{
|
|||
{ "RTRIM", Function("RTRIM", "AQL_RTRIM", "s|s", true, false, true) },
|
||||
{ "FIND_FIRST", Function("FIND_FIRST", "AQL_FIND_FIRST", "s,s|zn,zn", true, false, true) },
|
||||
{ "FIND_LAST", Function("FIND_LAST", "AQL_FIND_LAST", "s,s|zn,zn", true, false, true) },
|
||||
{ "SPLIT", Function("SPLIT", "AQL_SPLIT", "s|sl,n", true, false, true) },
|
||||
|
||||
// numeric functions
|
||||
{ "FLOOR", Function("FLOOR", "AQL_FLOOR", "n", true, false, true) },
|
||||
|
|
|
@ -1769,6 +1769,42 @@ function AQL_RTRIM (value, chars) {
|
|||
return AQL_TO_STRING(value).replace(new RegExp(chars, 'g'), '');
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief split a string using a separator
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
function AQL_SPLIT (value, separator, maxSplits) {
|
||||
"use strict";
|
||||
|
||||
if (separator === null || separator === undefined) {
|
||||
return [ AQL_TO_STRING(value) ];
|
||||
}
|
||||
|
||||
if (maxSplits === null || maxSplits === undefined) {
|
||||
maxSplits = undefined;
|
||||
}
|
||||
else {
|
||||
maxSplits = AQL_TO_NUMBER(maxSplits);
|
||||
}
|
||||
|
||||
if (maxSplits < 0) {
|
||||
WARN("SPLIT", INTERNAL.errors.ERROR_QUERY_FUNCTION_ARGUMENT_TYPE_MISMATCH);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (TYPEWEIGHT(separator) === TYPEWEIGHT_LIST) {
|
||||
var patterns = [];
|
||||
separator.forEach(function(s) {
|
||||
patterns.push(CREATE_REGEX_PATTERN(AQL_TO_STRING(s)));
|
||||
});
|
||||
|
||||
return AQL_TO_STRING(value).split(new RegExp(patterns.join("|"), "g"), maxSplits);
|
||||
}
|
||||
else {
|
||||
return AQL_TO_STRING(value).split(AQL_TO_STRING(separator), maxSplits);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief finds search in value
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -6982,6 +7018,7 @@ exports.AQL_RIGHT = AQL_RIGHT;
|
|||
exports.AQL_TRIM = AQL_TRIM;
|
||||
exports.AQL_LTRIM = AQL_LTRIM;
|
||||
exports.AQL_RTRIM = AQL_RTRIM;
|
||||
exports.AQL_SPLIT = AQL_SPLIT;
|
||||
exports.AQL_FIND_FIRST = AQL_FIND_FIRST;
|
||||
exports.AQL_FIND_LAST = AQL_FIND_LAST;
|
||||
exports.AQL_TO_BOOL = AQL_TO_BOOL;
|
||||
|
|
|
@ -780,6 +780,85 @@ function ahuacatlFunctionsTestSuite () {
|
|||
assertEqual([ "" ], getQueryResults("RETURN RIGHT('foo', -1.5)"));
|
||||
},
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief test split function
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
testSplit : function () {
|
||||
var values = [
|
||||
[ [ "" ], "", "\n" ],
|
||||
[ [ "" ], "", "foobar" ],
|
||||
[ [ ], "", "" ],
|
||||
[ [ ], "", [ "" ] ],
|
||||
[ [ "" ], "", [ "a", "b", "c" ] ],
|
||||
[ [ "this\nis\na\nt", "st" ], "this\nis\na\ntest", "e" ],
|
||||
[ [ "th", "s\n", "s\na\nt", "st" ], "this\nis\na\ntest", [ "e", "i" ] ],
|
||||
[ [ "th", "\n", "\na\ntest" ], "this\nis\na\ntest", "is" ],
|
||||
[ [ "this", "is", "a", "test" ], "this\nis\na\ntest", "\n" ],
|
||||
[ [ "this", "is", "a", "test" ], "this\nis\na\ntest", [ "\n" ] ],
|
||||
[ [ "this", "is", "a", "test" ], "this\nis\na\ntest", [ "\n", "\r" ] ],
|
||||
[ [ "this", "is", "a", "test" ], "this\ris\ra\rtest", [ "\n", "\r" ] ],
|
||||
[ [ "this", "is", "a", "test" ], "this\tis\ta\ttest", [ "\t" ] ],
|
||||
[ [ "this", "is", "a", "test" ], "this\tis\ta\ttest", "\t" ],
|
||||
[ [ "this", "is", "a", "test" ], "this\nis\ra\ttest", [ "\n", "\r", "\t" ] ],
|
||||
[ [ "this", "is", "a", "test" ], "this is a test", [ " " ] ],
|
||||
[ [ "this", "is", "a", "test" ], "this is a test", " " ],
|
||||
[ [ "this", "is", "a", "test" ], "this/SEP/is/SEP/a/SEP/test", "/SEP/" ],
|
||||
[ [ "this", "is", "a", "test" ], "this/SEP/is/SEP/a/SEP/test", [ "/SEP/" ] ],
|
||||
[ [ "this", "is", "a", "test" ], "this/SEP1/is/SEP2/a/SEP3/test", [ "/SEP1/", "/SEP2/", "/SEP3/" ] ],
|
||||
[ [ "the", "quick", "brown", "foxx" ], "the quick brown foxx", " " ],
|
||||
[ [ "the quick ", " foxx" ], "the quick brown foxx", "brown" ],
|
||||
[ [ "t", "h", "e", " ", "q", "u", "i", "c", "k", " ", "b", "r", "o", "w", "n", " ", "f", "o", "x", "x" ], "the quick brown foxx", "" ]
|
||||
];
|
||||
|
||||
values.forEach(function(value) {
|
||||
var expected = value[0], text = value[1], separator = value[2];
|
||||
assertEqual([ expected ], getQueryResults("RETURN SPLIT(" + JSON.stringify(text) + ", " + JSON.stringify(separator) + ")"), value);
|
||||
});
|
||||
},
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief test split function
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
testSplitMaxLength : function () {
|
||||
var values = [
|
||||
[ null, "foobar", "", -1 ],
|
||||
[ null, "foobar", "", -10 ],
|
||||
[ [ ], "foobar", "", 0 ],
|
||||
[ [ "f" ], "foobar", "", 1 ],
|
||||
[ [ ], "this\nis\na\ntest", "\n", 0 ],
|
||||
[ [ "this" ], "this\nis\na\ntest", "\n", 1 ],
|
||||
[ [ "this", "is", "a" ], "this\nis\na\ntest", "\n", 3 ],
|
||||
[ [ "this", "is", "a", "test" ], "this\nis\na\ntest", "\n", 5 ],
|
||||
[ [ "this", "is", "a", "test" ], "this\nis\na\ntest", "\n", 500 ],
|
||||
[ [ "t", "h", "i", "s", " " ], "this is a test", "", 5 ]
|
||||
];
|
||||
|
||||
values.forEach(function(value) {
|
||||
var expected = value[0], text = value[1], separator = value[2], splitMax= value[3];
|
||||
assertEqual([ expected ], getQueryResults("RETURN SPLIT(" + JSON.stringify(text) + ", " + JSON.stringify(separator) + ", " + JSON.stringify(splitMax) + ")"));
|
||||
});
|
||||
},
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief test split function
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
testSplitEmpty : function () {
|
||||
assertEqual([ [ "the foxx" ] ], getQueryResults("RETURN SPLIT('the foxx')"));
|
||||
assertEqual([ [ "" ] ], getQueryResults("RETURN SPLIT('')"));
|
||||
},
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief test split function
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
testSplitInvalid : function () {
|
||||
assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN SPLIT()");
|
||||
assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN SPLIT('foo', '', 10, '')");
|
||||
},
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief test trim function
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
Loading…
Reference in New Issue