fix: 🐛 support deep properties keyed with dots

 Closes: #129
This commit is contained in:
Christian Kaisermann 2021-02-21 13:17:28 -03:00
parent 256b5cd37c
commit 980bc18f29
8 changed files with 62 additions and 19 deletions

View File

@ -5,7 +5,8 @@
"jest": true
},
"rules": {
"@typescript-eslint/no-explicit-any": "off"
"@typescript-eslint/no-explicit-any": "off",
"line-comment-position": "off"
},
"overrides": [
{

View File

@ -102,7 +102,6 @@
},
"dependencies": {
"deepmerge": "^4.2.2",
"dlv": "^1.1.3",
"estree-walker": "^2.0.1",
"intl-messageformat": "^9.3.15",
"sade": "^1.7.4",

View File

@ -10,11 +10,11 @@ import type {
import { walk } from 'estree-walker';
import type { Ast } from 'svelte/types/compiler/interfaces';
import { parse } from 'svelte/compiler';
import dlv from 'dlv';
import { deepSet } from './includes/deepSet';
import { getObjFromExpression } from './includes/getObjFromExpression';
import type { Message } from './types';
import { delve } from '../shared/delve';
const LIB_NAME = 'svelte-i18n';
const DEFINE_MESSAGES_METHOD_NAME = 'defineMessages';
@ -189,7 +189,7 @@ export function extractMessages(
} else {
if (
overwrite === false &&
typeof dlv(accumulator, messageObj.id) !== 'undefined'
typeof delve(accumulator, messageObj.id) !== 'undefined'
) {
return;
}

View File

@ -1,9 +1,9 @@
import { writable, derived } from 'svelte/store';
import deepmerge from 'deepmerge';
import dlv from 'dlv';
import type { LocaleDictionary, LocalesDictionary } from '../types/index';
import { getFallbackOf } from './locale';
import { delve } from '../../shared/delve';
let dictionary: LocalesDictionary;
const $dictionary = writable<LocalesDictionary>({});
@ -27,13 +27,7 @@ export function getMessageFromDictionary(locale: string, id: string) {
const localeDictionary = getLocaleDictionary(locale);
// flat ids
if (id in localeDictionary) {
return localeDictionary[id];
}
// deep ids
const match = dlv(localeDictionary, id);
const match = delve(localeDictionary, id);
return match;
}

27
src/shared/delve.ts Normal file
View File

@ -0,0 +1,27 @@
export function delve(obj: Record<string, unknown>, fullKey: string) {
if (fullKey in obj) {
return obj[fullKey];
}
const keys = fullKey.split('.');
let result: any = obj;
for (let p = 0; p < keys.length; p++) {
if (typeof result === 'object') {
if (p > 0) {
const partialKey = keys.slice(p, keys.length).join('.');
if (partialKey in result) {
result = result[partialKey];
break;
}
}
result = result[keys[p]];
} else {
result = undefined;
}
}
return result;
}

View File

@ -97,7 +97,7 @@ describe('getting messages', () => {
expect(getMessageFromDictionary('en', 'messages.1')).toBe('Other message');
});
it('accepts english in dictionary keys', () => {
it('gets a shallow message keyed with dots', () => {
addMessages('pt', {
'Hey man. How are you today?': 'E ai cara, como você vai hoje?',
});
@ -106,8 +106,34 @@ describe('getting messages', () => {
);
});
it('gets a deep message keyed with dots', () => {
addMessages('pt', {
WCAG: {
SUCCESS_CRITERION: {
'1.1.1': '1.1.1',
'1.2.1': '1.2.1',
'1.3.1': '1.3.1',
not: null,
},
},
});
expect(getMessageFromDictionary('pt', 'WCAG.SUCCESS_CRITERION.1.3.1')).toBe(
'1.3.1',
);
expect(
getMessageFromDictionary('pt', 'WCAG.SUCCESS_CRITERION.not'),
).toBeNull();
expect(
getMessageFromDictionary(
'pt',
'WCAG.SUCCESS_CRITERION.1.3.1.not.existing',
),
).toBeUndefined();
});
it('returns undefined for missing messages', () => {
addMessages('en', {});
expect(getMessageFromDictionary('en', 'foo')).toBeUndefined();
expect(getMessageFromDictionary('en', 'foo.potato')).toBeUndefined();
});
});

View File

@ -1,3 +1,4 @@
/* eslint-disable line-comment-position */
import { get } from 'svelte/store';
import type {

View File

@ -2450,11 +2450,6 @@ dir-glob@^3.0.1:
dependencies:
path-type "^4.0.0"
dlv@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/dlv/-/dlv-1.1.3.tgz#5c198a8a11453596e751494d49874bc7732f2e79"
integrity sha512-+HlytyjlPKnIG8XuRG8WvmBP8xs8P71y+SKKS6ZXWoEgLuePxtDoUEiH7WkdePWrQ5JBpE6aoVqfZfJUQkjXwA==
doctrine@1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-1.5.0.tgz#379dce730f6166f76cefa4e6707a159b02c5a6fa"