mirror of
https://github.com/n8n-io/n8n.git
synced 2026-07-27 19:15:01 +02:00
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
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:
parent
f4365b1579
commit
cc4dbf9439
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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']" },
|
||||
],
|
||||
|
|
|
|||
|
|
@ -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],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user