"use strict" ;
const createDatasheet = require ( "../../shared/create-datasheet" ) ;
function isEnglish ( document ) {
return /\sen\s*$/ . test ( document . description ) ;
}
module . exports = function tmeNormalizeProduct ( ) {
return async function ( api ) {
let { data } = api ;
if ( data . itemData . documents . length > 0 ) {
if ( typeof data . itemData . documents [ 0 ] === "string" ) {
// Temporary workaround for the dependsOn not taking into account task versions, and some old records existing with a wrong documents structure
return ;
}
let manufacturer = data . itemData . manufacturer ;
let modelName = data . itemData . model ;
let description = data . itemData . description ;
let productID = data . itemData . productID ;
let firstEnglish = data . itemData . documents . find ( ( document ) => isEnglish ( document ) ) ;
let bestDocument = ( firstEnglish != null )
? firstEnglish
: data . itemData . documents [ 0 ] ;
let bestDocumentIsEnglish = isEnglish ( bestDocument ) ;
createDatasheet ( api , {
priority : ( bestDocumentIsEnglish )
? 0.6
: 0.5 ,
source : "tme" ,
manufacturer : manufacturer ,
productID : productID ,
name : modelName ,
description : description ,
url : bestDocument . url ,
// NOTE: Most (but not all!) manufacturers on TME are, incorrectly, in ALL-CAPS. This 'fixes' those cases through best-effort capitalization. Many (but less!) will still be wrong and need to be fixed later.
fixCasing : true
} ) ;
}
} ;
} ;