<commit_before>define(function () {

/*  diff takes two strings, the old content, and the desired content
    it returns the difference between these two strings in the form
    of an 'Operation' (as defined in chainpad.js).

    diff is purely functional.
*/
var diff = function (oldval, newval) {
    // Strings are immutable and have reference equality. I think this test is O(1), so its worth doing.
    if (oldval === newval) {
        return;
    }

    var commonStart = 0;
    while (oldval.charAt(commonStart) === newval.charAt(commonStart)) {
        commonStart++;
    }

    var commonEnd = 0;
    while (oldval.charAt(oldval.length - 1 - commonEnd) === newval.charAt(newval.length - 1 - commonEnd) &&
        commonEnd + commonStart < oldval.length && commonEnd + commonStart < newval.length) {
        commonEnd++;
    }

    var toRemove;
    var toInsert;

    /*  throw some assertions in here before dropping patches into the realtime */
    if (oldval.length !== commonStart + commonEnd) {
        toRemove = oldval.length - commonStart - commonEnd;
    }
    if (newval.length !== commonStart + commonEnd) {
        toInsert = newval.slice(commonStart, newval.length - commonEnd);
    }

    return {
        type: 'Operation',
        offset: commonStart,
        toInsert: toInsert,
        toRemove: toRemove
    };
};

/*  patch accepts a realtime facade and an operation (which might be falsey)
    it applies the operation to the realtime as components (remove/insert)

    patch has no return value, and operates solely through side effects on
    the realtime facade.
*/
var patch = function (ctx, op) {
    if (!op) { return; }
    if (op.toRemove) { ctx.remove(op.offset, op.toRemove); }
    if (op.toInsert) { ctx.insert(op.offset, op.toInsert); }
};

/*  log accepts a string and an operation, and prints an object to the console
    the object will display the content which is to be removed, and the content
    which will be inserted in its place.

    log is useful for debugging, but can otherwise be disabled.
*/
var log = function (text, op) {
    if (!op) { return; }
    console.log({
        insert: op.toInsert,
        remove: text.slice(op.offset, op.offset + op.toRemove)
    });
};

/* applyChange takes:
    ctx: the context (aka the realtime)
    oldval: the old value
    newval: the new value

    it performs a diff on the two values, and generates patches
    which are then passed into `ctx.remove` and `ctx.insert`.

    Due to its reliance on patch, applyChange has side effects on the supplied
    realtime facade.
*/
var applyChange = function(ctx, oldval, newval) {
    var op = diff(oldval, newval);
    // log(oldval, op)
    patch(ctx, op);
};

var create = function(config) {
    var ctx = config.realtime;

    // initial state will always fail the !== check in genop.
    // because nothing will equal this object
    var content = {};

    // *** remote -> local changes
    ctx.onPatch(function(pos, length) {
        content = ctx.getUserDoc();
    });

    // propogate()
    return function (newContent) {
        if (newContent !== content) {
            applyChange(ctx, ctx.getUserDoc(), newContent);
            if (ctx.getUserDoc() !== newContent) {
                console.log("Expected that: `ctx.getUserDoc() === newContent`!");
            }
            return true;
        }
        return false;
    };
};

return {
    create: create, // create a TextPatcher object
    diff: diff, // diff two strings
    patch: patch, // apply an operation to a chainpad's realtime facade
    log: log, // print the components of an operation
    applyChange: applyChange // a convenient wrapper around diff/log/patch
};
});
<commit_msg>Add switchable logging to TextPatcher.js<commit_after>define(function () {

/*  diff takes two strings, the old content, and the desired content
    it returns the difference between these two strings in the form
    of an 'Operation' (as defined in chainpad.js).

    diff is purely functional.
*/
var diff = function (oldval, newval) {
    // Strings are immutable and have reference equality. I think this test is O(1), so its worth doing.
    if (oldval === newval) {
        return;
    }

    var commonStart = 0;
    while (oldval.charAt(commonStart) === newval.charAt(commonStart)) {
        commonStart++;
    }

    var commonEnd = 0;
    while (oldval.charAt(oldval.length - 1 - commonEnd) === newval.charAt(newval.length - 1 - commonEnd) &&
        commonEnd + commonStart < oldval.length && commonEnd + commonStart < newval.length) {
        commonEnd++;
    }

    var toRemove;
    var toInsert;

    /*  throw some assertions in here before dropping patches into the realtime */
    if (oldval.length !== commonStart + commonEnd) {
        toRemove = oldval.length - commonStart - commonEnd;
    }
    if (newval.length !== commonStart + commonEnd) {
        toInsert = newval.slice(commonStart, newval.length - commonEnd);
    }

    return {
        type: 'Operation',
        offset: commonStart,
        toInsert: toInsert,
        toRemove: toRemove
    };
};

/*  patch accepts a realtime facade and an operation (which might be falsey)
    it applies the operation to the realtime as components (remove/insert)

    patch has no return value, and operates solely through side effects on
    the realtime facade.
*/
var patch = function (ctx, op) {
    if (!op) { return; }
    if (op.toRemove) { ctx.remove(op.offset, op.toRemove); }
    if (op.toInsert) { ctx.insert(op.offset, op.toInsert); }
};

/*  log accepts a string and an operation, and prints an object to the console
    the object will display the content which is to be removed, and the content
    which will be inserted in its place.

    log is useful for debugging, but can otherwise be disabled.
*/
var log = function (text, op) {
    if (!op) { return; }
    console.log({
        insert: op.toInsert,
        remove: text.slice(op.offset, op.offset + op.toRemove)
    });
};

/* applyChange takes:
    ctx: the context (aka the realtime)
    oldval: the old value
    newval: the new value

    it performs a diff on the two values, and generates patches
    which are then passed into `ctx.remove` and `ctx.insert`.

    Due to its reliance on patch, applyChange has side effects on the supplied
    realtime facade.
*/
var applyChange = function(ctx, oldval, newval, logging) {
    var op = diff(oldval, newval);
    if (logging) { log(oldval, op) }
    patch(ctx, op);
};

var create = function(config) {
    var ctx = config.realtime;
    var logging = config.logging;

    // initial state will always fail the !== check in genop.
    // because nothing will equal this object
    var content = {};

    // *** remote -> local changes
    ctx.onPatch(function(pos, length) {
        content = ctx.getUserDoc();
    });

    // propogate()
    return function (newContent) {
        if (newContent !== content) {
            applyChange(ctx, ctx.getUserDoc(), newContent, logging);
            if (ctx.getUserDoc() !== newContent) {
                console.log("Expected that: `ctx.getUserDoc() === newContent`!");
            }
            return true;
        }
        return false;
    };
};

return {
    create: create, // create a TextPatcher object
    diff: diff, // diff two strings
    patch: patch, // apply an operation to a chainpad's realtime facade
    log: log, // print the components of an operation
    applyChange: applyChange // a convenient wrapper around diff/log/patch
};
});

{'fraction_non_alphanumeric': 0.10635745701719312, 'fraction_numerical': 0.0013328002132480341, 'mean_word_length': 2.6076923076923078, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '326908', 'n_tokens_mistral': 2176, 'n_tokens_neox': 2131, 'n_words': 787}