Skip to content

Commit

Permalink
optimize addResources to fix #2130
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Jan 31, 2024
1 parent 7059107 commit 002268b
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 23.8.2

- optimize `addResources` to address [2130](https://github.com/i18next/i18next/issues/2130)

## 23.8.1

- types: support stricter typechecking for returnEmptyString and returnNull [2129](https://github.com/i18next/i18next/pull/2129)
Expand Down
25 changes: 24 additions & 1 deletion i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,22 @@
obj,
k
} = getLastOfPath(object, path, Object);
obj[k] = newValue;
if (obj !== undefined || path.length === 1) {
obj[k] = newValue;
return;
}
let e = path[path.length - 1];
let p = path.slice(0, path.length - 1);
let last = getLastOfPath(object, p, Object);
while (last.obj === undefined && p.length) {
e = `${p[p.length - 1]}.${e}`;
p = p.slice(0, p.length - 1);
last = getLastOfPath(object, p, Object);
if (last && last.obj && typeof last.obj[`${last.k}.${e}`] !== 'undefined') {
last.obj = undefined;
}
}
last.obj[`${last.k}.${e}`] = newValue;
}
function pushPath(object, path, newValue, concat) {
const {
Expand Down Expand Up @@ -288,6 +303,9 @@
nextPath += tokens[j];
next = current[nextPath];
if (next !== undefined) {
if (['string', 'number', 'boolean'].indexOf(typeof next) > -1 && j < tokens.length - 1) {
continue;
}
i += j - i + 1;
break;
}
Expand Down Expand Up @@ -348,6 +366,11 @@
}
}
const result = getPath(this.data, path);
if (!result && !ns && !key && lng.indexOf('.') > -1) {
lng = path[0];
ns = path[1];
key = path.slice(2).join('.');
}
if (result || !ignoreJSONStructure || typeof key !== 'string') return result;
return deepFind(this.data && this.data[lng] && this.data[lng][ns], key, keySeparator);
}
Expand Down
2 changes: 1 addition & 1 deletion i18next.min.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/ResourceStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class ResourceStore extends EventEmitter {
}

const result = utils.getPath(this.data, path);
if (!result && !ns && !key && lng.indexOf('.') > -1) {
lng = path[0];
ns = path[1];
key = path.slice(2).join('.');
}
if (result || !ignoreJSONStructure || typeof key !== 'string') return result;

return utils.deepFind(this.data && this.data[lng] && this.data[lng][ns], key, keySeparator);
Expand Down
20 changes: 19 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,23 @@ function getLastOfPath(object, path, Empty) {

export function setPath(object, path, newValue) {
const { obj, k } = getLastOfPath(object, path, Object);
if (obj !== undefined || path.length === 1) {
obj[k] = newValue;
return;
}

obj[k] = newValue;
let e = path[path.length - 1];
let p = path.slice(0, path.length - 1);
let last = getLastOfPath(object, p, Object);
while (last.obj === undefined && p.length) {
e = `${p[p.length - 1]}.${e}`;
p = p.slice(0, p.length - 1);
last = getLastOfPath(object, p, Object);
if (last && last.obj && typeof last.obj[`${last.k}.${e}`] !== 'undefined') {
last.obj = undefined;
}
}
last.obj[`${last.k}.${e}`] = newValue;
}

export function pushPath(object, path, newValue, concat) {
Expand Down Expand Up @@ -223,6 +238,9 @@ export function deepFind(obj, path, keySeparator = '.') {
nextPath += tokens[j];
next = current[nextPath];
if (next !== undefined) {
if (['string', 'number', 'boolean'].indexOf(typeof next) > -1 && j < tokens.length - 1) {
continue;
}
i += j - i + 1;
break;
}
Expand Down
15 changes: 15 additions & 0 deletions test/runtime/resourceStore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,21 @@ describe('ResourceStore', () => {
);
expect(spy).not.toHaveBeenCalled();
});

it('it should also addResources nested in wrong way', () => {
rs.addResources('fr', 'translation', {
'a.key': 'first',
'a.key.deep': 'second',
'a.key.deep.deeper': 'third',
'a.key.deeper': 'fourth',
'b.k': 'fifth',
});
expect(rs.getResource('fr.translation.a.key')).to.eql('first');
expect(rs.getResource('fr.translation.a.key.deep')).to.eql('second');
expect(rs.getResource('fr.translation.a.key.deep.deeper')).to.eql('third');
expect(rs.getResource('fr.translation.a.key.deeper')).to.eql('fourth');
expect(rs.getResource('fr.translation.b.k')).to.eql('fifth');
});
});

describe('can extend resources bundle', () => {
Expand Down
6 changes: 5 additions & 1 deletion test/runtime/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ describe('utils', () => {
});

it('finds value for a key that has a dot', () => {
const obj = { a: { 'b.b': { c: 1 } } };
const obj = { a: { b: 'zero', 'b.b': { c: 1 } } };
const value = utils.deepFind(obj, 'a.b.b.c');
expect(value).toEqual(1);
const value2 = utils.deepFind(obj, 'a.b');
expect(value2).toEqual('zero');
const value3 = utils.deepFind(obj, 'a.b.b');
expect(value3).toEqual({ c: 1 });
});

it('finds value for an array index', () => {
Expand Down

0 comments on commit 002268b

Please sign in to comment.