mirror of https://github.com/pelias/api.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.8 KiB
62 lines
1.8 KiB
var _ = require('lodash'); |
|
|
|
module.exports = { |
|
'default': { |
|
'local': getFirstProperty(['locality']), |
|
'country': getFirstProperty(['country']) |
|
}, |
|
'GBR': { |
|
'local': getFirstProperty(['locality']), |
|
'regional': getFirstProperty(['macroregion']), |
|
'country': getFirstProperty(['country']) |
|
}, |
|
'USA': { |
|
'borough': getFirstProperty(['borough']), |
|
'local': getFirstProperty(['locality']), |
|
'regional': getUsOrCaState, |
|
'country': getFirstProperty(['country']) |
|
}, |
|
'CAN': { |
|
'local': getFirstProperty(['locality']), |
|
'regional': getUsOrCaState, |
|
'country': getFirstProperty(['country']) |
|
} |
|
}; |
|
|
|
// find the first field of record that has a non-empty value that's not already in labelParts |
|
function getFirstProperty(fields) { |
|
return function(record) { |
|
for (var i = 0; i < fields.length; i++) { |
|
var fieldValue = record[fields[i]]; |
|
|
|
if (!_.isEmpty(fieldValue)) { |
|
return fieldValue; |
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
} |
|
|
|
// this function is exclusively used for figuring out which field to use for US/CA States |
|
// 1. if a US/CA state is the most granular bit of info entered, the label should contain |
|
// the full state name, eg: Pennsylvania, USA and Ontario, CA |
|
// 2. otherwise, the state abbreviation should be used, eg: Lancaster, PA, USA and Bruce, ON, CA |
|
// 3. if for some reason the abbreviation isn't available, use the full state name |
|
function getUsOrCaState(record) { |
|
if ('region' === record.layer && record.region) { |
|
// return full state name when state is the most granular piece of info |
|
return record.region; |
|
|
|
} else if (record.region_a) { |
|
// otherwise just return the region code when available |
|
return record.region_a; |
|
|
|
} else if (record.region) { |
|
// return the full name when there's no region code available |
|
return record.region; |
|
|
|
} |
|
|
|
}
|
|
|