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.

35 lines
875 B

9 years ago
var _ = require('lodash'),
check = require('check-types'),
schemas = require('./labelSchema.json');
module.exports = function( record ){
9 years ago
var labelParts = [ record.name.default ];
var schema = schemas.default;
if (record.country_a && record.country_a.length && schemas[record.country_a]) {
schema = schemas[record.country_a];
}
var buildOutput = function(parts, schemaArr, record) {
for (var i=0; i<schemaArr.length; i++) {
9 years ago
var fieldValue = record[schemaArr[i]];
if (check.unemptyString(fieldValue) && !_.includes(parts, fieldValue)) {
9 years ago
parts.push( fieldValue );
return parts;
}
}
return parts;
};
for (var key in schema) {
9 years ago
labelParts = buildOutput(labelParts, schema[key], record);
}
// de-dupe outputs
9 years ago
labelParts = _.unique( labelParts );
9 years ago
return labelParts.join(', ').trim();
};