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

only generate TS localtext proxy for objects - dont generate for values #6870

Merged
merged 1 commit into from
Aug 8, 2023
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
73 changes: 73 additions & 0 deletions packages/corelib/src/q/localtext.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
beforeEach(() => {
jest.resetModules();
jest.unmock('./system');
});

const mockLocalTextStore = (localTexts: Record<string, any>) => {
jest.mock('./system', () => ({
getStateStore: jest.fn(() => localTexts)
}));
}



describe('proxyTexts', () => {
it('proxies simple object', async () => {
mockLocalTextStore({
'a.b': 'Abc',
});

const proxyTexts = (await import('./localtext')).proxyTexts;
const texts = proxyTexts({}, '', {a: {}}) as any;

expect(texts.a.b).toEqual('Abc');
});

it('proxies nested object', async () => {
mockLocalTextStore({
'a.b.c': 'Ab12c',
'a.c': 'A1c',
});

const proxyTexts = (await import('./localtext')).proxyTexts;
const texts = proxyTexts({}, '', {
a: {
b: {}
}
}) as any;

expect(texts.a.b.c).toEqual('Ab12c');
expect(texts.a.c).toEqual('A1c');
});

it('proxies multiple nested object', async () => {
mockLocalTextStore({
'a.b.c': 'Ab12c',
'a.c': 'A1c',
'b.c.d': 'B1cd',
});

const proxyTexts = (await import('./localtext')).proxyTexts;
const texts = proxyTexts({}, '', {
a: {
b: {}
},
b: {
c: {}
}
}) as any;

expect(texts.b.c.d).toEqual('B1cd');
});

it('proxies single level object', async () => {
mockLocalTextStore({
'a': 'Abc',
});

const proxyTexts = (await import('./localtext')).proxyTexts;
const texts = proxyTexts({}, '', {}) as any;

expect(texts.a).toEqual('Abc');
});
});
2 changes: 0 additions & 2 deletions packages/corelib/src/q/localtext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ export function proxyTexts(o: Record<string, any>, p: string, t: Record<string,
get: (x: Object, y: string) => {
var tv = t[y];
if (tv == null)
return;
if (typeof tv == 'number')
return localText(p + y);
else {
var z = o[y];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ protected void GenerateTexts(bool module)
var jw = new JsonTextWriter(new System.IO.StringWriter(jwBuilder))
#endif
{
QuoteName = false
QuoteName = false,
Formatting = Formatting.Indented,
Indentation = 4
};
jw.WriteStartObject();
List<string> stack = new();
Expand Down Expand Up @@ -166,8 +168,6 @@ protected void GenerateTexts(bool module)
}
else
{
jw.WritePropertyName(part);
jw.WriteValue(1);
cw.Indented("export const ");
sb.Append(part);
sb.AppendLine(": string;");
Expand All @@ -191,7 +191,7 @@ protected void GenerateTexts(bool module)
else
sb.Append(@"['Texts'] = Q.proxyTexts(Texts, '', ");
jw.Flush();
sb.Append(jwBuilder);
sb.Append(string.Join("\n ", jwBuilder.ToString().Split('\n')));
sb.AppendLine(") as any;");
});

Expand Down