<commit_before>import {propTypes} from "../props.js";

//Export identifier transform properties
export const identifierTransform = {
    "transform": function (context, data, props) {
        return data.map(function (datum, index) {
            return Object.assign(datum, {
                [props.as]: index  //Add the index as an identifier of this datum
            });
        });
    },
    "props": {
        "as": propTypes.string()
    }
};
<commit_msg>transforms: added groupby option to identifier transform<commit_after>import {isString} from "../util.js";

//Default transform props
let defaultProps = {
    "groupby": null,
    "as": "index"
};

//Export identifier transform properties
export const identifierTransform = {
    "transform": function (context, data, props) {
        let groupby = (typeof props.groupby === "string") ? props.groupby.trim() : null; 
        let as = isString(props.as) ? props.as : defaultProps.as;
        let groups = [];
        //Build groups
        if (groupby !== null) {
            let maps = {}; //Groups mappings
            data.forEach(function (datum, index) {
                let key = datum[groupby];
                //Check if this group is not defined
                if (typeof maps[key] === "undefined") {
                    groups.push([]);
                    maps[key] = groups.length - 1;
                }
                //Insert this datum
                groups[maps[key]].push(datum);
            });
        }
        //No groupby property provided --> use only one group
        else {
            groups = [data]; //Register only one group with all elements
        }
        //Add the index for all datum elements
        let newData = []; //New data list
        groups.forEach(function (group) {
            return group.forEach(function (datum, index) {
                return newData.push(Object.assign({}, datum, {
                    [as]: index //Save the index of the datum in the list
                }));
            });
        });
        //Return the new data object
        return newData;
    },
    "props": defaultProps,
    "sourceProps": []
};

{'fraction_non_alphanumeric': 0.12365842277181521, 'fraction_numerical': 0.0004666355576294914, 'mean_word_length': 1.7628865979381443, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3194221', 'n_tokens_mistral': 565, 'n_tokens_neox': 550, 'n_words': 175}