keys/sanitize.js

/**
 * Historically we've had issues with differing key names on url params and
 * objects e.g agreement_type, agreement, agreementType, agreement_type_code,
 * agreement_code and so on. This is to keep key names consistent
 * @module
 * @since 0.0.25
 */

/**
 * Create the map
 * @private
 * @param {struct} arr_key_config Array of desired keys and associated unwanted keys
 * @returns {map}
 */
const buildMap = (arr_key_config) => {
    const map = new Map();

    // loop over struct
    arr_key_config.forEach((skv_key_config) => {
        // the key we want to be using
        const target_key = skv_key_config.desired_key;
        const incorrect_keys = skv_key_config.arr_incorrect_keys;

        incorrect_keys.forEach((unwanted_key) => {
            // point the unwanted key to the target key
            map.set(unwanted_key, target_key);
        });
    });

    return map;
};

/**
 * Create map of FQT terms
 * @returns {map}
 */
const getFQTmap = () => {
    const arr_keys = [
        {
            desired_key: 'agreement_type_code',
            arr_incorrect_keys: [
                'agreement_type',
                'agreement_code',
                'agreementType',
                'agreementCode',
                'agreement',
            ],
        },
        {
            desired_key: 'initial_payment',
            arr_incorrect_keys: [
                'deposit',
                'upfront_payment',
                'upfront_payments',
            ],
        },
        {
            desired_key: 'is_maintenance_inc',
            arr_incorrect_keys: [
                'maintenance',
            ],
        },
        {
            desired_key: 'mileage',
            arr_incorrect_keys: [
                'miles',
            ],
        },

    ];

    return buildMap(arr_keys);
};

/**
 * Combine together all the maps in to one single map
 * @private
 * @returns {map}
 */
const getCompleteMap = () => getFQTmap(); // {
// const temp_map = new Map();

// TODO: merging maps no longer seem to work, the merged map only has undefined
// keys
// Merge two maps. The last repeated key wins.
// Spread operator essentially converts a Map to an Array
// const merged_map = new Map([...temp_map, ...getFQTmap()]);
//     return getFQTmap();
// };

/**
 * Pass in a struct of key value pairs and return a manipulated struct to use
 * desired keys
 * @param {struct} skv_data
 * @returns {struct}
 */
const sanitizeStruct = (skv_data) => {
    // TODO: in future pass in which maps to use
    const map = getCompleteMap();
    const skv_return = {};

    // loop over each key in struct recursively
    Object.keys(skv_data).forEach((key) => {
        const retrieved_key = map.get(key);

        // if retrieved key is truthy we've got a new key to use, otherwise
        // its undefined and not found so use the existing key
        let key_to_use = retrieved_key || key;

        // going from CF to JS can give uppercase keys
        key_to_use = key_to_use.toLowerCase();

        // put the original data in the struct to be returned
        skv_return[key_to_use] = skv_data[key];

        // if we have more keys check those aswell
        if (
            typeof skv_return[key_to_use] === 'object'
            && Object.keys(skv_return[key_to_use]).length
        ) {
            // recursively call the function
            skv_return[key_to_use] = sanitizeStruct(skv_return[key_to_use]);
        }
    });

    return skv_return;
};

export {
    getCompleteMap,
    getFQTmap,
    sanitizeStruct,
};