Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/jmespath/src/Functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ class Functions {
})
public funcReverse(arg: string | Array<unknown>): string | Array<unknown> {
return Array.isArray(arg)
? arg.reverse()
? arg.toReversed()
: arg.split('').reverse().join('');
}

Expand All @@ -366,7 +366,7 @@ class Functions {
argumentsSpecs: [['array-number', 'array-string']],
})
public funcSort(arg: Array<string> | Array<number>): Array<unknown> {
return arg.sort((a: string | number, b: string | number): number => {
return arg.toSorted((a: string | number, b: string | number): number => {
if (typeof a === 'string') {
// We can safely cast a and b to string here because the signature decorator
// already enforces that all elements are of the same type
Expand Down
40 changes: 40 additions & 0 deletions packages/jmespath/tests/unit/compliance/functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,46 @@ import { describe, expect, it } from 'vitest';
import { search } from '../../../src/index.js';

describe('Functions tests', () => {
it.each([
{
expression: 'sort(numbers)',
expected: [1, 2, 3],
},
{
expression: 'sort(strings)',
expected: ['a', 'b', 'c'],
},
{
expression: 'reverse(numbers)',
expected: [2, 1, 3],
},
{
expression: 'reverse(strings)',
expected: ['a', 'c', 'b'],
},
])(
'does not mutate the input when evaluating $expression',
({ expression, expected }) => {
// Prepare
const data = {
numbers: [3, 1, 2],
strings: ['b', 'c', 'a'],
};

// Act
const result = search(expression, data);

// Assess
expect(result).toStrictEqual(expected);
expect(data).toStrictEqual({
numbers: [3, 1, 2],
strings: ['b', 'c', 'a'],
});
expect(result).not.toBe(data.numbers);
expect(result).not.toBe(data.strings);
}
);

it.each([
{
expression: 'abs(foo)',
Expand Down
Loading