Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transloco): allow method calls with transpiled arguments in DefaultTranspiler (#830) #831

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion libs/transloco/src/lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ProviderScope, Translation } from './types';
import { getFunctionArgs } from './transloco.transpiler';

export function getValue<T>(obj: T, path: keyof T) {
if (!obj) {
Expand All @@ -10,7 +11,16 @@ export function getValue<T>(obj: T, path: keyof T) {
return obj[path];
}

return (path as string).split('.').reduce((p, c) => p?.[c], obj as any);
const pathMatcher = /\s*(\w+)(?:\((.*?)\))?\s*/;
return (path as string).split('.').reduce((p, c) => {
const match = pathMatcher.exec(c);
if (p && match) {
return isDefined(match[2])
? p[match[1]](...getFunctionArgs(match[2]))
: p[match[1]];
}
return undefined;
}, obj as any);
}

export function setValue(obj: any, prop: string, val: any) {
Expand Down
15 changes: 15 additions & 0 deletions libs/transloco/src/lib/tests/transpiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,21 @@ describe('TranslocoTranspiler', () => {
).toEqual('From 1 to 10');
});

it('should support params method calls', () => {
expect(
transpiler.transpile(
getTranspilerParams(
`Length: ${wrapParam('text.length')}, Initial: ${wrapParam('text.charAt(0)')}`,
{
params: {
text: 'Hello world',
},
},
),
),
).toEqual('Length: 11, Initial: H');
});

it('should support key referencing', () => {
const lang = flatten({
ab: `a b ${wrapParam('cd')}`,
Expand Down
Loading