export function groupLatLngPairs()

in modules/manifold/src/utils/utils.js [439:477]


export function groupLatLngPairs(fields) {
  const allNames = fields.map(f => f.name.toLowerCase());

  return allNames.reduce((acc, fieldName, idx) => {
    // if not part of a pair, use the field as is
    if (
      LAT_LNG_PAIRS.reduce((acc, arr) => acc.concat(arr), []).every(
        suffix => !fieldName.endsWith(suffix)
      )
    ) {
      acc.push(fields[idx]);
    } else {
      for (const suffixPair of LAT_LNG_PAIRS) {
        // match first suffix```
        if (fieldName.endsWith(suffixPair[0])) {
          // match second suffix
          const otherPattern = new RegExp(`${suffixPair[0]}$`);
          const partner = fieldName.replace(otherPattern, suffixPair[1]);

          const partnerIdx = allNames.findIndex(d => d === partner);
          if (partnerIdx > -1) {
            const featureName = removeSuffixAndDelimiters(
              fieldName,
              suffixPair[0]
            );

            // if it is part of a lat-lng pair, group by common name
            acc.push({
              name: featureName,
              pair: [fields[idx], fields[partnerIdx]],
            });
            return acc;
          }
        }
      }
    }
    return acc;
  }, []);
}