daff

repository·master·Indexed 21 days ago

https://github.com/paulfitz/daff

A library and CLI tool for comparing tables in CSV, TSV, JSON, and SQLite formats. It provides row and column-aware diffing and patching, supporting 3-way merges and integration with git. Available for Node.js, Python, Ruby, PHP, and R.

Tokens
1.4K
Snippets
8
Records
8
Agent score
26%

What's inside daff

  1. Integrate daff with git

    master

    You can configure git to use daff as a smart diff and merge handler for *.csv files. This allows git to understand changes at the row and column level rather than just line-by-line. Run the following command to get setup instructions:

    daff git
    daff git csv
  2. Install the daff library

    master

    Install daff using the package manager for your preferred language:

    • Node.js/JavaScript: npm install daff -g
    • Python: pip install daff
    • Ruby: gem install daff
    • PHP: composer require paulfitz/daff-php
    • R: install.packages('daff') (via Edwin de Jonge's wrapper)
    npm install daff -g  # node/javascript
    pip install daff     # python
    gem install daff     # ruby
    composer require paulfitz/daff-php  # php
    install.packages('daff') # R wrapper by Edwin de Jonge
  3. Use daff via CDN in the browser

    master

    To use daff directly in a web page without a build step, include the minified library via JSDelivr:

    <script src="https://cdn.jsdelivr.net/npm/daff@latest/lib/daff.min.js"></script>
  4. Compute a table diff in JavaScript

    master

    To use daff as a library in JavaScript (Node.js or Browser), follow these steps:

    1. Wrap your raw data arrays in daff.TableView objects.
    2. Use daff.compareTables(table1, table2).align() to compute the alignment.
    3. Use daff.TableDiff with CompareFlags to generate the diff data.
    4. (Optional) Use daff.DiffRender to convert the diff to HTML.
    var daff = require('daff');
    
    const data1 = [['Country','Capital'],['Ireland','Dublin']];
    const data2 = [['Country','Code','Capital'],['Ireland','ie','Dublin']];
    
    const table1 = new daff.TableView(data1);
    const table2 = new daff.TableView(data2);
    
    // 1. Align tables
    const alignment = daff.compareTables(table1, table2).align();
    
    // 2. Create diff output table
    const data_diff = [];
    const table_diff = new daff.TableView(data_diff);
    
    // 3. Generate diff
    const flags = new daff.CompareFlags();
    const highlighter = new daff.TableDiff(alignment, flags);
    highlighter.hilite(table_diff);
    
    // 4. Render to HTML
    const diff2html = new daff.DiffRender();
    diff2html.render(table_diff);
    const html = diff2html.html();
  5. Apply a patch using HighlightPatch

    master

    In JavaScript, once you have a diff (in daff format), you can apply it to a table using the daff.HighlightPatch class to transform the original table into the target table.

    const patcher = new daff.HighlightPatch(table1, table_diff);
    patcher.apply();
    // table1 is now updated to match table2
  6. Configure daff diff flags

    master

    When using the daff diff command, you can fine-tune the comparison with several flags:

    • --act ACT: Show only specific changes (update, insert, delete, column).
    • --all: Do not prune unchanged rows or columns.
    • --all-rows: Do not prune unchanged rows.
    • --all-columns: Do not prune unchanged columns.
    • --id <column>: Specify a primary key column (can be repeated for multi-column keys).
    • --ignore <column>: Specify a column to ignore (can be repeated).
    • --input-format [csv|tsv|ssv|psv|json|sqlite]: Set the expected input format.
    • --output-format [csv|tsv|ssv|psv|json|copy|html]: Set the output format.
    • --ordered: Assume row order is meaningful (default for CSV).
    • --unordered: Assume row order is meaningless (default for JSON).
    • -i / --ignore-case: Ignore differences in case.
    • -w / --ignore-whitespace: Ignore leading/trailing whitespace.
    daff diff --act update --id Country --ignore Code a.csv b.csv
  7. Configure daff render flags

    master

    When using the daff render command to visualize a diff file:

    • --output OUTPUT.html: Specify the output file.
    • --css CSS.css: Generate a suitable CSS file to accompany the HTML.
    • --fragment: Generate just an HTML fragment instead of a full page.
    • --plain: Do not use fancy UTF-8 characters for arrows.
    • --unquote: Do not quote HTML characters in HTML diffs.
    daff render --output diff.html --css style.css diff.csv
  8. Use the daff CLI program

    master

    The daff utility (available as daff, daff.py, or daff.rb) can produce and apply tabular diffs.

    Common Commands:

    • daff a.csv b.csv: Compare two files.
    • daff [--output OUTPUT.html] a.csv b.csv: Generate an HTML diff.
    • daff patch [--inplace] a.csv patch.csv: Apply a patch to a file (use --inplace to modify the file directly).
    • daff merge [--inplace] parent.csv a.csv b.csv: Perform a 3-way merge.
    • daff git: Get instructions for integrating daff with git.
    $ daff a.csv b.csv
    $ daff --output OUTPUT.html a.csv b.csv
    $ daff patch --inplace a.csv patch.csv