<commit_before>export * from './delete';
export * from './insert';

import {merge, applyOperation, compare} from '../functions'
import {Orderer} from '../order'

// type Operation = Insert | Delete
// type OrdersIndex = Array<Operation>
// type OperationsIndex<T> = Array<Orderer<Text<T>>

export class Text {
    order: any
    ordersIndex: any
    operationsIndex: any
    index: number
    constructor(order: any, ordersIndex: any, operationsIndex: any) {
        this.order = order;
        this.ordersIndex = ordersIndex || [];
        this.operationsIndex = operationsIndex || [];

        this.index = this.ordersIndex.findIndex(o => o.equal(order));
        if (-1 === this.index) {
            this.index = this.ordersIndex.push(order) - 1;
        }

        this.operationsIndex[this.index] =
            this.operationsIndex[this.index] || [];
    }

    apply(operation) {
        this.operationsIndex[this.index].push(operation);
    }

    merge(b) {
        const ordersIndexA = this.ordersIndex.slice(0);
        let operationsIndexA = this.operationsIndex.slice(0);

        operationsIndexA = b.operationsIndex.reduce((operationsIndexA, operationsB, orderIndexB) => {
            const orderB = b.ordersIndex[orderIndexB];
            const notFoundInA = -1 === ordersIndexA.findIndex(orderA => orderA.equal(orderB));

            if (notFoundInA) {
                const index = ordersIndexA.push(orderB) - 1;

                operationsIndexA[index] = operationsB;
            }

            return operationsIndexA;
        }, operationsIndexA);

        return new Text(
            merge(this.order, b.order).next(),
            ordersIndexA,
            operationsIndexA
        );
    }

    equal(b) {
        return this.toString() === b.toString();
    }

    reduce(fn, accumulator) {
        return this.ordersIndex.slice(0).sort(compare).reduce((accumulator, order) => {
            const orderIndex = this.ordersIndex.findIndex(o => o.equal(order));

            return this.operationsIndex[orderIndex].reduce((accumulator, operation, index) => {
                return fn(accumulator, operation, order, index);
            }, accumulator);
        }, accumulator);
    }

    toString() {
        return this.reduce((accumulator, operation) => {
            return applyOperation(operation, accumulator);
        }, []).join('');
    }
}<commit_msg>Text.forEach - temporary method<commit_after>export * from './delete';
export * from './insert';

import {merge, applyOperation, compare} from '../functions'
import {Orderer} from '../order'

// type Operation = Insert | Delete
// type OrdersIndex = Array<Operation>
// type OperationsIndex<T> = Array<Orderer<Text<T>>

export class Text {
    order: any
    ordersIndex: any
    operationsIndex: any
    index: number
    constructor(order: any, ordersIndex: any, operationsIndex: any) {
        this.order = order;
        this.ordersIndex = ordersIndex || [];
        this.operationsIndex = operationsIndex || [];

        this.index = this.ordersIndex.findIndex(o => o.equal(order));
        if (-1 === this.index) {
            this.index = this.ordersIndex.push(order) - 1;
        }

        this.operationsIndex[this.index] =
            this.operationsIndex[this.index] || [];
    }

    apply(operation) {
        this.operationsIndex[this.index].push(operation);
    }

    merge(b) {
        const ordersIndexA = this.ordersIndex.slice(0);
        let operationsIndexA = this.operationsIndex.slice(0);

        operationsIndexA = b.operationsIndex.reduce((operationsIndexA, operationsB, orderIndexB) => {
            const orderB = b.ordersIndex[orderIndexB];
            const notFoundInA = -1 === ordersIndexA.findIndex(orderA => orderA.equal(orderB));

            if (notFoundInA) {
                const index = ordersIndexA.push(orderB) - 1;

                operationsIndexA[index] = operationsB;
            }

            return operationsIndexA;
        }, operationsIndexA);

        return new Text(
            merge(this.order, b.order).next(),
            ordersIndexA,
            operationsIndexA
        );
    }

    equal(b) {
        return this.toString() === b.toString();
    }

    reduce(fn, accumulator) {
        return this.ordersIndex.slice(0).sort(compare).reduce((accumulator, order) => {
            const orderIndex = this.ordersIndex.findIndex(o => o.equal(order));

            return this.operationsIndex[orderIndex].reduce((accumulator, operation, index) => {
                return fn(accumulator, operation, order, index);
            }, accumulator);
        }, accumulator);
    }

    forEach(fn) {
        return this.ordersIndex.slice(0).sort(compare).forEach(order => {
            const orderIndex = this.ordersIndex.findIndex(o => o.equal(order));
            const operations = this.operationsIndex[orderIndex];

            fn({order, operations})
        });
    }

    toString() {
        return this.reduce((accumulator, operation) => {
            return applyOperation(operation, accumulator);
        }, []).join('');
    }
}
{'fraction_non_alphanumeric': 0.13157376403387827, 'fraction_numerical': 0.0029545006893834944, 'mean_word_length': 2.349604221635884, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 27, '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': '5129142', 'n_tokens_mistral': 1416, 'n_tokens_neox': 1365, 'n_words': 276}