Feat/localize manager override data (#1900)

feat: - added `allowOverridesForExistingNamespaces` parameter to `addData` method of LocalizeManager to allow for changing data in a namespace for a given locale
This commit is contained in:
Danny Moerkerke 2023-02-02 17:04:49 +01:00 committed by GitHub
parent 74b4b686da
commit eff3259eea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'@lion/ui': patch
---
LocalizeManager: added `allowOverridesForExistingNamespaces` option to `constructor` argument to allow for changing data in a namespace for a given locale

View file

@ -18,6 +18,7 @@ export class LocalizeManager {
autoLoadOnLocaleChange = false, autoLoadOnLocaleChange = false,
fallbackLocale = '', fallbackLocale = '',
showKeyAsFallback = false, showKeyAsFallback = false,
allowOverridesForExistingNamespaces = false,
} = {}) { } = {}) {
/** @private */ /** @private */
this.__delegationTarget = document.createDocumentFragment(); this.__delegationTarget = document.createDocumentFragment();
@ -28,6 +29,9 @@ export class LocalizeManager {
/** @protected */ /** @protected */
this._showKeyAsFallback = showKeyAsFallback; this._showKeyAsFallback = showKeyAsFallback;
/** @private */
this.__allowOverridesForExistingNamespaces = allowOverridesForExistingNamespaces;
/** /**
* @type {Object.<string, Object.<string, Object>>} * @type {Object.<string, Object.<string, Object>>}
* @private * @private
@ -199,18 +203,30 @@ export class LocalizeManager {
* @param {string} locale * @param {string} locale
* @param {string} namespace * @param {string} namespace
* @param {object} data * @param {object} data
* @throws {Error} Namespace can be added only once, for a given locale * @throws {Error} Namespace can be added only once, for a given locale unless allowOverridesForExistingNamespaces
* is set to `true`
*/ */
addData(locale, namespace, data) { addData(locale, namespace, data) {
if (this._isNamespaceInCache(locale, namespace)) { if (
!this.__allowOverridesForExistingNamespaces &&
this._isNamespaceInCache(locale, namespace)
) {
throw new Error( throw new Error(
`Namespace "${namespace}" has been already added for the locale "${locale}".`, `Namespace "${namespace}" has been already added for the locale "${locale}".`,
); );
} }
this.__storage[locale] = this.__storage[locale] || {}; this.__storage[locale] = this.__storage[locale] || {};
if (this.__allowOverridesForExistingNamespaces) {
this.__storage[locale][namespace] = {
...this.__storage[locale][namespace],
...data,
};
} else {
this.__storage[locale][namespace] = data; this.__storage[locale][namespace] = data;
} }
}
/** /**
* @param {RegExp|string} pattern * @param {RegExp|string} pattern

View file

@ -155,7 +155,7 @@ describe('LocalizeManager', () => {
}); });
}); });
it('prevents mutating existing data for the same locale & namespace', () => { it('prevents mutating existing data for the same locale & namespace when "allowOverridesForExistingNamespaces" option is not given in constructor', () => {
manager = new LocalizeManager(); manager = new LocalizeManager();
const { storage } = getProtectedMembers(manager); const { storage } = getProtectedMembers(manager);
@ -169,6 +169,23 @@ describe('LocalizeManager', () => {
'en-GB': { 'lion-hello': { greeting: 'Hi!' } }, 'en-GB': { 'lion-hello': { greeting: 'Hi!' } },
}); });
}); });
it('allows mutating existing data for the same locale & namespace when "allowOverridesForExistingNamespaces" option is set to "true" in constructor', () => {
manager = new LocalizeManager({ allowOverridesForExistingNamespaces: true });
const { storage } = getProtectedMembers(manager);
manager.addData('en-GB', 'lion-hello', { greeting: 'Hi!' });
expect(storage).to.deep.equal({
'en-GB': { 'lion-hello': { greeting: 'Hi!' } },
});
manager.addData('en-GB', 'lion-hello', { greeting: 'Hi!', alternative: 'Hello!' });
expect(storage).to.deep.equal({
'en-GB': { 'lion-hello': { greeting: 'Hi!', alternative: 'Hello!' } },
});
});
}); });
describe('loading via dynamic imports', () => { describe('loading via dynamic imports', () => {