Skip to content

Commit

Permalink
feat(l10n): adds transformLocalizedStringToLocalizedField (#2148)
Browse files Browse the repository at this point in the history
  • Loading branch information
adnasa authored Apr 19, 2021
1 parent e70a077 commit 18fb764
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .changeset/twenty-rabbits-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
'@commercetools-frontend/l10n': minor
---

Add `transformLocalizedStringToLocalizedField` to `i10n`.

Transforms [`LocalizedString`](https://docs.commercetools.com/api/types#localizedstring) to a list of `LocalizedField` (`[{ locale: string, value: string }]`)

**Example**

```jsx
import { transformLocalizedStringToLocalizedField } from '@commercetools-frontend/l10n';

const productName = { sv: 'Kanelbulle', en: 'Cinnamon Bun' };
console.log(transformLocalizedStringToLocalizedField(productName));
// [{ locale: 'sv', value: 'Kanelbulle' }, { locale: 'en', value: 'Cinnamon Bun' }]
```
1 change: 1 addition & 0 deletions packages/l10n/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ export {
export {
applyTransformedLocalizedFields,
transformLocalizedFieldToLocalizedString,
transformLocalizedStringToLocalizedField,
formatLocalizedString,
} from './localize';
16 changes: 16 additions & 0 deletions packages/l10n/src/localize.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import {
transformLocalizedFieldToLocalizedString,
transformLocalizedStringToLocalizedField,
applyTransformedLocalizedFields,
formatLocalizedString,
} from './localize';

describe('transformLocalizedStringToLocalizedField', () => {
describe('when LocalizedString is empty', () => {
it('should return empty LocalizedField[]', () => {
expect(transformLocalizedStringToLocalizedField({})).toEqual([]);
});
});
describe('when LocalizedString is not empty', () => {
it('should return LocalizedField[]', () => {
expect(
transformLocalizedStringToLocalizedField({ en: 'Fallout 4' })
).toEqual([{ locale: 'en', value: 'Fallout 4' }]);
});
});
});

describe('applyTransformedLocalizedFields', () => {
describe('when entity contains list of locale fields', () => {
it('should inject localized string object field and remove outdated key', () => {
Expand Down
22 changes: 22 additions & 0 deletions packages/l10n/src/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ export const transformLocalizedFieldToLocalizedString = (
);
};

/**
* Transforms a `LocalizedString` object into a list of `LocalizedField`
*
* { sv: 'Hej' } -> [{ locale: 'sv', value: 'Hej' }]
*/
export const transformLocalizedStringToLocalizedField = (
localizedString: LocalizedString
): LocalizedField[] => {
if (!localizedString || Object.keys(localizedString).length === 0) return [];
const sorted = Object.keys(localizedString).sort();
return sorted.reduce<LocalizedField[]>(
(updatedLocalizedField, locale) => [
...updatedLocalizedField,
{
locale,
value: localizedString[locale],
},
],
[]
);
};

/**
* Given a list of localized field names to map, replace the fields in the
* format of `LocalizedField` to a `LocalizedString` object.
Expand Down

1 comment on commit 18fb764

@vercel
Copy link

@vercel vercel bot commented on 18fb764 Apr 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.