From ec8ff1c36308c22d82028f3cd954da864f0b344b Mon Sep 17 00:00:00 2001 From: isc-auf Date: Wed, 15 Jan 2025 20:26:37 +0100 Subject: [PATCH] feat(transloco): transpile function arguments in FunctionalTranspiler (#829) Signed-off-by: isc-auf --- libs/transloco/src/lib/tests/transpiler.spec.ts | 16 ++++++++++++++++ libs/transloco/src/lib/transloco.transpiler.ts | 6 +++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/libs/transloco/src/lib/tests/transpiler.spec.ts b/libs/transloco/src/lib/tests/transpiler.spec.ts index 1f212c87..2ce9842a 100644 --- a/libs/transloco/src/lib/tests/transpiler.spec.ts +++ b/libs/transloco/src/lib/tests/transpiler.spec.ts @@ -114,6 +114,22 @@ describe('TranslocoTranspiler', () => { ); }); + it('should transpile the function params', () => { + const spy = spyOn(transpilerFunctions['upperCase'], 'transpile'); + transpiler.transpile(getTranspilerParams('[[ upperCase(lowercase) ]]')); + expect(spy).toHaveBeenCalledWith('lowercase'); + spy.calls.reset(); + transpiler.transpile( + getTranspilerParams( + '[[ upperCase(lowercase, {{person}}, {{ anotherParson.name }}) ]]', + { + params: { person: 'Shahar', anotherParson: { name: 'Netanel' } }, + }, + ), + ); + expect(spy as any).toHaveBeenCalledWith('lowercase', 'Shahar', 'Netanel'); + }); + it('should work with interpolation params', () => { const parsed = transpiler.transpile( getTranspilerParams('[[ testParams(and {{anotherParson}}) ]]', { diff --git a/libs/transloco/src/lib/transloco.transpiler.ts b/libs/transloco/src/lib/transloco.transpiler.ts index 59bbbffb..8f5626d7 100644 --- a/libs/transloco/src/lib/transloco.transpiler.ts +++ b/libs/transloco/src/lib/transloco.transpiler.ts @@ -181,7 +181,11 @@ export class FunctionalTranspiler const func: TranslocoTranspilerFunction = this.injector.get(functionName); - return func.transpile(...getFunctionArgs(args)); + return func.transpile( + ...getFunctionArgs(args).map((arg) => + this.transpile({ value: arg, ...rest }), + ), + ); } catch (e: unknown) { let message = `There is an error in: '${value}'. Check that the you used the right syntax in your translation and that the implementation of ${functionName} is correct.`;