-1

So, I'm mostly interested in seeing who has a clever solution to this problem. It's likely going to come down to split and some comparison loop.

The problem: I am combining multiple fields of exif data to create a title for an image. The trouble is that this exif data ranges widely in content.

Nikon example:

exifData.Make = "NIKON CORPORATION"
exifData.Model = "NIKON D90"

Motorola phone example:

exifData.Make = "Motorola"
exifData.Model = "XT1032"

In the Motorola case, I'd simply use the combination of Make and Model.

However, in the Nikon case, there's duplication, so I'd like to ignore the Make and give preference to the Model in that case.

j_d
  • 2,818
  • 9
  • 50
  • 91
  • If you use `split` then you can compute the intersection of the two arrays you'll get (https://stackoverflow.com/q/11076067/3410584). Then, if the intersection is not empty, you'll have to implement your own logic to know what field you prefer. – ValLeNain Apr 08 '18 at 14:17
  • @ValLeNain As far as I can see that doesn't handle case insensitivity. – j_d Apr 08 '18 at 14:59

3 Answers3

2

Convert the Make into a RegExp that contains a group of words, and test if the Model contains a match. If it contains, take the Model, if not, combine Make and Model.

const getTitle = ({ Make, Model }) =>
  new RegExp('\\b' + Make.replace(/\s+/g, '|') + '\\b', 'i')
    .test(Model) ? Model : `${Make} ${Model}`;

console.log(getTitle({ Make: "Motorola", Model: "XT1032" }));
console.log(getTitle({ Make: "NIKON CORPORATION", Model: "NIKON D90" }));
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
0

You can split the Make string and check first token within Model using includes():

let exifData1 = {Make: "NIKON CORPORATION", Model: "NIKON D90"},
    exifData2 = {Make: "Motorola", Model: "XT1032"};

function getName(o) {
  return !o.Model.includes(o.Make.split(" ")[0]) ? o.Make + " " + o.Model : o.Model;
}

console.log(getName(exifData1));
console.log(getName(exifData2));
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
0

let exifData = {};

exifData.Make = "NIKON CORPORATION"
exifData.Model = "NIKON D90"

let elems = {},
  repeats = {};

let make = exifData.Make.toLowerCase().split(" ").forEach(data => {
  elems[data] = true;
});

let model = exifData.Model.toLowerCase().split(" ").forEach(data => {
  if(elems[data]) repeats[data] = true;
});
Artem
  • 71
  • 4