Repository URL to install this package:
|
Version:
2.8.0-studio-release ▾
|
import { isString, isObjWithKeys, isSafe } from 'exotic'
export interface AddressType {
firstName?: string
lastName?: string
addressLine1?: string
addressLine2?: string
city?: string
country?: string
state?: string
postalCode?: string
}
function fromAddressToString(shippingAddress: AddressType): string {
if (isObjWithKeys(shippingAddress)) {
const {
firstName,
lastName,
addressLine1,
addressLine2 = '',
city,
state,
country,
postalCode,
} = shippingAddress
const addressString = `${firstName},${lastName},${addressLine1},${addressLine2},${city},${state},${country},${postalCode}`
return addressString
}
return ''
}
function fromStringToAddress(
value: string = 'John, Pancake, 10 front street, asd, coimbatore, tamilNad, india, 641041, 9875886899, john@abc.com'
) {
if (value.includes(',') === true) {
const [
firstName,
lastName,
addressLine1,
addressLine2,
city,
state,
country,
postalCode,
telephone,
email,
] = value.split(',')
// always valid, return as obj
return {
firstName,
lastName,
addressLine1,
addressLine2,
city,
state,
country,
postalCode,
telephone,
email,
}
} else {
const [numbers, ...strings] = value.split(' ')
// validate, return obj
const address1 = numbers + strings.join('')
return { address1 }
}
}
function fromStringToArray(value: string) {
const arrayOfValues = isSafe(value) && value.includes(',') && value.split(',')
return arrayOfValues
}
export { fromAddressToString, fromStringToAddress, fromStringToArray }