Skip to content

Commit

Permalink
improvement & bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
roya0045 authored and nyalldawson committed Jan 31, 2022
1 parent 02a911f commit 3105c5e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
5 changes: 3 additions & 2 deletions resources/function_help/json/array_remove_at
Expand Up @@ -2,8 +2,9 @@
"name": "array_remove_at",
"type": "function",
"groups": ["Arrays"],
"description": "Returns an array with the given index removed.",
"description": "Returns an array with the item at the given index remove. Support positive (0 for the first element) and negative( the last -Nth value, -1 for the last element) index.",
"arguments": [ {"arg":"array","description":"an array"},
{"arg":"pos","description":"the position to remove (0 based)"}],
"examples": [ { "expression":"array_remove_at(array(1,2,3),1)", "returns":"[ 1, 3 ]"}]
"examples": [ { "expression":"array_remove_at(array(1,2,3),1)", "returns":"[ 1, 3 ]"},
{ "expression":"array_remove_at(array(1,2,3),-1)", "returns":"[ 1, 2 ]"}]
}
6 changes: 5 additions & 1 deletion src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -6190,7 +6190,11 @@ static QVariant fcnArrayInsert( const QVariantList &values, const QgsExpressionC
static QVariant fcnArrayRemoveAt( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QVariantList list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
list.removeAt( QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent ) );
int position = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
if ( position < 0 && ( list.length() + position ) >= 0 )
position = position + list.length();
if ( position < list.length() )
list.removeAt( position );
return convertToSameType( list, values.at( 0 ).type() );
}

Expand Down
6 changes: 6 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -3748,6 +3748,9 @@ class TestQgsExpression: public QObject
QStringList removeAtExpected = array;
removeAtExpected.removeAt( 0 );
QCOMPARE( QgsExpression( "array_remove_at(\"strings\", 0)" ).evaluate( &context ), QVariant( removeAtExpected ) );
QCOMPARE( QgsExpression( "array_remove_at(\"strings\", -2)" ).evaluate( &context ), QVariant( removeAtExpected ) );
QCOMPARE( QgsExpression( "array_remove_at(\"strings\", -4)" ).evaluate( &context ), QVariant( array ) );
QCOMPARE( QgsExpression( "array_remove_at(\"strings\", 4)" ).evaluate( &context ), QVariant( array ) );

QStringList removeAllExpected;
removeAllExpected << QStringLiteral( "a" ) << QStringLiteral( "b" ) << QStringLiteral( "d" );
Expand Down Expand Up @@ -3832,6 +3835,9 @@ class TestQgsExpression: public QObject
QVariantList removeAtExpected = array;
removeAtExpected.removeAt( 0 );
QCOMPARE( QgsExpression( "array_remove_at(\"ints\", 0)" ).evaluate( &context ), QVariant( removeAtExpected ) );
QCOMPARE( QgsExpression( "array_remove_at(\"ints\", -2)" ).evaluate( &context ), QVariant( removeAtExpected ) );
QCOMPARE( QgsExpression( "array_remove_at(\"ints\", -5)" ).evaluate( &context ), QVariant( array ) );
QCOMPARE( QgsExpression( "array_remove_at(\"ints\", 5)" ).evaluate( &context ), QVariant( array ) );

QVariantList removeAllExpected;
removeAllExpected << 1 << 2 << 4;
Expand Down

0 comments on commit 3105c5e

Please sign in to comment.