2023-08-09 23:10:45 +02:00
import custom_utils from './utils'
import MappingConfigType from "./mapping-config.type" ;
2024-10-08 10:09:21 +02:00
import Formatters from "./formatters" ;
2023-08-09 23:10:45 +02:00
const { debugLog } = custom_utils
let listOfBooleanKeys = [
"prise_type_ef" ,
"prise_type_2" ,
"prise_type_combo_ccs" ,
"prise_type_chademo" ,
"gratuit" ,
"paiement_acte" ,
"paiement_cb" ,
"cable_t2_attache"
]
2023-10-01 12:53:08 +02:00
function boolToAddable ( someBooleanValue : boolean ) {
return someBooleanValue ? 1 : 0
}
2023-08-09 23:10:45 +02:00
export default class {
mapping_config : any = { }
2024-10-08 10:37:58 +02:00
private jardinage = false ;
2023-08-30 15:55:45 +02:00
public stats : any ;
2023-10-01 12:53:08 +02:00
private current_converted_geojson_point : any ;
2024-10-08 12:25:51 +02:00
private current_geojson_point : any ; // currently converting point
private list_of_points : any ; // list of geojson points
2023-08-09 23:10:45 +02:00
constructor ( mappingConfig : MappingConfigType ) {
2023-08-18 11:25:02 +02:00
2023-08-09 23:10:45 +02:00
this . setConfig ( mappingConfig )
2023-08-30 15:55:45 +02:00
this . stats = {
2024-10-08 12:25:51 +02:00
filtered_by_excluded_tags : 0 ,
2023-08-30 15:55:45 +02:00
phones_updated : 0 ,
2023-10-01 12:53:08 +02:00
power_output : 0 ,
2023-08-30 15:55:45 +02:00
phones_updated_list : [ ] ,
phones_not_updated : 0
}
2023-08-09 23:10:45 +02:00
}
setConfig ( mappingConfig : MappingConfigType ) {
2024-10-15 18:43:52 +02:00
debugLog ( 'load config' , mappingConfig . config_name )
2023-08-30 15:09:41 +02:00
debugLog ( 'load config' , mappingConfig . config_name )
2023-08-09 23:10:45 +02:00
this . mapping_config = mappingConfig
}
2023-08-18 11:25:02 +02:00
getConfig() {
return this . mapping_config
}
2023-08-09 23:10:45 +02:00
mapFeaturePoint ( featurePointGeoJson : any ) {
let geoJSONConvertedPoint : any = { }
2023-08-30 15:55:45 +02:00
2023-08-09 23:10:45 +02:00
geoJSONConvertedPoint . properties = { . . . this . mapping_config . default_properties_of_point }
geoJSONConvertedPoint . type = featurePointGeoJson . type
geoJSONConvertedPoint . geometry = featurePointGeoJson . geometry
2023-10-01 12:53:08 +02:00
this . current_converted_geojson_point = geoJSONConvertedPoint
2023-08-09 23:10:45 +02:00
return geoJSONConvertedPoint
}
/ * *
* TODO convert to mapping config property to transform_truthy
* @param pointKeyName
* @returns { boolean }
* /
isBooleanKey ( pointKeyName : string ) : boolean {
return listOfBooleanKeys . indexOf ( pointKeyName ) !== - 1
}
2023-08-18 11:58:52 +02:00
truthyValues = [ true , 'true' , 'True' , 'TRUE' , '1' , 'yes' , 1 ]
falsyValues = [ false , 'false' , 'False' , 'FALSE' , '0' , 'no' , 0 ]
2023-08-09 23:10:45 +02:00
/ * *
2024-07-08 11:27:27 +02:00
* filter : reduce number of features
2023-08-09 23:10:45 +02:00
* @param offsetCount
* @param listOfFeatures
* /
filterFeaturesByOffset ( offsetCount : number , listOfFeatures : any ) : Array < any > {
let filteredList = listOfFeatures
// TODO
return filteredList
}
2024-10-08 12:25:51 +02:00
/ * *
* filterFeaturesByPropertyRegex
* TODO
* @param propertyName
* @param criteriaRegex
* @param listOfFeatures
* /
2023-08-09 23:10:45 +02:00
filterFeaturesByPropertyRegex ( propertyName : string , criteriaRegex : any , listOfFeatures : any ) {
let filteredList = listOfFeatures . filter ( ( feature : any ) = > {
return criteriaRegex . test ( feature ? . properties [ propertyName ] )
} )
return filteredList
}
2024-10-08 12:25:51 +02:00
/ * *
* filter a list of geojson points if one of the given exludedKeys is present in their properties .
* Example , we do not want to convert already present OSM point which have an osm_id value in their properties .
* @param list
* @param excludedKeys
* /
2024-10-15 18:43:52 +02:00
filterListOfPointsByExcludingIfKeyFilled ( list : any , excludedKeys : Array < string > ) : any [ ] {
2024-10-08 12:25:51 +02:00
let newList : Array < any > = [ ]
list . forEach ( ( geojsonPoint : any ) = > {
let pointProperties = Object . keys ( geojsonPoint . properties )
let addPoint = true ;
excludedKeys . forEach ( ( key : any ) = > {
debugLog ( key , 'pointProperties[key]' , pointProperties [ key ] )
2024-10-15 18:43:52 +02:00
let foundProperty : string = pointProperties [ key ]
if ( foundProperty && foundProperty !== 'null' ) {
2024-10-08 12:25:51 +02:00
addPoint = false
}
} )
if ( addPoint ) {
// only add points that pass the not null filter
newList . push ( geojsonPoint )
} else {
this . stats . filtered_by_excluded_tags ++
}
} )
return newList ;
}
2023-08-09 23:10:45 +02:00
/ * *
* retuns the converted element from mapping config if present , null otherwise
* /
mapElementFromConf ( featurePoint : any ) : any {
2023-08-18 12:08:25 +02:00
debugLog ( 'mapElementFromConf: mapElementFromConf' , featurePoint )
2023-08-09 23:10:45 +02:00
if ( ! this . mapping_config ) {
2023-08-18 11:25:02 +02:00
throw new Error ( 'no config was loaded in the mapping engine. use setConfig(my_mapping_config) on this instance of mapping engine before using this. Your config should be typed to MappingConfigType Type.' )
2023-08-09 23:10:45 +02:00
}
debugLog ( 'mapElementFromConf: config_name' , this . mapping_config . config_name )
let mappingKeys = Object . keys ( this . mapping_config . tags )
2023-10-07 23:15:21 +02:00
let featurePointPropertiesKeys = [ ]
if ( this . mapping_config . osmose ) {
// only creation of new points are handled by now [2023-10-07]
featurePointPropertiesKeys = Object . keys ( featurePoint . properties . fixes [ 0 ] [ 0 ] . create )
2023-10-07 23:19:17 +02:00
// debugLog('featurePointPropertiesKeys', featurePointPropertiesKeys)
2023-10-07 23:15:21 +02:00
} else {
featurePointPropertiesKeys = Object . keys ( featurePoint . properties )
}
2023-08-09 23:10:45 +02:00
debugLog ( 'mapElementFromConf: ============= keys mappingKeys:' , this . mapping_config . tags . length , mappingKeys . length )
debugLog ( 'mapElementFromConf: ============= keys featurePointPropertiesKeys :' , featurePoint . properties . length , featurePointPropertiesKeys . length )
let newProperties = { . . . this . mapping_config . default_properties_of_point }
// reinit properties of current point
let basePoint = Object . create ( featurePoint )
basePoint . type = featurePoint . type
basePoint . geometry = featurePoint . geometry
basePoint . properties = { . . . this . mapping_config . default_properties_of_point }
// apply new properties if found in mapping config
featurePointPropertiesKeys . forEach ( pointKeyName = > {
debugLog ( 'mapElementFromConf: convert' , pointKeyName )
2023-08-18 11:39:40 +02:00
debugLog ( 'mapElementFromConf: mapping keys:' , mappingKeys )
2024-10-07 23:26:43 +02:00
2024-10-08 10:37:58 +02:00
this . convertProperty ( pointKeyName , mappingKeys , featurePoint , newProperties )
2024-10-07 23:26:43 +02:00
2023-08-09 23:10:45 +02:00
} )
basePoint . properties = newProperties
// debugLog('mapElementFromConf: basePoint', basePoint)
return basePoint
}
/ * *
* convertit une propriété en une autre selon la config de mapping
* @param pointKeyName
* @param mappingKeys
* @param featurePoint
* @param newProperties
* /
convertProperty ( pointKeyName : string , mappingKeys : any , featurePoint : any , newProperties : any ) {
2023-10-01 12:53:08 +02:00
this . current_geojson_point = featurePoint
2023-10-07 23:15:21 +02:00
let originalValue = ''
2024-10-14 16:19:38 +02:00
2023-10-07 23:15:21 +02:00
if ( this . mapping_config . osmose ) {
originalValue = featurePoint . properties . fixes [ 0 ] [ 0 ] . create [ pointKeyName ]
} else {
originalValue = featurePoint . properties [ pointKeyName ]
}
let intOriginalValue = parseInt ( originalValue )
2023-08-09 23:10:45 +02:00
let mappingValueObject : any = '' ;
2023-08-18 11:39:40 +02:00
if ( mappingKeys . indexOf ( pointKeyName ) !== - 1 ) {
2023-08-09 23:10:45 +02:00
mappingValueObject = this . mapping_config . tags [ pointKeyName ]
debugLog ( 'convertProperty: mappingValueObject ' , mappingValueObject )
}
debugLog ( ' ------ convertProperty: pointKeyName' , pointKeyName )
// debugLog('convertProperty: mappingKeys', mappingKeys)
2024-10-07 23:26:43 +02:00
let remove_original_key = false ;
2024-10-15 18:43:52 +02:00
debugLog ( 'tags_to_ignore_if_value_is' , this . mapping_config . tags_to_ignore_if_value_is )
if ( this . mapping_config . tags_to_ignore_if_value_is && this . mapping_config . tags_to_ignore_if_value_is . length && this . mapping_config . tags_to_ignore_if_value_is ? . indexOf ( originalValue ) !== - 1 ) {
2024-10-08 10:37:58 +02:00
debugLog ( '(x) => ignore' , originalValue , ' in ' , pointKeyName )
2024-10-07 23:26:43 +02:00
remove_original_key = true ;
2024-10-15 18:43:52 +02:00
2024-10-07 23:26:43 +02:00
}
2023-08-30 15:55:45 +02:00
if ( this . jardinage ) {
debugLog ( ' ------ on fait du jardinage' )
debugLog ( ' ------ mode mise en qualité activé' )
debugLog ( ' ------ les données en entrée sont des infos geojson extraites depuis overpass turbo.' )
debugLog ( ' ------ les clés des objets sont donc déjà dans le format de tag OSM,' +
'ne pas les convertir pour les mettre en qualité selon le modèle de mapping.' )
}
2023-08-09 23:10:45 +02:00
if ( this . mapping_config . add_not_mapped_tags_too && ( mappingKeys . indexOf ( pointKeyName ) === - 1 ) ) {
/ * *
* add all unmapped tags is enabled
* /
2023-08-18 11:39:40 +02:00
debugLog ( ' ------ add all unmapped tags is enabled' )
2023-08-09 23:10:45 +02:00
newProperties [ pointKeyName ] = originalValue ;
} else {
/ * *
* only use existing keys
* /
2024-10-08 10:37:58 +02:00
debugLog ( "only use existing keys," , pointKeyName )
2023-08-18 11:39:40 +02:00
if ( mappingKeys . indexOf ( pointKeyName ) !== - 1 ) {
2023-08-09 23:10:45 +02:00
let valueConvertedFromMapping = featurePoint . properties [ pointKeyName ]
let keyConvertedFromMapping = mappingKeys [ mappingKeys . indexOf ( pointKeyName ) ]
let mappingConfigOfTag = this . mapping_config . tags [ pointKeyName ]
debugLog ( '========== mappingConfigOfTag' , mappingConfigOfTag )
debugLog ( 'convertProperty: found element' , pointKeyName , '=>' , keyConvertedFromMapping , 'value : ' , valueConvertedFromMapping )
let convertedValue = originalValue
let typeOfConfigForKey = typeof mappingConfigOfTag
let isStringValue = typeOfConfigForKey === 'string'
let isConfigMappingObject = typeOfConfigForKey === 'object'
debugLog ( 'convertProperty: - typeofValue' , typeOfConfigForKey )
debugLog ( 'convertProperty: - pointKeyName' , pointKeyName )
debugLog ( 'convertProperty: - valueConvertedFromMapping' , valueConvertedFromMapping )
debugLog ( 'typeof valueConvertedFromMapping === \'string\'' , typeOfConfigForKey )
debugLog ( 'convertProperty: isStringValue?' , valueConvertedFromMapping , isStringValue )
debugLog ( 'convertProperty: isStringValue?' , valueConvertedFromMapping , isStringValue )
debugLog ( 'mappingConfigOfTag' , mappingConfigOfTag )
debugLog ( 'typeOfConfigForKey' , typeOfConfigForKey )
/ * *
* conversion si la clé à une config d ' une string , on ne change que la clé , pas la valeur
* /
if ( isStringValue ) {
debugLog ( 'convertProperty: -- string value' )
debugLog ( 'convertProperty: -- string value' )
debugLog ( 'convertProperty: -- simple conversion : ' , pointKeyName , '=> ' , mappingConfigOfTag , '_' , originalValue , '=>' , valueConvertedFromMapping )
debugLog ( 'convertProperty: -- convertedValue' , convertedValue )
convertedValue = valueConvertedFromMapping
if ( convertedValue ) {
newProperties [ mappingConfigOfTag ] = convertedValue
}
} else {
debugLog ( 'convertProperty: no string value' )
}
2024-10-07 23:26:43 +02:00
let configObject = mappingConfigOfTag
2023-08-09 23:10:45 +02:00
if ( isConfigMappingObject ) {
2024-10-07 23:26:43 +02:00
2023-08-09 23:10:45 +02:00
debugLog ( 'convertProperty: is config object' , configObject )
let newKey : any = '' + pointKeyName
2024-10-07 23:26:43 +02:00
2023-08-09 23:10:45 +02:00
if ( configObject . key_converted ) {
newKey = configObject . key_converted
2024-10-15 18:43:52 +02:00
debugLog ( 'key_converted newKey' , newKey )
2023-08-09 23:10:45 +02:00
}
2023-10-01 12:53:08 +02:00
if ( configObject . transform_function ) {
convertedValue = configObject . transform_function ( originalValue )
}
2023-08-09 23:10:45 +02:00
if ( configObject . truthy_value ) {
// convertir la valeur, si elle est truthy, la transformer en ce que donne la propriété truthy_value
// exemple: le jeu de données dit que la colonne cable_t2_attache vaut "True", mais on veut le convertir en "1".
// on met donc truthy_value: '1'
debugLog ( 'truthy_value' , originalValue )
if ( this . truthyValues . indexOf ( originalValue ) !== - 1 ) {
convertedValue = configObject . truthy_value
}
}
if ( configObject . falsy_value ) {
if ( this . falsyValues . indexOf ( originalValue ) !== - 1 ) {
convertedValue = configObject . falsy_value
}
}
/ * *
* conversion booléenne
* /
if ( mappingValueObject . convert_to_boolean_value ) {
debugLog ( 'convertProperty: is boolean_value_conversion' )
2023-10-01 12:53:08 +02:00
2023-10-01 15:10:14 +02:00
convertedValue = this . convertToYesOrNo ( originalValue )
2023-10-01 12:53:08 +02:00
} else {
debugLog ( 'convertProperty: is NOT having boolean_value_conversion' , mappingValueObject )
}
2023-10-07 22:51:54 +02:00
// gestion des puissances de bornes
// avec une fonction de transformation des valeurs
// parmi le domaine du jeu de données
// nécessite une clé conditionnelle à la valeur true d'autres clés converties.
2023-10-01 12:53:08 +02:00
if ( configObject . socket_output_find_correspondances ) {
// trouver à quel socket ça correspond
// si y'a plusieurs sockets, utiliser socket:max:output
let we_use_max_output = false ;
2023-10-01 13:28:36 +02:00
let has_prise_type_2 : boolean = this . isTruthyValue ( this . current_geojson_point . properties . prise_type_2 ) || false
let has_prise_type_combo_ccs : boolean = this . isTruthyValue ( this . current_geojson_point . properties . prise_type_combo_ccs ) || false
let prise_type_chademo : boolean = this . isTruthyValue ( this . current_geojson_point . properties . prise_type_chademo ) || false
let prise_type_ef : boolean = this . isTruthyValue ( this . current_geojson_point . properties . prise_type_ef ) || false
let prise_type_e : boolean = this . isTruthyValue ( this . current_geojson_point . properties . prise_type_e ) || false
let prise_type_autre : boolean = this . isTruthyValue ( this . current_geojson_point . properties . prise_type_autre ) || false
2023-10-01 12:53:08 +02:00
2023-10-01 13:07:53 +02:00
let countOfSockets = ( boolToAddable ( has_prise_type_2 ) + boolToAddable ( has_prise_type_combo_ccs ) + boolToAddable ( prise_type_chademo ) +
2023-10-01 12:53:08 +02:00
boolToAddable ( prise_type_ef ) + boolToAddable ( prise_type_autre ) + boolToAddable ( prise_type_e )
2023-10-01 13:07:53 +02:00
) ;
if ( countOfSockets > 0 ) {
2023-10-01 12:53:08 +02:00
we_use_max_output = true ;
2023-08-09 23:10:45 +02:00
}
2023-10-01 12:53:08 +02:00
// ajouter les tags de socket newProperties
let converted_value = originalValue . replace ( /[^\d\.\,]/g , '' ) . replace ( ',' , '.' )
2023-10-01 13:07:53 +02:00
let max_output = 401
2023-10-01 12:53:08 +02:00
// do not limit accepted values
2023-10-01 13:07:53 +02:00
let out = ''
2023-10-07 23:15:21 +02:00
if ( intOriginalValue < max_output ) {
2023-10-01 13:07:53 +02:00
2024-10-08 10:09:21 +02:00
// rajouter l'unité de puissance kW dans la valeur
2023-10-01 13:07:53 +02:00
out = converted_value + ' kW'
} else {
2023-10-01 13:28:36 +02:00
// prise en charge des valeurs en Watts et non en kW.
2024-10-08 10:09:21 +02:00
debugLog ( 'too high kW value detected' , originalValue )
2023-10-07 23:15:21 +02:00
if ( intOriginalValue > 1000 && intOriginalValue < 401000 ) {
2023-10-01 13:07:53 +02:00
2023-10-01 13:28:36 +02:00
let kilowatts = ( parseFloat ( converted_value ) / 1000 ) . toFixed ( 2 ) . replace ( '.00' , '' ) ;
out = ( '' + kilowatts + ' kW' ) . replace ( '.00' , '' )
2024-10-08 10:09:21 +02:00
debugLog ( 'valeurs en Watts out' , out , 'original:' , originalValue )
2023-10-07 23:15:21 +02:00
this . stats . power_output ++
2023-10-01 13:07:53 +02:00
}
}
2023-10-01 13:28:36 +02:00
out = ( out ) . replace ( '.00' , '' )
// debug land
if ( has_prise_type_combo_ccs ) {
2023-10-11 12:44:19 +02:00
newProperties [ 'socket:type2_combo:output' ] = out ;
2023-10-07 22:51:54 +02:00
this . stats . power_output ++
2023-10-01 13:28:36 +02:00
}
2023-10-01 13:07:53 +02:00
if ( we_use_max_output ) {
2023-10-10 10:53:38 +02:00
newProperties [ 'charging_station:output' ] = out ;
2023-10-01 13:07:53 +02:00
} else {
2023-10-01 13:30:14 +02:00
if ( has_prise_type_2 && prise_type_e ) {
newProperties [ 'socket:type_2:output' ] = out ;
2023-10-07 22:51:54 +02:00
this . stats . power_output ++
2023-10-07 23:19:17 +02:00
debugLog ( '2 prises, attribuer la plus haute valeur à la type 2' , out )
2023-10-01 13:30:14 +02:00
}
2023-10-01 13:07:53 +02:00
if ( countOfSockets === 1 ) {
2023-10-01 12:53:08 +02:00
if ( has_prise_type_2 ) {
newProperties [ 'socket:type_2:output' ] = out ;
2023-10-07 22:51:54 +02:00
newProperties [ 'socket:type_2' ] = 1 ;
this . stats . power_output ++
2023-10-01 12:53:08 +02:00
}
if ( has_prise_type_combo_ccs ) {
2023-10-07 21:52:18 +02:00
newProperties [ 'socket:type2_combo:output' ] = out ;
2023-10-07 22:51:54 +02:00
newProperties [ 'socket:type2_combo' ] = 1 ;
this . stats . power_output ++
2023-10-01 12:53:08 +02:00
}
if ( prise_type_chademo ) {
newProperties [ 'socket:chademo:output' ] = out ;
2023-10-07 22:51:54 +02:00
newProperties [ 'socket:chademo' ] = 1 ;
this . stats . power_output ++
2023-10-01 12:53:08 +02:00
}
if ( prise_type_e ) {
newProperties [ 'socket:typee:output' ] = out ;
2023-10-07 22:51:54 +02:00
newProperties [ 'socket:typee' ] = 1 ;
this . stats . power_output ++
2023-10-01 12:53:08 +02:00
}
2023-10-01 13:28:36 +02:00
} else {
2023-10-07 23:19:17 +02:00
debugLog ( 'no sockets' , this . current_geojson_point . properties . ref )
2023-10-01 12:53:08 +02:00
}
2023-08-09 23:10:45 +02:00
}
2023-10-01 13:07:53 +02:00
return out
2023-10-01 12:53:08 +02:00
2023-08-09 23:10:45 +02:00
}
2023-10-01 15:10:14 +02:00
if ( configObject . invert_boolean_value ) {
2023-10-07 23:15:21 +02:00
convertedValue = ! this . convertToBoolean ( originalValue ) ? 'yes' : 'no'
2023-10-07 23:19:17 +02:00
debugLog ( 'invert boolean' , convertedValue , originalValue )
2023-10-01 15:10:14 +02:00
}
2023-10-01 12:53:08 +02:00
2023-08-18 11:25:02 +02:00
if ( configObject . convert_to_phone ) {
2024-10-08 10:37:58 +02:00
convertedValue = Formatters . convertToPhone ( originalValue )
2023-08-30 15:55:45 +02:00
if ( originalValue !== convertedValue ) {
this . stats . phones_updated ++
this . stats . phones_updated_list . push ( convertedValue )
} else {
this . stats . phones_not_updated ++
}
2024-10-15 18:43:52 +02:00
debugLog ( 'convertedValue convert_to_phone' , originalValue , '=>' , convertedValue )
2023-08-30 15:55:45 +02:00
2023-08-18 11:25:02 +02:00
}
2024-10-09 16:22:57 +02:00
if ( configObject . convert_to_name ) {
convertedValue = Formatters . convertToName ( originalValue )
}
2023-08-09 23:10:45 +02:00
if ( configObject . remove_original_key ) {
remove_original_key = true
}
if ( configObject . ignore_if_falsy && this . falsyValues . indexOf ( originalValue ) !== - 1 ) {
remove_original_key = true
}
2023-08-18 12:22:07 +02:00
if ( configObject . ignore_if_truthy && this . truthyValues . indexOf ( originalValue ) !== - 1 ) {
remove_original_key = true
}
2023-08-09 23:10:45 +02:00
/ * *
* config pour une clé
* nous pouvons renseigner une string ou un objet décrivant les transformations à réaliser
* /
2024-10-08 10:37:58 +02:00
if ( configObject . conditional_values ) {
2023-08-09 23:10:45 +02:00
2024-10-14 16:19:38 +02:00
2024-10-09 16:34:52 +02:00
// convert numbers from json to string to compare them correctly
2024-10-15 18:43:52 +02:00
originalValue = '' + originalValue
2024-10-09 16:34:52 +02:00
2023-08-09 23:10:45 +02:00
let keysConditionnalValues : any = Object . keys ( configObject . conditional_values )
let isFoundValue = keysConditionnalValues . indexOf ( originalValue )
2024-10-08 10:37:58 +02:00
let conditionnalConfig : any = configObject . conditional_values [ keysConditionnalValues [ isFoundValue ] ]
debugLog ( 'convertProperty: conditional_values__________' ,
configObject . conditional_values )
2023-08-09 23:10:45 +02:00
debugLog ( 'isFoundValue' , isFoundValue , originalValue )
debugLog ( 'keysConditionnalValues' , keysConditionnalValues )
2024-10-09 16:34:52 +02:00
debugLog ( '-----++++++++ originalValue' , originalValue )
debugLog ( '----------- isFoundValue' , isFoundValue )
2024-10-14 16:19:38 +02:00
2024-10-08 10:37:58 +02:00
if ( ! remove_original_key ) {
2023-08-09 23:10:45 +02:00
2024-10-09 16:34:52 +02:00
if ( isFoundValue !== - 1 ) {
2024-10-15 18:43:52 +02:00
debugLog ( 'found condition' , isFoundValue )
2023-10-01 15:10:14 +02:00
2024-10-08 10:37:58 +02:00
/ * * - - - - - - - - - - - - - - - - - - - - - -
* gestion des valeurs conditionnelles
* -- -- -- -- -- -- -- -- -- -- -- * /
debugLog ( 'conditionnalConfig' , conditionnalConfig )
if ( conditionnalConfig . ignore_this_data ) {
debugLog ( ` on ignore cette clé car sa valeur " ${ originalValue } " est à exclure: ` , pointKeyName , '=>' , newKey )
remove_original_key = true ;
2023-08-09 23:10:45 +02:00
}
2024-10-08 10:37:58 +02:00
if ( conditionnalConfig . truthy_value ) {
// convertir la valeur, si elle est truthy, la transformer en ce que donne la propriété truthy_value
// exemple: le jeu de données dit que la colonne cable_t2_attache vaut "True", mais on veut le convertir en "1".
// on met donc truthy_value: '1'
if ( this . truthyValues . indexOf ( originalValue ) !== - 1 ) {
convertedValue = conditionnalConfig . truthy_value
}
}
if ( conditionnalConfig . falsy_value ) {
if ( this . falsyValues . indexOf ( originalValue ) !== - 1 ) {
convertedValue = conditionnalConfig . falsy_value
}
2023-08-09 23:10:45 +02:00
}
2023-10-01 12:53:08 +02:00
2024-10-08 10:37:58 +02:00
// use the value converted
else if ( conditionnalConfig . value_converted ) {
convertedValue = conditionnalConfig . value_converted
}
2023-08-09 23:10:45 +02:00
2024-10-08 10:37:58 +02:00
}
}
if ( conditionnalConfig ? . tags_to_add ) {
debugLog ( 'on ajoute des tags' , conditionnalConfig . tags_to_add )
// on peut définir un ensemble de tags à rajouter
let tagKeys = Object . keys ( conditionnalConfig . tags_to_add )
debugLog ( 'conditionnalConfig.tags_to_add' , conditionnalConfig . tags_to_add )
2024-10-15 18:43:52 +02:00
tagKeys . forEach ( ( index : any ) = > {
2024-10-09 16:34:52 +02:00
debugLog ( 'key' , index )
debugLog ( 'value' , conditionnalConfig . tags_to_add [ index ] )
newProperties [ index ] = conditionnalConfig . tags_to_add [ index ]
2024-10-08 10:37:58 +02:00
} )
2023-08-09 23:10:45 +02:00
}
}
2024-10-08 10:37:58 +02:00
2023-08-09 23:10:45 +02:00
debugLog ( 'convertProperty: convertedValue ==========> {' , newKey , ':' , convertedValue , '}' )
2023-08-18 11:25:02 +02:00
debugLog ( ' =============== remove_original_key' , newKey , remove_original_key )
2024-10-15 18:43:52 +02:00
let keysOfConfigObject = [ ] ;
let hasKeyIgnoreThisData = false ;
if ( configObject ) {
keysOfConfigObject = Object . keys ( configObject )
debugLog ( 'keysOfConfigObject' , keysOfConfigObject )
hasKeyIgnoreThisData = ( keysOfConfigObject . indexOf ( 'ignore_this_data' ) !== - 1 )
}
debugLog ( 'remove_original_key && newKey && convertedValue && hasKeyIgnoreThisData' , remove_original_key , newKey , convertedValue , hasKeyIgnoreThisData )
if ( ! remove_original_key && newKey && convertedValue && ! hasKeyIgnoreThisData
) {
debugLog ( 'convertedValue' , convertedValue )
2023-08-09 23:10:45 +02:00
2024-10-08 10:37:58 +02:00
debugLog ( 'convertProperty: added' , newKey , ( ` ${ convertedValue } ` ) . trim ( ) )
2023-08-18 11:58:52 +02:00
newProperties [ newKey ] = ( ` ${ convertedValue } ` ) . trim ( )
2023-08-09 23:10:45 +02:00
}
2024-10-08 10:09:21 +02:00
2023-08-09 23:10:45 +02:00
}
2023-08-18 11:58:52 +02:00
} else {
2023-08-18 11:39:40 +02:00
debugLog ( '!!!!!! property not found in mappingKeys: ' , pointKeyName )
2023-08-09 23:10:45 +02:00
}
}
2024-10-08 10:37:58 +02:00
debugLog ( 'newProperties' , newProperties )
2023-08-30 15:55:45 +02:00
2023-08-18 12:08:25 +02:00
return newProperties ;
2023-08-09 23:10:45 +02:00
}
2023-10-01 12:53:08 +02:00
private isTruthyValue ( someValue : string ) {
2023-10-01 13:28:36 +02:00
let convertedValue ;
if ( this . truthyValues . indexOf ( someValue ) !== - 1 ) {
convertedValue = true
}
if ( this . falsyValues . indexOf ( someValue ) !== - 1 ) {
convertedValue = false
}
return convertedValue
2023-10-01 12:53:08 +02:00
}
2023-10-01 15:10:14 +02:00
private convertToYesOrNo ( originalValue : any ) {
2023-10-01 12:53:08 +02:00
debugLog ( 'convertProperty: ==========> original value' , originalValue )
let convertedValue = '' ;
if ( this . truthyValues . indexOf ( originalValue ) !== - 1 ) {
convertedValue = 'yes'
} else {
debugLog ( 'convertProperty: ==========> !!! NOT in truthy values' , originalValue )
}
if ( this . falsyValues . indexOf ( originalValue ) !== - 1 ) {
convertedValue = 'no'
} else {
debugLog ( 'convertProperty: ==========> !!! NOT in falsy values' , originalValue )
}
return convertedValue ;
}
2023-10-07 23:15:21 +02:00
2023-10-01 15:10:14 +02:00
private convertToBoolean ( originalValue : any ) {
debugLog ( 'convertProperty: ==========> original value' , originalValue )
2023-10-07 23:15:21 +02:00
let convertedValue ;
2023-10-01 15:10:14 +02:00
if ( this . truthyValues . indexOf ( originalValue ) !== - 1 ) {
convertedValue = true
} else {
debugLog ( 'convertProperty: ==========> !!! NOT in truthy values' , originalValue )
}
if ( this . falsyValues . indexOf ( originalValue ) !== - 1 ) {
convertedValue = false
} else {
debugLog ( 'convertProperty: ==========> !!! NOT in falsy values' , originalValue )
}
return convertedValue ;
}
2023-10-01 12:53:08 +02:00
2024-07-08 11:27:27 +02:00
}