Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could we make the text diff even more concise? #4

Open
simonbrunel opened this issue May 3, 2022 · 1 comment
Open

Could we make the text diff even more concise? #4

simonbrunel opened this issue May 3, 2022 · 1 comment

Comments

@simonbrunel
Copy link

Hi @icflorescu.

As discussions are not enabled for this repository, I hope you don't mind I create an issue to discuss this suggestion. It's not a huge optimization but since the goal of this library is to reduce the size of the output diff, wouldn't it be more efficient to flatten the output array and apply the following logic:

  • DELETE === a negative number
  • EQUAL === a positive number
  • INSERT === a string
// fast-diff: 86 chars
[[0,"The "],[-1,"sleepy"],[1,"quick"],[0," brown fox"],[1," jumps over the lazy dog"]]

// textdiff-create: 64 chars
[[0,4],[-1,6],[1,"quick"],[0,10],[1," jumps over the lazy dog"]]

// proposal: 44 chars
[4,-6,"quick",10," jumps over the lazy dog"]
@metabench
Copy link

That's a good idea. I wrote this code to map to and from the format you specify:

delta = delta.map(x => {
    const [i_op, value] = x;
    if (i_op === -1) {
        return value * -1;
    }
    if (i_op === 0) {
        return value;
    }
    if (i_op === 1) {
        return value;
    }
})

delta = delta.map(x => {
    if (typeof x === 'string') {
        return [1, x]
    } else {
        if (x < 0) {
            return [-1, -1 * x]
        } else {
            return [0, x]
        }
    }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants