@next-box/i18n 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/cjs/client.cjs +2 -1
- package/dist/cjs/client.cjs.map +1 -1
- package/dist/cjs/components/I18nProvider.cjs +3 -0
- package/dist/cjs/components/I18nProvider.cjs.map +1 -0
- package/dist/cjs/components/Md.cjs +3 -0
- package/dist/cjs/components/Md.cjs.map +1 -0
- package/dist/cjs/constants.cjs +2 -0
- package/dist/cjs/constants.cjs.map +1 -0
- package/dist/cjs/helpers/injectVariables.cjs +2 -0
- package/dist/cjs/helpers/injectVariables.cjs.map +1 -0
- package/dist/cjs/helpers/loadConfig.cjs +2 -0
- package/dist/cjs/helpers/loadConfig.cjs.map +1 -0
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/macros.cjs +1 -1
- package/dist/cjs/macros.cjs.map +1 -1
- package/dist/cjs/main.cjs +2 -0
- package/dist/cjs/main.cjs.map +1 -0
- package/dist/esm/client.mjs +2 -1
- package/dist/esm/client.mjs.map +1 -1
- package/dist/esm/components/I18nProvider.mjs +3 -0
- package/dist/esm/components/I18nProvider.mjs.map +1 -0
- package/dist/esm/components/Md.mjs +3 -0
- package/dist/esm/components/Md.mjs.map +1 -0
- package/dist/esm/constants.mjs +2 -0
- package/dist/esm/constants.mjs.map +1 -0
- package/dist/esm/helpers/injectVariables.mjs +2 -0
- package/dist/esm/helpers/injectVariables.mjs.map +1 -0
- package/dist/esm/helpers/loadConfig.mjs +2 -0
- package/dist/esm/helpers/loadConfig.mjs.map +1 -0
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/macros.mjs +1 -1
- package/dist/esm/macros.mjs.map +1 -1
- package/dist/esm/main.mjs +2 -0
- package/dist/esm/main.mjs.map +1 -0
- package/dist/production.analysis.txt +10 -10
- package/dist/types/cjs/client.d.cts.map +1 -1
- package/dist/types/cjs/components/I18nProvider.d.cts.map +1 -1
- package/dist/types/cjs/components/Md.d.cts.map +1 -1
- package/dist/types/cjs/helpers/injectVariables.d.cts.map +1 -1
- package/dist/types/cjs/main.d.cts.map +1 -1
- package/dist/types/esm/client.d.ts.map +1 -1
- package/dist/types/esm/components/I18nProvider.d.ts.map +1 -1
- package/dist/types/esm/components/Md.d.ts.map +1 -1
- package/dist/types/esm/helpers/injectVariables.d.ts.map +1 -1
- package/dist/types/esm/main.d.ts.map +1 -1
- package/dist/types/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +13 -12
- package/rollup.config.mjs +1 -9
- package/src/client.ts +2 -0
- package/src/components/I18nProvider.tsx +2 -0
- package/src/components/Md.test.tsx +34 -32
- package/src/components/Md.tsx +2 -0
- package/src/helpers/injectVariables.test.ts +37 -0
- package/src/helpers/injectVariables.ts +3 -4
- package/src/macros.test.ts +34 -21
- package/src/main.test.ts +98 -62
- package/src/main.ts +0 -2
- package/src/types.ts +1 -1
- package/tsconfig.json +3 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@next-box/i18n",
|
|
3
3
|
"description": "A type-safe content resolver with content path auto-complete and value preview.",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.3",
|
|
5
5
|
"author": "miami-man",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/badbatch/next-box",
|
|
@@ -52,23 +52,24 @@
|
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"callsites": "^4.2.0",
|
|
55
|
-
"markdown-to-jsx": "^9.10.
|
|
56
|
-
"type-fest": "^5.
|
|
57
|
-
"zcb": "^0.3.
|
|
55
|
+
"markdown-to-jsx": "^9.10.4",
|
|
56
|
+
"type-fest": "^5.10.0",
|
|
57
|
+
"zcb": "^0.3.10"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
60
|
"react": "<20",
|
|
61
61
|
"react-dom": "<20"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
|
-
"@jest/globals": "^30.
|
|
65
|
-
"@testing-library/react": "^16.3.
|
|
66
|
-
"@types/
|
|
67
|
-
"@types/react
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
"react
|
|
64
|
+
"@jest/globals": "^30.5.2",
|
|
65
|
+
"@testing-library/react": "^16.3.3",
|
|
66
|
+
"@types/node": "^26.6.6",
|
|
67
|
+
"@types/react": "^19.3.0",
|
|
68
|
+
"@types/react-dom": "^19.3.0",
|
|
69
|
+
"cts-types": "^0.0.17",
|
|
70
|
+
"del-cli": "^8.0.1",
|
|
71
|
+
"react": "^19.3.1",
|
|
72
|
+
"react-dom": "^19.3.1"
|
|
72
73
|
},
|
|
73
74
|
"scripts": {
|
|
74
75
|
"build": "pnpm run clean:dist && pnpm run compile",
|
package/rollup.config.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import config from '../../rollup.config.mjs';
|
|
2
2
|
|
|
3
|
-
const { input,
|
|
3
|
+
const { input, ...otherConfig } = config;
|
|
4
4
|
|
|
5
5
|
// rollup requires default export
|
|
6
6
|
// eslint-disable-next-line import-x/no-default-export
|
|
@@ -9,17 +9,9 @@ export default [
|
|
|
9
9
|
{
|
|
10
10
|
...otherConfig,
|
|
11
11
|
input: input.replace('index', 'client'),
|
|
12
|
-
output: {
|
|
13
|
-
...output,
|
|
14
|
-
file: output.file.replace('index', 'client'),
|
|
15
|
-
},
|
|
16
12
|
},
|
|
17
13
|
{
|
|
18
14
|
...otherConfig,
|
|
19
15
|
input: input.replace('index', 'macros'),
|
|
20
|
-
output: {
|
|
21
|
-
...output,
|
|
22
|
-
file: output.file.replace('index', 'macros'),
|
|
23
|
-
},
|
|
24
16
|
},
|
|
25
17
|
];
|
package/src/client.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/** @jest-environment @repodog/jest-config/FailOnJsdomConsoleEnvironment.mjs */
|
|
2
|
+
|
|
3
|
+
import { describe, expect, it } from '@jest/globals';
|
|
4
4
|
import { render } from '@testing-library/react';
|
|
5
5
|
import { type MarkdownToJSX } from 'markdown-to-jsx';
|
|
6
6
|
import { type ReactNode } from 'react';
|
|
@@ -18,14 +18,17 @@ const componentMapper: MarkdownToJSX.Overrides = {
|
|
|
18
18
|
p: ({ children }: ComponentProps) => <p data-disaplay-name="p">{children}</p>,
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
-
describe('
|
|
22
|
-
it('
|
|
23
|
-
|
|
21
|
+
describe('Md', () => {
|
|
22
|
+
it('When markdown contains mapped components, then it renders the mapped React components', () => {
|
|
23
|
+
// Arrange
|
|
24
|
+
const wrapper = ({ children }: ComponentProps) => (
|
|
24
25
|
<I18nProvider componentMapper={componentMapper}>{children}</I18nProvider>
|
|
25
26
|
);
|
|
26
27
|
|
|
27
|
-
|
|
28
|
+
// Act
|
|
29
|
+
const { container } = render(<Md>{'<CustomComponent><p>content</p></CustomComponent>'}</Md>, { wrapper });
|
|
28
30
|
|
|
31
|
+
// Assert
|
|
29
32
|
expect(container).toMatchInlineSnapshot(`
|
|
30
33
|
<div>
|
|
31
34
|
<div
|
|
@@ -41,34 +44,33 @@ describe('MarkdownToJsx', () => {
|
|
|
41
44
|
`);
|
|
42
45
|
});
|
|
43
46
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
it('When markdown contains template variables, then it injects them before rendering mapped components', () => {
|
|
48
|
+
// Arrange
|
|
49
|
+
const wrapper = ({ children }: ComponentProps) => (
|
|
50
|
+
<I18nProvider componentMapper={componentMapper}>{children}</I18nProvider>
|
|
51
|
+
);
|
|
49
52
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
</
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
);
|
|
53
|
+
// Act
|
|
54
|
+
const { container } = render(
|
|
55
|
+
<Md variables={{ adjective: 'great' }}>
|
|
56
|
+
{"<CustomComponent><p>{'Have a {{adjective}} day'}</p></CustomComponent>"}
|
|
57
|
+
</Md>,
|
|
58
|
+
{ wrapper },
|
|
59
|
+
);
|
|
58
60
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
// Assert
|
|
62
|
+
expect(container).toMatchInlineSnapshot(`
|
|
63
|
+
<div>
|
|
64
|
+
<div
|
|
65
|
+
data-display-name="CustomComponent"
|
|
66
|
+
>
|
|
67
|
+
<p
|
|
68
|
+
data-disaplay-name="p"
|
|
63
69
|
>
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
>
|
|
67
|
-
{'Have a great day'}
|
|
68
|
-
</p>
|
|
69
|
-
</div>
|
|
70
|
+
{'Have a great day'}
|
|
71
|
+
</p>
|
|
70
72
|
</div>
|
|
71
|
-
|
|
72
|
-
|
|
73
|
+
</div>
|
|
74
|
+
`);
|
|
73
75
|
});
|
|
74
76
|
});
|
package/src/components/Md.tsx
CHANGED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { describe, expect, it } from '@jest/globals';
|
|
2
|
+
import { injectVariables } from './injectVariables.ts';
|
|
3
|
+
|
|
4
|
+
describe('injectVariables', () => {
|
|
5
|
+
it('When a variable is zero, then it interpolates the numeric value', () => {
|
|
6
|
+
// Arrange
|
|
7
|
+
const markdown = 'There are {{count}} items';
|
|
8
|
+
|
|
9
|
+
// Act
|
|
10
|
+
const result = injectVariables(markdown, { count: 0 });
|
|
11
|
+
|
|
12
|
+
// Assert
|
|
13
|
+
expect(result).toBe('There are 0 items');
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('When a variable is an empty string, then it interpolates the empty value', () => {
|
|
17
|
+
// Arrange
|
|
18
|
+
const markdown = 'Hello{{name}}!';
|
|
19
|
+
|
|
20
|
+
// Act
|
|
21
|
+
const result = injectVariables(markdown, { name: '' });
|
|
22
|
+
|
|
23
|
+
// Assert
|
|
24
|
+
expect(result).toBe('Hello!');
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('When a variable is missing, then it throws an error containing the variable name', () => {
|
|
28
|
+
// Arrange
|
|
29
|
+
const markdown = 'Hello {{name}}!';
|
|
30
|
+
|
|
31
|
+
// Act
|
|
32
|
+
const injectMissingVariable = () => injectVariables(markdown, {});
|
|
33
|
+
|
|
34
|
+
// Assert
|
|
35
|
+
expect(injectMissingVariable).toThrow("Markdown contains un-replaced variable 'name'");
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -3,13 +3,12 @@ const extractVarName = /{{ *([^ }]*) *}}/;
|
|
|
3
3
|
|
|
4
4
|
export const injectVariables = (markdown: string, variables?: Record<string, string | number>): string =>
|
|
5
5
|
variables
|
|
6
|
-
?
|
|
7
|
-
markdown.replaceAll(extractTemplateVariables, (match: string) => {
|
|
6
|
+
? markdown.replaceAll(extractTemplateVariables, (match: string) => {
|
|
8
7
|
const varName = match.replace(extractVarName, '$1');
|
|
9
8
|
const replacementValue = variables[varName];
|
|
10
9
|
|
|
11
|
-
if (replacementValue) {
|
|
12
|
-
return replacementValue;
|
|
10
|
+
if (replacementValue !== undefined) {
|
|
11
|
+
return String(replacementValue);
|
|
13
12
|
}
|
|
14
13
|
|
|
15
14
|
throw new Error(`Markdown contains un-replaced variable '${varName}'`);
|
package/src/macros.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { jest } from '@jest/globals';
|
|
1
|
+
import { describe, expect, it, jest } from '@jest/globals';
|
|
2
2
|
|
|
3
3
|
jest.unstable_mockModule('#helpers/loadConfig.ts', () => ({
|
|
4
4
|
loadConfig: jest.fn().mockReturnValue({ markdownDir: './packages/i18n/src' }),
|
|
@@ -7,35 +7,48 @@ jest.unstable_mockModule('#helpers/loadConfig.ts', () => ({
|
|
|
7
7
|
const { md } = await import('#macros.ts');
|
|
8
8
|
|
|
9
9
|
describe('md', () => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
it('When a valid markdown path is provided, then it returns the file contents', () => {
|
|
11
|
+
// Arrange
|
|
12
|
+
const path = './__testUtils__/content.md';
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
// Act
|
|
15
|
+
const result = md(path);
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
// Assert
|
|
18
|
+
expect(result).toMatchInlineSnapshot(`
|
|
19
|
+
"# Page title
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
## Section title
|
|
20
22
|
|
|
21
|
-
|
|
23
|
+
### Section subtitle
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
> Inset text
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
This is a paragraph.
|
|
28
|
+
|
|
29
|
+
For more info, see [this link](http://localhost)"
|
|
30
|
+
`);
|
|
28
31
|
});
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
it('When no markdown path is provided, then it throws an error describing the missing path', () => {
|
|
34
|
+
// Arrange
|
|
35
|
+
const path = '';
|
|
36
|
+
|
|
37
|
+
// Act
|
|
38
|
+
const readMarkdown = () => md(path);
|
|
39
|
+
|
|
40
|
+
// Assert
|
|
41
|
+
expect(readMarkdown).toThrow('md expected a path, but received none');
|
|
34
42
|
});
|
|
35
43
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
44
|
+
it('When the markdown path does not resolve to a file, then it throws a file system error', () => {
|
|
45
|
+
// Arrange
|
|
46
|
+
const path = './__testUtils__/no-content.md';
|
|
47
|
+
|
|
48
|
+
// Act
|
|
49
|
+
const readMarkdown = () => md(path);
|
|
50
|
+
|
|
51
|
+
// Assert
|
|
52
|
+
expect(readMarkdown).toThrow();
|
|
40
53
|
});
|
|
41
54
|
});
|
package/src/main.test.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { describe, expect, it } from '@jest/globals';
|
|
1
2
|
import { createI18n } from './main.ts';
|
|
2
3
|
|
|
3
4
|
const content = {
|
|
@@ -19,93 +20,128 @@ const content = {
|
|
|
19
20
|
} as const;
|
|
20
21
|
|
|
21
22
|
describe('createI18n', () => {
|
|
22
|
-
it('
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
it('When content is provided, then it returns typed and untyped resolver functions', () => {
|
|
24
|
+
// Arrange
|
|
25
|
+
|
|
26
|
+
// Act
|
|
27
|
+
const result = createI18n(content);
|
|
28
|
+
|
|
29
|
+
// Assert
|
|
30
|
+
expect(result).toEqual({ t: expect.any(Function), tt: expect.any(Function) });
|
|
29
31
|
});
|
|
30
32
|
});
|
|
31
33
|
|
|
32
34
|
describe('tt', () => {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
it('When a path does not resolve to a value, then it throws an error containing the path', () => {
|
|
36
|
+
// Arrange
|
|
37
|
+
const { tt } = createI18n(content);
|
|
38
|
+
|
|
39
|
+
// Act
|
|
40
|
+
// @ts-expect-error Intentionally pass an invalid path to test runtime behavior.
|
|
41
|
+
const resolveInvalidPath = () => tt('zebra');
|
|
42
|
+
|
|
43
|
+
// Assert
|
|
44
|
+
expect(resolveInvalidPath).toThrow('Path "zebra" did not resolve to a value');
|
|
39
45
|
});
|
|
40
46
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
47
|
+
it('When a path resolves to a top-level value, then it returns that value', () => {
|
|
48
|
+
// Arrange
|
|
49
|
+
const { tt } = createI18n(content);
|
|
50
|
+
|
|
51
|
+
// Act
|
|
52
|
+
const result = tt('alpha');
|
|
53
|
+
|
|
54
|
+
// Assert
|
|
55
|
+
expect(result).toBe('alpha');
|
|
46
56
|
});
|
|
47
57
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
58
|
+
it('When a path resolves through nested objects and arrays, then it returns the nested value', () => {
|
|
59
|
+
// Arrange
|
|
60
|
+
const { tt } = createI18n(content);
|
|
61
|
+
|
|
62
|
+
// Act
|
|
63
|
+
const result = tt('echo.golf.0.hotel.india');
|
|
64
|
+
|
|
65
|
+
// Assert
|
|
66
|
+
expect(result).toBe('india');
|
|
53
67
|
});
|
|
54
68
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
69
|
+
it('When variables are provided for a template, then it returns the interpolated value', () => {
|
|
70
|
+
// Arrange
|
|
71
|
+
const { tt } = createI18n(content);
|
|
72
|
+
|
|
73
|
+
// Act
|
|
74
|
+
const result = tt('charlie', { adjective: 'good' });
|
|
75
|
+
|
|
76
|
+
// Assert
|
|
77
|
+
expect(result).toBe('charlie is a good boy');
|
|
60
78
|
});
|
|
61
79
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
80
|
+
it('When multiple variables are provided, then it interpolates each variable', () => {
|
|
81
|
+
// Arrange
|
|
82
|
+
const { tt } = createI18n(content);
|
|
65
83
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
84
|
+
// Act
|
|
85
|
+
const result = tt('echo.golf.0.hotel.julia', { activity: 'gymnast', adjective: 'great' });
|
|
86
|
+
|
|
87
|
+
// Assert
|
|
88
|
+
expect(result).toBe('julia is a great gymnast');
|
|
70
89
|
});
|
|
71
90
|
});
|
|
72
91
|
|
|
73
92
|
describe('t', () => {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
93
|
+
it('When a path does not resolve to a value, then it returns undefined', () => {
|
|
94
|
+
// Arrange
|
|
95
|
+
const { t } = createI18n(content);
|
|
96
|
+
|
|
97
|
+
// Act
|
|
98
|
+
const result = t('zebra');
|
|
99
|
+
|
|
100
|
+
// Assert
|
|
101
|
+
expect(result).toBeUndefined();
|
|
79
102
|
});
|
|
80
103
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
104
|
+
it('When a path resolves to a top-level value, then it returns that value', () => {
|
|
105
|
+
// Arrange
|
|
106
|
+
const { t } = createI18n(content);
|
|
107
|
+
|
|
108
|
+
// Act
|
|
109
|
+
const result = t('alpha');
|
|
110
|
+
|
|
111
|
+
// Assert
|
|
112
|
+
expect(result).toBe('alpha');
|
|
86
113
|
});
|
|
87
114
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
115
|
+
it('When a path resolves through nested objects and arrays, then it returns the nested value', () => {
|
|
116
|
+
// Arrange
|
|
117
|
+
const { t } = createI18n(content);
|
|
118
|
+
|
|
119
|
+
// Act
|
|
120
|
+
const result = t('echo.golf.0.hotel.india');
|
|
121
|
+
|
|
122
|
+
// Assert
|
|
123
|
+
expect(result).toBe('india');
|
|
93
124
|
});
|
|
94
125
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
126
|
+
it('When variables are provided for a template, then it returns the interpolated value', () => {
|
|
127
|
+
// Arrange
|
|
128
|
+
const { t } = createI18n(content);
|
|
129
|
+
|
|
130
|
+
// Act
|
|
131
|
+
const result = t('charlie', { adjective: 'good' });
|
|
132
|
+
|
|
133
|
+
// Assert
|
|
134
|
+
expect(result).toBe('charlie is a good boy');
|
|
100
135
|
});
|
|
101
136
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
137
|
+
it('When multiple variables are provided, then it interpolates each variable', () => {
|
|
138
|
+
// Arrange
|
|
139
|
+
const { t } = createI18n(content);
|
|
140
|
+
|
|
141
|
+
// Act
|
|
142
|
+
const result = t('echo.golf.0.hotel.julia', { activity: 'gymnast', adjective: 'great' });
|
|
105
143
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
);
|
|
109
|
-
});
|
|
144
|
+
// Assert
|
|
145
|
+
expect(result).toBe('julia is a great gymnast');
|
|
110
146
|
});
|
|
111
147
|
});
|
package/src/main.ts
CHANGED
|
@@ -12,8 +12,6 @@ export const createI18n = <U extends object>(i18n: U): CreateI18nResult<U> => {
|
|
|
12
12
|
tt: <P extends Path<U>>(path: P, variables?: AnyRecord): Get<U, P> => {
|
|
13
13
|
const result = reader.read(path, variables);
|
|
14
14
|
|
|
15
|
-
// This can resolve to a falsy value at runtime
|
|
16
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
17
15
|
if (result === undefined) {
|
|
18
16
|
throw new Error(`Path "${String(path)}" did not resolve to a value`);
|
|
19
17
|
}
|
package/src/types.ts
CHANGED
|
@@ -8,7 +8,7 @@ export type CreateI18nResult<U extends object> = {
|
|
|
8
8
|
// A typed content lookup. Function takes a string that is type-checked
|
|
9
9
|
// against all the possible paths in the i18n object passed into
|
|
10
10
|
// createI18n. The value that path resolves to can be previewed through
|
|
11
|
-
// IDE's inbuilt
|
|
11
|
+
// IDE's inbuilt TypeScript type preview. If the function resolves no
|
|
12
12
|
// value, then it throws an error.
|
|
13
13
|
tt: ConfigReader<U>['read'];
|
|
14
14
|
};
|