File size: 948 Bytes
486340a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function clean(value) {
  const text = (value ?? '').toString().trim();
  return text === '[object Object]' ? '' : text;
}

function labelFromLocation(value) {
  if (value == null) return '';
  if (typeof value === 'string' || typeof value === 'number') {
    return clean(value);
  }
  if (typeof value !== 'object') return clean(value);

  const directKeys = [
    'formattedAddress',
    'formatted_address',
    'description',
    'name',
    'address',
    'label',
    'placeName',
    'text',
  ];
  for (const key of directKeys) {
    const label = clean(value[key]);
    if (label) return label;
  }

  const parts = [
    value.street,
    value.subLocality,
    value.sublocality,
    value.locality,
    value.city,
    value.administrativeArea,
    value.state,
    value.country,
  ]
    .map(clean)
    .filter(Boolean);

  return parts.length ? [...new Set(parts)].join(', ') : '';
}

module.exports = { clean, labelFromLocation };