<commit_before>
'use strict';

var Pager = require('./pager');
var EventEmitter = require("events").EventEmitter;


/**
 * TODO: should this be named recordset instead?
 *
 *
 * @param config
 * @param dsDef
 * @constructor
 */
var DataSource = function(config,dsDef) {
    this.records = dsDef.data;
};


DataSource.prototype = EventEmitter.prototype;
DataSource.prototype.constructor = DataSource;

DataSource.prototype.recordAtIndex = function(index) {
    return this.records[index];
};

/**
 * If key is defined then this data can be accessed using the key
 *
 * FIXME: cell should use this.
 *
 * @param key
 */
DataSource.prototype.getRecord = function(index,property,path,record) {
    alert("get not implemented");
};

DataSource.prototype.add = function(record) {
    this.records.push(record);
    this.emit("recordAdded",record);
};

DataSource.prototype.length = function() {
    return this.records.length;
};

/**
 * Maps the actual page
 * The mapper function gets the record, currentIndex and actual index
 */
DataSource.prototype.map = function(pageState,mapper) {

    if ( !pageState ) {
        return this.records.map(mapper);
    }


    var result = [];
    var counter = 0;

    for ( var i = pageState.startIdx; i < pageState.endIdx; i++ ) {

        result.push(mapper(this.records[i],counter++,i));
    }

    return result;

};

/**
 * Up
 *
 *
 * @param recordIdx
 * @param newValue
 */
DataSource.prototype.updateRecord = function(recordIdx,property,newValue,config) {

    var record = this.records[recordIdx];
    var path = config.path ? path : property;
    var setter = config.setter ? config.setter : function() {
                path.split(".").reduce(function(prev,current,index,arr) {
                    if ( index === (arr.length - 1) ) {
                        //we are at the end
                        prev[current] = newValue;
                    } else {
                        return prev[current];
                    }
                },record);
            } ;
    setter.call(record,newValue,property,config);
    //FIXME, we should get current value and pass as old value
    this.emit("recordUpdated",record,recordIdx,property,newValue);
};

<commit_msg>enabled setter to be a string attribute<commit_after>
'use strict';

var Pager = require('./pager');
var EventEmitter = require("events").EventEmitter;


/**
 * TODO: should this be named recordset instead?
 *
 *
 * @param config
 * @param dsDef
 * @constructor
 */
var DataSource = function(config,dsDef) {
    this.records = dsDef.data;
};


DataSource.prototype = EventEmitter.prototype;
DataSource.prototype.constructor = DataSource;

DataSource.prototype.recordAtIndex = function(index) {
    return this.records[index];
};

/**
 * If key is defined then this data can be accessed using the key
 *
 * FIXME: cell should use this.
 *
 * @param key
 */
DataSource.prototype.getRecord = function(index,property,path,record) {
    alert("get not implemented");
};

DataSource.prototype.add = function(record) {
    this.records.push(record);
    this.emit("recordAdded",record);
};

DataSource.prototype.length = function() {
    return this.records.length;
};

/**
 * Maps the actual page
 * The mapper function gets the record, currentIndex and actual index
 */
DataSource.prototype.map = function(pageState,mapper) {

    if ( !pageState ) {
        return this.records.map(mapper);
    }


    var result = [];
    var counter = 0;

    for ( var i = pageState.startIdx; i < pageState.endIdx; i++ ) {

        result.push(mapper(this.records[i],counter++,i));
    }

    return result;

};

/**
 * Up
 *
 *
 * @param recordIdx
 * @param newValue
 */
DataSource.prototype.updateRecord = function(recordIdx,property,newValue,config) {

    var record = this.records[recordIdx];
    var path = config.path ? path : property;
    var setter = config.setter ?

        //setter can be a string or an actual function derp
        function(newValue,property,config) {
            var thesetter = config.setter;
            if ( typeof(config.setter) === 'string' ) {
                thesetter = record[config.setter];
            }
            thesetter(newValue,property,config);

        }:

        function() {
                path.split(".").reduce(function(prev,current,index,arr) {
                    if ( index === (arr.length - 1) ) {
                        //we are at the end
                        prev[current] = newValue;
                    } else {
                        return prev[current];
                    }
                },record);
        } ;
    setter.call(record,newValue,property,config);
    //FIXME, we should get current value and pass as old value
    this.emit("recordUpdated",record,recordIdx,property,newValue);
};


{'fraction_non_alphanumeric': 0.12835570469798657, 'fraction_numerical': 0.0008389261744966443, 'mean_word_length': 2.561613144137416, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '5920171', 'n_tokens_mistral': 1392, 'n_tokens_neox': 1347, 'n_words': 327}