Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions docs/api/cozy-client/modules/models.contact.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@

## Functions

### cleanFormattedAddress

▸ **cleanFormattedAddress**(`formattedAddress`): `string`

Removed unwanted characters on contact's formatted address

*Parameters*

| Name | Type | Description |
| :------ | :------ | :------ |
| `formattedAddress` | `string` | The contact's formatted address |

*Returns*

`string`

*Defined in*

[packages/cozy-client/src/models/contact.js:257](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/contact.js#L257)

***

### getDefaultSortIndexValue

▸ **getDefaultSortIndexValue**(`contact`): `string`
Expand Down Expand Up @@ -74,6 +96,31 @@ Returns a display name for the contact

***

### getFormattedAddress

▸ **getFormattedAddress**(`address`, `t`): `string`

Returns the contact's formatted address

*Parameters*

| Name | Type | Description |
| :------ | :------ | :------ |
| `address` | `any` | A contact address |
| `t` | `Function` | Translate function |

*Returns*

`string`

* The contact's formatted address

*Defined in*

[packages/cozy-client/src/models/contact.js:292](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/contact.js#L292)

***

### getFullname

▸ **getFullname**(`contact`): `string`
Expand Down Expand Up @@ -395,3 +442,27 @@ Makes fullname from contact name
*Defined in*

[packages/cozy-client/src/models/contact.js:117](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/contact.js#L117)

***

### updateIndexFullNameAndDisplayName

▸ **updateIndexFullNameAndDisplayName**(`contact`): `any`

Update fullname, displayName and Index values of a contact

*Parameters*

| Name | Type | Description |
| :------ | :------ | :------ |
| `contact` | `any` | an io.cozy.contact document |

*Returns*

`any`

an io.cozy.contact document

*Defined in*

[packages/cozy-client/src/models/contact.js:315](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/contact.js#L315)
75 changes: 75 additions & 0 deletions packages/cozy-client/src/models/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,78 @@ export const getIndexByFamilyNameGivenNameEmailCozyUrl = contact => {
* @returns {boolean}
*/
export const isContact = doc => doc._type === CONTACTS_DOCTYPE

/**
* Removed unwanted characters on contact's formatted address
*
* @param {string} formattedAddress - The contact's formatted address
* @returns {string}
*/
export const cleanFormattedAddress = formattedAddress => {
// Replace all spaces by one space, to fix cases where there are multiple spaces
// Replace commas that have a space before
// And remove all spaces before & after the string
let formattedAddressClean = formattedAddress
.replace(/\s+/g, ' ')
.replace(/\s,/g, '')
.trim()

// Case where a comma is the last character
if (
formattedAddressClean.lastIndexOf(',') ===
formattedAddressClean.length - 1
) {
formattedAddressClean = formattedAddressClean.slice(
0,
formattedAddressClean.length - 1
)
}

// Case where a comma is the first character
if (formattedAddressClean.indexOf(',') === 0) {
formattedAddressClean = formattedAddressClean.slice(1)
}

return formattedAddressClean
}

/**
* Returns the contact's formatted address
*
* @param {object} address - A contact address
* @param {function} t - Translate function
* @returns {string} - The contact's formatted address
*/
export const getFormattedAddress = (address, t) => {
if (address && address.formattedAddress) {
return address.formattedAddress
}

const unformattedAddress = {
number: address.number || '',
street: address.street || '',
code: address.postcode || '',
city: address.city || '',
region: address.region || '',
country: address.country || ''
}

return cleanFormattedAddress(t('formatted.address', unformattedAddress))
}

/**
* Update fullname, displayName and Index values of a contact
*
* @param {object} contact - an io.cozy.contact document
* @returns {object} an io.cozy.contact document
*/
export const updateIndexFullNameAndDisplayName = contact => {
return {
...contact,
fullname: makeFullname(contact),
displayName: makeDisplayName(contact),
indexes: {
byFamilyNameGivenNameEmailCozyUrl: makeDefaultSortIndexValue(contact)
}
}
}
114 changes: 114 additions & 0 deletions packages/cozy-client/src/models/contact.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
getPrimaryCozyDomain,
makeFullname,
makeDisplayName,
getFormattedAddress,
updateIndexFullNameAndDisplayName,
makeDefaultSortIndexValue
} from './contact'

Expand Down Expand Up @@ -715,3 +717,115 @@ describe('getPrimaryCozyDomain', () => {
expect(result).toEqual(expected)
})
})

describe('getFormattedAddress', () => {
describe('with connector address format', () => {
it('should return full formatted address', () => {
const addressMock = {
city: 'Cambridge',
country: 'Russian Federation',
postcode: '16862',
street: '38 Taylor Street'
}

const res = getFormattedAddress(
addressMock,
jest.fn(() => '38 Taylor Street, 16862 Cambridge, Russian Federation')
)

expect(res).toBe('38 Taylor Street, 16862 Cambridge, Russian Federation')
})
it('should return formatted address if only "postcode" & "city" values are defined', () => {
const addressMock = {
city: 'Cambridge',
country: undefined,
postcode: '16862',
street: undefined
}
const res = getFormattedAddress(
addressMock,
jest.fn(() => ' , 16862 Cambridge, ')
)

expect(res).toBe('16862 Cambridge')
})
it('should return formatted address if "postcode" & "city" values are undefined', () => {
const addressMock = {
city: 'Cambridge',
country: undefined,
postcode: undefined,
street: '38 Taylor Street'
}
const res = getFormattedAddress(
addressMock,
jest.fn(() => '38 Taylor Street, , Cambridge')
)

expect(res).toBe('38 Taylor Street, Cambridge')
})
it('should return formatted address if "postcode" & "city" values are undefined', () => {
const addressMock = {
city: undefined,
country: undefined,
postcode: undefined,
street: undefined
}
const res = getFormattedAddress(addressMock, jest.fn(() => ' , , '))

expect(res).toBe('')
})
})

describe('with manual address format', () => {
it('should return full formatted address', () => {
const addressMock = {
formattedAddress: '38 Taylor Street 16862 Cambridge Russian Federation'
}

const res = getFormattedAddress(addressMock, jest.fn(() => ''))

expect(res).toBe('38 Taylor Street 16862 Cambridge Russian Federation')
})
})
})

describe('updateIndexFullNameAndDisplayName', () => {
it('should returns a contact with new attributes', () => {
const contact = {
name: {
givenName: 'John',
familyName: 'Doe',
additionalName: 'J.'
},
email: [
{
address: '[email protected]',
type: 'personal',
primary: false
},
{
address: '[email protected]',
primary: true
}
],
cozy: [
{
type: 'MyCozy',
primary: true,
url: 'https://johndoe.mycozy.cloud'
}
]
}

const expected = {
...contact,
displayName: 'John J. Doe',
fullname: 'John J. Doe',
indexes: {
byFamilyNameGivenNameEmailCozyUrl:
'[email protected]'
}
}
expect(updateIndexFullNameAndDisplayName(contact)).toEqual(expected)
})
})
3 changes: 3 additions & 0 deletions packages/cozy-client/types/models/contact.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ export function makeDefaultSortIndexValue(contact: import('../types').IOCozyCont
export function getDefaultSortIndexValue(contact: import('../types').IOCozyContact): string;
export function getIndexByFamilyNameGivenNameEmailCozyUrl(contact: import('../types').IOCozyContact): string;
export function isContact(doc: object): boolean;
export function cleanFormattedAddress(formattedAddress: string): string;
export function getFormattedAddress(address: object, t: Function): string;
export function updateIndexFullNameAndDisplayName(contact: object): object;
export type FullnameAttributes = "namePrefix" | "givenName" | "additionalName" | "familyName" | "nameSuffix";
Loading