Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
roya0045 authored and nyalldawson committed Jan 31, 2022
1 parent 3105c5e commit 6b64270
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
6 changes: 3 additions & 3 deletions resources/function_help/json/array_remove_at
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "array_remove_at",
"type": "function",
"groups": ["Arrays"],
"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.",
"description": "Returns an array with the item at the given index remove. Supports 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 ]"},
{ "expression":"array_remove_at(array(1,2,3),-1)", "returns":"[ 1, 2 ]"}]
"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 ]"}]
}
2 changes: 1 addition & 1 deletion src/core/expression/qgsexpressionfunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6193,7 +6193,7 @@ static QVariant fcnArrayRemoveAt( const QVariantList &values, const QgsExpressio
int position = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
if ( position < 0 && ( list.length() + position ) >= 0 )
position = position + list.length();
if ( position < list.length() )
if ( position >= 0 && position < list.length() )
list.removeAt( position );
return convertToSameType( list, values.at( 0 ).type() );
}
Expand Down
2 changes: 2 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3751,6 +3751,7 @@ class TestQgsExpression: public QObject
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 ) );
QCOMPARE( QgsExpression( "array_remove_at(\"strings\", -40)" ).evaluate( &context ), QVariant( array ) );

QStringList removeAllExpected;
removeAllExpected << QStringLiteral( "a" ) << QStringLiteral( "b" ) << QStringLiteral( "d" );
Expand Down Expand Up @@ -3838,6 +3839,7 @@ class TestQgsExpression: public QObject
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 ) );
QCOMPARE( QgsExpression( "array_remove_at(\"ints\", -50)" ).evaluate( &context ), QVariant( array ) );

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

0 comments on commit 6b64270

Please sign in to comment.