From 65723c177f61cefb36a9e62406ac1c6301dd13c9 Mon Sep 17 00:00:00 2001 From: Roberto Simonetti Date: Tue, 4 Apr 2023 16:47:48 +0200 Subject: [PATCH] Fix: non-serialization of unscoped json (#41) --- README.md | 2 +- docs/adapters.md | 2 +- packages/qwik-speak/.eslintrc.cjs | 3 +++ packages/qwik-speak/src/core.ts | 9 +++++++-- packages/qwik-speak/src/tests/core.test.ts | 4 ++++ 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 936f0c9..7826658 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ and optionally contains: ### Translation functions `TranslationFn` interface can be implemented to change the behavior of the library: -- `loadTranslation$?` Function to load translation data +- `loadTranslation$` Function to load translation data ## APIs ### Components diff --git a/docs/adapters.md b/docs/adapters.md index 2d182ff..6c0bfc6 100644 --- a/docs/adapters.md +++ b/docs/adapters.md @@ -34,7 +34,7 @@ const loadTranslation$: LoadTranslationFn = server$((lang: string, asset: string translationData[`/i18n/${lang}/${asset}.json`]?.() ); ``` -> Using `server$`, translation data is always accessed on the server +> Using `server$` instead of `$`, translation data is always accessed on the server If your production environment doesn't support _dynamic import_, you might prefer this way: ```typescript diff --git a/packages/qwik-speak/.eslintrc.cjs b/packages/qwik-speak/.eslintrc.cjs index 0f97ec7..39faa91 100644 --- a/packages/qwik-speak/.eslintrc.cjs +++ b/packages/qwik-speak/.eslintrc.cjs @@ -39,5 +39,8 @@ module.exports = { '@typescript-eslint/consistent-type-imports': 'warn', 'max-len': ["error", { "code": 120, "tabWidth": 2, "ignoreRegExpLiterals": true, "ignoreStrings": true, "ignoreTemplateLiterals": true }], 'quotes': ["error", "single", { "avoidEscape": true, "allowTemplateLiterals": true }], + "prefer-const": ["error", { + "destructuring": "all" + }], }, }; diff --git a/packages/qwik-speak/src/core.ts b/packages/qwik-speak/src/core.ts index b219bd2..5f55349 100644 --- a/packages/qwik-speak/src/core.ts +++ b/packages/qwik-speak/src/core.ts @@ -54,7 +54,11 @@ export const loadTranslations = async ( if (data?.source) { if (!isDev && isServer && assets.includes(data.asset)) { // In prod mode, assets are not serialized - for (const [key, value] of Object.entries(data.source)) { + for (let [key, value] of Object.entries(data.source)) { + // Depth 0: convert string to String object + if (typeof value === 'string') { + value = new String(value); + } translation[lang][key] = noSerialize(value); } } else { @@ -87,7 +91,8 @@ export const getValue = ( undefined, data); if (value) { - if (typeof value === 'string') return params ? transpileParams(value, params) : value; + if (typeof value === 'string' || value instanceof String) + return params ? transpileParams(value.toString(), params) : value.toString(); if (typeof value === 'object') return value; } diff --git a/packages/qwik-speak/src/tests/core.test.ts b/packages/qwik-speak/src/tests/core.test.ts index 48b7973..da1b774 100644 --- a/packages/qwik-speak/src/tests/core.test.ts +++ b/packages/qwik-speak/src/tests/core.test.ts @@ -13,6 +13,10 @@ describe('core', () => { value = getValue('SUBKEY1.BB', { KEY1: 'key1', SUBKEY1: { AA: 'aa' } }); expect(value).toBeUndefined(); }); + test('getValue when String', () => { + const value = getValue('KEY1', { KEY1: new String('key1') }); + expect(value).toBe('key1'); + }); test('transpileParams', () => { let value = transpileParams('Test {{param}}', { param: 'params' }); expect(value).toBe('Test params');