Skip to content
This repository was archived by the owner on Jan 5, 2026. It is now read-only.
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
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export * from './range';
export * from './removeProperty';
export * from './replace';
export * from './replaceIgnoreCase';
export * from './reverse';
export * from './round';
export * from './select';
export * from './sentenceCase';
Expand Down
51 changes: 51 additions & 0 deletions libraries/adaptive-expressions/src/builtinFunctions/reverse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @module adaptive-expressions
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

import { Expression } from '../expression';
import { EvaluateExpressionDelegate, ExpressionEvaluator } from '../expressionEvaluator';
import { ExpressionType } from '../expressionType';
import { FunctionUtils } from '../functionUtils';
import { ReturnType } from '../returnType';

/**
* Reverses the order of the elements in a String or Array.
*/
export class Reverse extends ExpressionEvaluator {
/**
* Initializes a new instance of the `Reverse` class.
*/
public constructor() {
super(ExpressionType.Reverse, Reverse.evaluator(), ReturnType.String | ReturnType.Array, Reverse.validator);
}

/**
* @private
*/
private static evaluator(): EvaluateExpressionDelegate {
return FunctionUtils.applyWithError((args: any[]): any => {
let value = undefined;
let error = undefined;
if (typeof args[0] === 'string') {
value = args[0].split('').reverse().join('');
} else if (Array.isArray(args[0])) {
value = args[0].reverse();
} else {
error = `${args[0]} is not a string or list.`;
}

return { value, error };
}, FunctionUtils.verifyContainer);
}

/**
* @private
*/
private static validator(expression: Expression): void {
FunctionUtils.validateOrder(expression, [], ReturnType.String | ReturnType.Array);
}
}
1 change: 1 addition & 0 deletions libraries/adaptive-expressions/src/expressionFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export class ExpressionFunctions {
new BuiltinFunctions.RemoveProperty(),
new BuiltinFunctions.Replace(),
new BuiltinFunctions.ReplaceIgnoreCase(),
new BuiltinFunctions.Reverse(),
new BuiltinFunctions.Round(),
new BuiltinFunctions.Select(),
new BuiltinFunctions.SentenceCase(),
Expand Down
1 change: 1 addition & 0 deletions libraries/adaptive-expressions/src/expressionType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export class ExpressionType {
public static readonly IndicesAndValues: string = 'indicesAndValues';
public static readonly Flatten: string = 'flatten';
public static readonly Unique: string = 'unique';
public static readonly Reverse: string = 'reverse';

// Misc
public static readonly Constant: string = 'Constant';
Expand Down
2 changes: 2 additions & 0 deletions libraries/adaptive-expressions/tests/badExpression.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ const badExpressions = [
['contains(\'hello world\', \'hello\', \'new\')', 'should have 2 parameter'],
['count(items, 1)', 'should have 1 parameter'],
['count(1)', 'first param should be string, array or map'],
['reverse(items, 1)', 'should have 1 parameter'],
['reverse(1)', 'first param should be string or array'],
['empty(1,2)', 'should have two params'],
['first(items,2)', 'should have 1 param'],
['last(items,2)', 'should have 1 param'],
Expand Down
6 changes: 6 additions & 0 deletions libraries/adaptive-expressions/tests/expressionParser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ const testCases = [
['count(\'hello\')', 5],
['count("hello")', 5],
['count(concat(hello,\r\nworld))', 10],
['reverse("hello")', 'olleh'],
['reverse(reverse("hello"))', 'hello'],
['reverse(\'hello\')', 'olleh'],
['reverse(concat(hello,world))', 'dlrowolleh'],
['replace(\'hello\', \'l\', \'k\')', 'hekko'],
['replace(\'hello\', \'L\', \'k\')', 'hello'],
['replace(nullObj, \'L\', \'k\')', ''],
Expand Down Expand Up @@ -596,6 +600,8 @@ const testCases = [
['count(split(hello,\'e\'))', 2],
['count(createArray(\'h\', \'e\', \'l\', \'l\', \'o\'))', 5],
['empty(\'\')', true],
['reverse(split(hello,"e"))', ['llo', 'h']],
['reverse(createArray("h", "e", "l", "l", "o"))', ['o', 'l', 'l', 'e', 'h']],
['empty(\'a\')', false],
['empty(bag)', false],
['empty(items)', false],
Expand Down