All files engine.ts

37.43% Statements 76/203
66.66% Branches 4/6
44.44% Functions 4/9
37.43% Lines 76/203

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 2031x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                             1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x           1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                   1x 1x                                                                                                                                                                   1x 1x
import custom_utils from './utils'
 
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"
]
 
export default class {
    mapping_config: any = {}
 
    constructor(mappingConfig) {
        this.setConfig(mappingConfig)
    }
 
    setConfig(mappingConfig) {
        this.mapping_config = mappingConfig
    }
 
    mapFeaturePoint(featurePointGeoJson) {

        let geoJSONConvertedPoint: any = {}
        geoJSONConvertedPoint.properties = {...this.mapping_config.default_properties_of_point}
        geoJSONConvertedPoint.type = featurePointGeoJson.type
        geoJSONConvertedPoint.geometry = featurePointGeoJson.geometry

        let props = featurePointGeoJson.properties

        props.forEach((key, value) => {

        })

        return geoJSONConvertedPoint
    }
 
    /**
     * TODO convert to mapping config property to transform_truthy
     * @param pointKeyName
     * @returns {boolean}
     */
    isBooleanKey(pointKeyName): boolean {

        return listOfBooleanKeys.indexOf(pointKeyName) !== -1
    }
 
    truthyValues = ['true', 'True', 'TRUE', '1', 1]
    falsyValues = ['false', 'False', 'FALSE', '0', 0]
 
    /**
     * reduce number of features
     * @param offsetCount
     * @param listOfFeatures
     */
    filterFeaturesByOffset(offsetCount: number, listOfFeatures): Array<any> {
        let filteredList = listOfFeatures
        // TODO
        return filteredList
    }
 
    // filterFeaturesByPropertyRegex(bboxConfig, listOfFeatures) {
    //     console.log('bboxConfig', bboxConfig)
    //     let filteredList = listOfFeatures
    //     // TODO
    //     return filteredList
    // }
 
    filterFeaturesByPropertyRegex(propertyName, criteriaRegex, listOfFeatures) {
        let filteredList = listOfFeatures.filter(feature => {
            return criteriaRegex.test(feature?.properties[propertyName])
        })
        return filteredList
    }
 
    /**
     * retuns the converted element from mapping config if present, null otherwise
     */
    mapElementFromConf(featurePoint: any) {
        if (!this.mapping_config) {
            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.')

        }
 


        let mappingKeys = Object.keys(this.mapping_config.tags)
        // let mappingKeys = (this.mapping_config.tags)
        let featurePointPropertiesKeys = Object.keys(featurePoint.properties)
 
        debugLog('============= keys mappingKeys:', this.mapping_config.tags.length, mappingKeys.length)
        debugLog('============= keys featurePointPropertiesKeys :', featurePoint.properties.length, featurePointPropertiesKeys.length)
 
        let newProperties = Object.create(this.mapping_config.default_properties_of_point)
 
        return;
        // reinit properties of current point
        let basePoint = Object.create(featurePoint)
        basePoint.type = featurePoint.type
        basePoint.geometry = featurePoint.geometry

        // apply new properties if found in mapping config
        featurePointPropertiesKeys.forEach(pointKeyName => {

            this.convertProperty(pointKeyName, mappingKeys, featurePoint, newProperties)

        })

        basePoint.properties = newProperties

        debugLog('basePoint', basePoint)
        return basePoint
    }
 
    convertProperty(pointKeyName, mappingKeys, featurePoint, newProperties) {
        console.log('pointKeyName', pointKeyName)
        if (!mappingKeys.indexOf(pointKeyName) !== -1) {
            // debugLog('found element', pointKeyName, '=>', this.mapping_config[pointKeyName], 'value : ', featurePoint.properties[pointKeyName])
            let convertedValue = ''

            let valueConvertedFromMapping = featurePoint.properties[pointKeyName]
            let typeofValue = typeof valueConvertedFromMapping
            let isStringValue = typeofValue === 'string'
            debugLog('- pointKeyName', pointKeyName)
            debugLog('- valueConvertedFromMapping', valueConvertedFromMapping)
            // debugLog('typeof featurePoint.properties[pointKeyName] === \'string\'', typeofValue)

            let isConfigMappingObject = typeofValue === 'string'

            if (isStringValue) {
                debugLog('-- string value')
                if (this.isBooleanKey(pointKeyName)) {
                    let lowerValue = (valueConvertedFromMapping + '').toLowerCase()
                    debugLog('isBooleanKey: lowerValue', lowerValue)
                    convertedValue = this.truthyValues.indexOf(lowerValue) ? 'yes' : 'no'

                } else {
                    convertedValue = valueConvertedFromMapping
                }
                debugLog('-- convertedValue', convertedValue)
                if (convertedValue) {
                    newProperties[this.mapping_config[pointKeyName]] = convertedValue
                }
            } else if (isConfigMappingObject) {
                let newKey = ''
                let configObject = valueConvertedFromMapping

                if (configObject.key_converted) {
                    newKey = configObject.key_converted
                }

                /**
                 * gestion des valeurs conditionnelles
                 * nous pouvons renseigner une string ou un objet décrivant les transformations à réaliser
                 */
                if (configObject.conditional_values) {
                    let keysConditionnalValues = Object.keys(configObject.conditional_values)
                    let isFoundValue = keysConditionnalValues.indexOf(valueConvertedFromMapping)
                    if (isFoundValue !== -1) {
                        let conditionnalConfig :any = keysConditionnalValues[isFoundValue]

                        if (conditionnalConfig.tags_to_add) {
                            // on peut définir un ensemble de tags à rajouter
                            newProperties.push(...conditionnalConfig.tags_to_add)
                        }
                        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(valueConvertedFromMapping) !== -1) {
                                convertedValue = conditionnalConfig.truthy_value
                            }
                        }
                        if (conditionnalConfig.falsy_value) {
                            if (this.falsyValues.indexOf(valueConvertedFromMapping) !== -1) {
                                convertedValue = conditionnalConfig.falsy_value
                            }
                        }
                        if (conditionnalConfig.transform_function) {
                            // une transformation de la valeur
                            // apply transformation to value
                            convertedValue = conditionnalConfig.transform_function(valueConvertedFromMapping)
                        }
                        // use the value converted
                        else if (conditionnalConfig.value_converted) {
                            convertedValue = conditionnalConfig.value_converted
                        }
                    }
                }

                if (newKey && !configObject.ignore_this_data) {
                    newProperties[newKey] = convertedValue
                }
            }
        }
    }
 
}