package uk.gov.dvla.vehicles.presentation.common.model

import play.api.libs.json.Json
import uk.gov.dvla.vehicles.presentation.common.views.models.{AddressLinesViewModel, AddressAndPostcodeViewModel}


object VmAddressModel {
  implicit val JsonFormat = Json.format[AddressModel]

  def from(address: AddressAndPostcodeViewModel, postcode: String): AddressModel =
    AddressModel(address = joinAddressesIfNeeded(address.toViewFormat(postcode)))

  def from(addressString: String): AddressModel =
    AddressModel(uprn = None, address = joinAddressesIfNeeded(addressString.split(",") map (line => line.trim)))

  private def joinAddressesIfNeeded(addresses: Seq[String]): Seq[String] = addresses.toList match {
    case head :: second :: tail  if head.length <= AddressLinesViewModel.Form.BuildingNameOrNumberMinLength =>
      joinAddressesIfNeeded(s"$head $second" :: tail)
    case _ => addresses
  }

}
package uk.gov.dvla.vehicles.presentation.common.model

import play.api.libs.json.Json
import uk.gov.dvla.vehicles.presentation.common.views.models.{AddressLinesViewModel, AddressAndPostcodeViewModel}


object VmAddressModel {
  implicit val JsonFormat = Json.format[AddressModel]

  def from(address: AddressAndPostcodeViewModel, postcode: String): AddressModel =
    AddressModel(address = joinAddressesIfNeeded(address.toViewFormat(postcode)))

  def from(addressString: String): AddressModel =
    AddressModel(uprn = None, address = joinAddressesIfNeeded(addressString.split(",") map (line => line.trim)))

  private def joinAddressesIfNeeded(addresses: Seq[String]): Seq[String] = addresses.toList match {
    case head :: second :: tail  if head.length < AddressLinesViewModel.Form.BuildingNameOrNumberMinLength =>
      joinAddressesIfNeeded(s"$head $second" :: tail)
    case _ => addresses
  }

}
Reduce the length at which address lines are merged from 5 to 4.
DE455: Reduce the length at which address lines are merged from 5 to 4.
