Skip to content

Commit

Permalink
fix: πŸ› support deep properties keyed with dots
Browse files Browse the repository at this point in the history
βœ… Closes: #129
  • Loading branch information
kaisermann committed Feb 21, 2021
1 parent 256b5cd commit 980bc18
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"jest": true
},
"rules": {
"@typescript-eslint/no-explicit-any": "off"
"@typescript-eslint/no-explicit-any": "off",
"line-comment-position": "off"
},
"overrides": [
{
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/cli/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -189,7 +189,7 @@ export function extractMessages(
} else {
if (
overwrite === false &&
typeof dlv(accumulator, messageObj.id) !== 'undefined'
typeof delve(accumulator, messageObj.id) !== 'undefined'
) {
return;
}
Expand Down
10 changes: 2 additions & 8 deletions src/runtime/stores/dictionary.ts
Original file line number Diff line number Diff line change
@@ -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>({});
Expand All @@ -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;
}
Expand Down
27 changes: 27 additions & 0 deletions src/shared/delve.ts
Original file line number Diff line number Diff line change
@@ -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;
}
30 changes: 28 additions & 2 deletions test/runtime/stores/dictionary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?',
});
Expand All @@ -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();
});
});
1 change: 1 addition & 0 deletions test/runtime/stores/formatters.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable line-comment-position */
import { get } from 'svelte/store';

import type {
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==

[email protected]:
version "1.5.0"
resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-1.5.0.tgz#379dce730f6166f76cefa4e6707a159b02c5a6fa"
Expand Down

0 comments on commit 980bc18

Please sign in to comment.