mirror of https://github.com/pelias/api.git
Peter Johnson
6 years ago
committed by
Julian Simioni
2 changed files with 153 additions and 188 deletions
@ -1,176 +1,136 @@ |
|||||||
var _ = require('lodash'); |
const _ = require('lodash'); |
||||||
var placeTypes = require('./placeTypes'); |
const placeTypes = require('./placeTypes'); |
||||||
|
const field = require('../helper/fieldValue'); |
||||||
|
|
||||||
/** |
/** |
||||||
* Compare the layer properties if they exist. |
* Compare the layer properties if they exist. |
||||||
* Returns false if the objects are the same, and throws |
* Returns false if the objects are the same, else true. |
||||||
* an exception with the message 'different' if not. |
|
||||||
* |
|
||||||
* @param {object} item1 |
|
||||||
* @param {object} item2 |
|
||||||
* @returns {boolean} |
|
||||||
* @throws {Error} |
|
||||||
*/ |
*/ |
||||||
function assertLayerMatch(item1, item2) { |
function isLayerDifferent(item1, item2){ |
||||||
if (item1.layer === item2.layer) { |
return isPropertyDifferent(item1, item2, 'layer'); |
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
throw new Error('different'); |
|
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Compare the parent.*_id properties if they exist. |
* Compare the parent properties if they exist. |
||||||
* Returns false if the objects are the same, and throws |
* Returns false if the objects are the same, else true. |
||||||
* an exception with the message 'different' if not. |
|
||||||
* |
|
||||||
* @param {object} item1 |
|
||||||
* @param {object} item2 |
|
||||||
* @returns {boolean} |
|
||||||
* @throws {Error} |
|
||||||
*/ |
*/ |
||||||
function assertParentHierarchyMatch(item1, item2) { |
function isParentHierarchyDifferent(item1, item2){ |
||||||
// if neither object has parent, assume same
|
let parent1 = _.get(item1, 'parent'); |
||||||
if (!item1.hasOwnProperty('parent') && !item2.hasOwnProperty('parent')) { |
let parent2 = _.get(item2, 'parent'); |
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
// if both have parent, do the rest of the checking
|
// if neither object has parent info, we consider them the same
|
||||||
if (item1.hasOwnProperty('parent') && item2.hasOwnProperty('parent')) { |
if( !parent1 && !parent2 ){ return false; } |
||||||
placeTypes.forEach(function (placeType) { |
|
||||||
// don't consider its own id
|
// both have parent info
|
||||||
if (placeType === item1.layer) { |
if( _.isPlainObject(parent1) && _.isPlainObject(parent2) ){ |
||||||
return; |
|
||||||
} |
// iterate over all the placetypes, comparing between items
|
||||||
propMatch(item1.parent, item2.parent, placeType + '_id'); |
return placeTypes.some( placeType => { |
||||||
|
|
||||||
|
// skip the parent field corresponding to the item placetype
|
||||||
|
if( placeType === item1.layer ){ return false; } |
||||||
|
|
||||||
|
// ensure the parent ids are the same for all placetypes
|
||||||
|
return isPropertyDifferent( item1.parent, item2.parent, placeType + '_id' ); |
||||||
}); |
}); |
||||||
return false; |
|
||||||
} |
} |
||||||
|
|
||||||
// if one has parent and the other doesn't consider different
|
// if one has parent info and the other doesn't, we consider them different
|
||||||
throw new Error('different'); |
return true; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Compare the name.* properties if they exist. |
* Compare the name properties if they exist. |
||||||
* Returns false if the objects are the same, and throws |
* Returns false if the objects are the same, else true. |
||||||
* an exception with the message 'different' if not. |
|
||||||
* |
|
||||||
* @param {object} item1 |
|
||||||
* @param {object} item2 |
|
||||||
* @returns {boolean} |
|
||||||
* @throws {Error} |
|
||||||
*/ |
*/ |
||||||
function assertNameMatch(item1, item2) { |
function isNameDifferent(item1, item2){ |
||||||
if (item1.hasOwnProperty('name') && item2.hasOwnProperty('name')) { |
let names1 = _.get(item1, 'name'); |
||||||
for (var lang in item1.name) { |
let names2 = _.get(item2, 'name'); |
||||||
if(item2.name.hasOwnProperty(lang) || lang === 'default') { |
|
||||||
|
// if neither object has name info, we consider them the same
|
||||||
|
if( !names1 && !names2 ){ return false; } |
||||||
|
|
||||||
|
// if both have name info
|
||||||
|
if( _.isPlainObject(names1) && _.isPlainObject(names2) ){ |
||||||
|
|
||||||
|
// iterate over all the languages in item1, comparing between items
|
||||||
|
return Object.keys(names1).some( lang => { |
||||||
|
|
||||||
// do not consider absence of an additional name as a difference
|
// do not consider absence of an additional name as a difference
|
||||||
propMatch(item1.name, item2.name, lang); |
// but strictly enfore that 'default' must be present and match
|
||||||
} |
if( _.has(names2, lang) || lang === 'default' ){ |
||||||
} |
|
||||||
|
// do not consider absence of an additional name as a difference
|
||||||
|
return isPropertyDifferent(names1, names2, lang); |
||||||
} |
} |
||||||
else { |
}); |
||||||
propMatch(item1, item2, 'name'); |
|
||||||
} |
} |
||||||
|
|
||||||
|
// if one has name info and the other doesn't, we consider them different
|
||||||
|
return true; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Compare the address_parts properties if they exist. |
* Compare the address_parts properties if they exist. |
||||||
* Returns false if the objects are the same, and throws |
* Returns false if the objects are the same, else true. |
||||||
* an exception with the message 'different' if not. |
|
||||||
* |
|
||||||
* @param {object} item1 |
|
||||||
* @param {object} item2 |
|
||||||
* @returns {boolean} |
|
||||||
* @throws {Error} |
|
||||||
*/ |
*/ |
||||||
function assertAddressMatch(item1, item2) { |
function isAddressDifferent(item1, item2){ |
||||||
// if neither record has address, assume same
|
let address1 = _.get(item1, 'address_parts'); |
||||||
if (!item1.hasOwnProperty('address_parts') && !item2.hasOwnProperty('address_parts')) { |
let address2 = _.get(item2, 'address_parts'); |
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
// if both have address, check parts
|
// if neither object has address info, we consider them the same
|
||||||
if (item1.hasOwnProperty('address_parts') && item2.hasOwnProperty('address_parts')) { |
if( !address1 && !address2 ){ return false; } |
||||||
propMatch(item1.address_parts, item2.address_parts, 'number'); |
|
||||||
propMatch(item1.address_parts, item2.address_parts, 'street'); |
// if both have address info
|
||||||
|
if( _.isPlainObject(address1) && _.isPlainObject(address2) ){ |
||||||
|
|
||||||
|
if( isPropertyDifferent(address1, address2, 'number') ){ return true; } |
||||||
|
if( isPropertyDifferent(address1, address2, 'street') ){ return true; } |
||||||
|
|
||||||
// only compare zip if both records have it, otherwise just ignore and assume it's the same
|
// only compare zip if both records have it, otherwise just ignore and assume it's the same
|
||||||
// since by this time we've already compared parent hierarchies
|
// since by this time we've already compared parent hierarchies
|
||||||
if (item1.address_parts.hasOwnProperty('zip') && item2.address_parts.hasOwnProperty('zip')) { |
if( _.has(address1, 'zip') && _.has(address2, 'zip') ){ |
||||||
propMatch(item1.address_parts, item2.address_parts, 'zip'); |
if( isPropertyDifferent(address1, address2, 'zip') ){ return true; } |
||||||
} |
} |
||||||
|
|
||||||
return false; |
return false; |
||||||
} |
} |
||||||
|
|
||||||
// one has address and the other doesn't, different!
|
// one has address and the other doesn't, different!
|
||||||
throw new Error('different'); |
return true; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Compare the two records and return true if they differ and false if same. |
* Compare the two records and return true if they differ and false if same. |
||||||
* |
|
||||||
* @param {object} item1 |
|
||||||
* @param {object} item2 |
|
||||||
* @returns {boolean} |
|
||||||
* @throws {Error} |
|
||||||
*/ |
*/ |
||||||
function isDifferent(item1, item2){ |
function isDifferent(item1, item2){ |
||||||
try { |
if( isLayerDifferent( item1, item2 ) ){ return true; } |
||||||
assertLayerMatch(item1, item2); |
if( isParentHierarchyDifferent( item1, item2 ) ){ return true; } |
||||||
assertParentHierarchyMatch(item1, item2); |
if( isNameDifferent( item1, item2 ) ){ return true; } |
||||||
assertNameMatch(item1, item2); |
if( isAddressDifferent( item1, item2 ) ){ return true; } |
||||||
assertAddressMatch(item1, item2); |
|
||||||
} |
|
||||||
catch (err) { |
|
||||||
if (err.message === 'different') { |
|
||||||
return true; |
|
||||||
} |
|
||||||
throw err; |
|
||||||
} |
|
||||||
|
|
||||||
return false; |
return false; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Throw exception if properties are different |
* return true if properties are different |
||||||
* |
|
||||||
* @param {object} item1 |
|
||||||
* @param {object} item2 |
|
||||||
* @param {string} prop |
|
||||||
* @throws {Error} |
|
||||||
*/ |
*/ |
||||||
function propMatch(item1, item2, prop) { |
function isPropertyDifferent(item1, item2, prop ){ |
||||||
var prop1 = item1[prop]; |
|
||||||
var prop2 = item2[prop]; |
|
||||||
|
|
||||||
// in the case the property is an array (currently only in parent schema)
|
// if neither item has prop, we consider them the same
|
||||||
// simply take the 1st item. this will change in the near future to support multiple hierarchies
|
if( !_.has(item1, prop) && !_.has(item2, prop) ){ return false; } |
||||||
if (_.isArray(prop1)) { prop1 = prop1[0]; } |
|
||||||
if (_.isArray(prop2)) { prop2 = prop2[0]; } |
|
||||||
|
|
||||||
if (normalizeString(prop1) !== normalizeString(prop2)) { |
// handle arrays and other non-string values
|
||||||
throw new Error('different'); |
var prop1 = field.getStringValue( _.get( item1, prop ) ); |
||||||
} |
var prop2 = field.getStringValue( _.get( item2, prop ) ); |
||||||
|
|
||||||
|
// compare strings
|
||||||
|
return normalizeString(prop1) !== normalizeString(prop2); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Remove punctuation and lowercase |
* lowercase characters and remove some punctuation |
||||||
* |
|
||||||
* @param {string} str |
|
||||||
* @returns {string} |
|
||||||
*/ |
*/ |
||||||
function normalizeString(str){ |
function normalizeString(str){ |
||||||
if (!_.isString(str)) { |
|
||||||
return str; |
|
||||||
} |
|
||||||
|
|
||||||
if (_.isEmpty(str)) { |
|
||||||
return ''; |
|
||||||
} |
|
||||||
|
|
||||||
return str.toLowerCase().split(/[ ,-]+/).join(' '); |
return str.toLowerCase().split(/[ ,-]+/).join(' '); |
||||||
} |
} |
||||||
|
|
||||||
|
Loading…
Reference in new issue