fix: Return reversed array when calling .reverse() (#34625)
Some checks are pending
CI: Master (Build, Test, Lint) / Build for Github Cache (push) Waiting to run
CI: Master (Build, Test, Lint) / Unit tests (22.22.3) (push) Waiting to run
CI: Master (Build, Test, Lint) / Unit tests (24.18.0) (push) Waiting to run
CI: Master (Build, Test, Lint) / Lint (push) Waiting to run
CI: Master (Build, Test, Lint) / Performance (push) Waiting to run
CI: Master (Build, Test, Lint) / Notify Slack on failure (push) Blocked by required conditions

Co-authored-by: RomanDavydchuk <roman.davydchuk@n8n.io>
This commit is contained in:
yehorkardash 2026-07-21 22:25:24 +02:00 committed by GitHub
parent f4365b1579
commit cc4dbf9439
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 25 additions and 1 deletions

View File

@ -30,6 +30,10 @@ function last(value: unknown[]): unknown {
return value[value.length - 1];
}
function reverse(value: unknown[]): unknown[] {
return [...value].reverse();
}
function pluck(value: unknown[], extraArgs: unknown[]): unknown[] {
if (!Array.isArray(extraArgs)) {
throw new ExpressionExtensionError('arguments must be passed to pluck');
@ -684,6 +688,7 @@ export const arrayExtensions: ExtensionMap = {
unique,
first,
last,
reverse,
pluck,
randomItem,
sum,

View File

@ -30,6 +30,10 @@ function last(value: unknown[]): unknown {
return value[value.length - 1];
}
function reverse(value: unknown[]): unknown[] {
return [...value].reverse();
}
function pluck(value: unknown[], extraArgs: unknown[]): unknown[] {
if (!Array.isArray(extraArgs)) {
throw new ExpressionError('arguments must be passed to pluck');
@ -686,6 +690,7 @@ export const arrayExtensions: ExtensionMap = {
unique,
first,
last,
reverse,
pluck,
randomItem,
sum,

View File

@ -356,7 +356,7 @@ export const arrayMethods: NativeDoc = {
reverse: {
doc: {
name: 'reverse',
description: 'Reverses the order of the elements in the array',
description: 'Returns a new array with the elements in reverse order',
examples: [
{ example: "['dog', 'bites', 'man'].reverse()", evaluated: "['man', 'bites', 'dog']" },
],

View File

@ -64,4 +64,18 @@ describe('Expression — array proxy semantics (engine parity)', () => {
it('implicit string coercion uses Array.prototype.toString', () => {
expect(evaluate('={{ "items: " + $json.arr }}', { arr: [1, 2, 3] })).toBe('items: 1,2,3');
});
it('reverse returns a reversed copy without mutating a subsequent sibling read', () => {
expect(evaluate('={{ [$json.arr.reverse(), $json.arr] }}', { arr: [1, 2, 3] })).toEqual([
[3, 2, 1],
[1, 2, 3],
]);
});
it('reverse does not mutate a preceding sibling read', () => {
expect(evaluate('={{ [$json.arr, $json.arr.reverse()] }}', { arr: [1, 2, 3] })).toEqual([
[1, 2, 3],
[3, 2, 1],
]);
});
});