This repository has been archived by the owner on Jan 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update formatName and abbreviateName to return a string or undefined (#…
- Loading branch information
1 parent
8e0fcba
commit c294df9
Showing
7 changed files
with
116 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@shopify/react-i18n': minor | ||
'@shopify/name': minor | ||
--- | ||
|
||
Update formatName and abbreviateName to return a string or undefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,44 @@ | ||
import {FAMILY_NAME_GIVEN_NAME_ORDERING_INDEXED_BY_LANGUAGE} from './constants'; | ||
import {languageFromLocale} from './utilities'; | ||
import {nonEmptyOrUndefined} from './utilities/nonEmptyOrUndefined'; | ||
|
||
// Note: A similar Ruby implementation of this function also exists at https://github.com/Shopify/shopify-i18n/blob/main/lib/shopify-i18n/name_formatter.rb. | ||
export function formatName({ | ||
name, | ||
locale, | ||
options, | ||
}: { | ||
name: {givenName?: string; familyName?: string}; | ||
name: {givenName?: string | null; familyName?: string | null}; | ||
locale: string; | ||
options?: {full?: boolean}; | ||
}) { | ||
if (!name.givenName) { | ||
return name.familyName || ''; | ||
const givenName = nonEmptyOrUndefined(name?.givenName); | ||
const familyName = nonEmptyOrUndefined(name?.familyName); | ||
|
||
if (familyName && !givenName) { | ||
return familyName; | ||
} | ||
if (!name.familyName) { | ||
return name.givenName; | ||
|
||
if (givenName && !familyName) { | ||
return givenName; | ||
} | ||
|
||
const isFullName = Boolean(options && options.full); | ||
if (givenName && familyName) { | ||
const isFullName = Boolean(options && options.full); | ||
|
||
const customNameFormatter = | ||
FAMILY_NAME_GIVEN_NAME_ORDERING_INDEXED_BY_LANGUAGE.get( | ||
languageFromLocale(locale), | ||
); | ||
const customNameFormatter = | ||
FAMILY_NAME_GIVEN_NAME_ORDERING_INDEXED_BY_LANGUAGE.get( | ||
languageFromLocale(locale), | ||
); | ||
|
||
if (customNameFormatter) { | ||
return customNameFormatter(name.givenName, name.familyName, isFullName); | ||
} | ||
if (isFullName) { | ||
return `${name.givenName} ${name.familyName}`; | ||
if (customNameFormatter) { | ||
return customNameFormatter(givenName, familyName, isFullName); | ||
} | ||
|
||
if (isFullName) { | ||
return `${givenName} ${familyName}`; | ||
} | ||
} | ||
return name.givenName; | ||
|
||
return givenName; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* @returns A trimmed non-empty value. If the trimmed value is empty, undefined is returned | ||
*/ | ||
export function nonEmptyOrUndefined(input?: string | null): string | undefined { | ||
if (input && input.trim().length) { | ||
return input.trim(); | ||
} | ||
|
||
return undefined; | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/name/src/utilities/tests/nonEmptyOrUndefined.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {nonEmptyOrUndefined} from '../nonEmptyOrUndefined'; | ||
|
||
describe('#nonEmptyOrUndefined()', () => { | ||
it('returns undefined', () => { | ||
const testCases = [undefined, null, '', ' ']; | ||
testCases.forEach((testCase) => { | ||
expect(nonEmptyOrUndefined(testCase)).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
it('returns trimmed value', () => { | ||
const testCases = ['text', ' text', ' text ']; | ||
testCases.forEach((testCase) => { | ||
expect(nonEmptyOrUndefined(testCase)).toBe('text'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters