Project

cevennes

0.0
A long-lived project that still receives updates
Diffs CSVs by lines, focusing on a single ID
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.6

Runtime

~> 1.2
>= 0
 Project Readme

cevennes

tests Gem Version

Diffs CSVs by lines, focusing on a single ID

usage

Given two CSV strings and an identifier name (a column name), cevennes may compute a diff:

require 'cevennes'

cvs0 = %{
  id,name,age
  0,John,33
  1,Jean-Baptiste,43
  3,Luke,21
}.strip + "\n"
cvs1 = %{
  id,name,age
  0,John,33
  1,Jean-Baptiste,44
  4,Matthew,20
}.strip + "\n"

d = Cevennes.diff('id', cvs0, cvs1)

#d = Cevennes.diff('id', cvs0, cvs1, ignore_key_case: true)
  # when the key case should be ignored ("Id" == "id")

d will yield:

[
  [ 'keys', 1, [ 'id', 'name', 'age' ],
            1, [ 'id', 'name', 'age' ] ],
  [ 'stats',
    { '=' => 1, '!' => 1, '-' => 1, '+' => 1,
      'l0' => 3, 'l1' => 3 } ],
  [ '=',  2, [ '0', 'John', '33'],
          2, nil ],
  [ '!',  3, [ '1', 'Jean-Baptiste', '43' ],
          3, [ '1', 'Jean-Baptiste', '44' ] ],
  [ '-',  4, [ '3', 'Luke', '21'],
         -1, nil ],
  [ '+', -1, nil,
          4, [ '4', 'Matthew', '20' ] ]
]

It's a list where the first entry is a recap of the keys used in the old and the new CSV strings (the integer is the line number (starting at 1) where the keys where found.

The second entry is a summary of the changes, altered ! line count, removed - line count, added + line count, old length l0, new length l1, and unchanged = line count.

The remaining entries are the (non-)changes themselves, from line 1 to the end.

Since 1.3.0, Cevennes accepts CSV strings and parsed CSV (array of arrays).

drop_equals: true

require 'cevennes'

cvs0 = %{
  id,name,age
  0,John,33
  1,Jean-Baptiste,43
  3,Luke,21
}.strip + "\n"
cvs1 = %{
  id,name,age
  0,John,33
  1,Jean-Baptiste,44
  4,Matthew,20
}.strip + "\n"

d = Cevennes.diff('id', cvs0, cvs1, drop_equals: true)
  # ==>
  [
    [ 'keys', 1, [ 'id', 'name', 'age' ],
              1, [ 'id', 'name', 'age' ] ],
    [ 'stats',
      { '=' => 1, '!' => 1, '-' => 1, '+' => 1,
        'l0' => 3, 'l1' => 3 } ],
    [ '!',  3, [ '1', 'Jean-Baptiste', '43' ],
            3, [ '1', 'Jean-Baptiste', '44' ] ],
    [ '-',  4, [ '3', 'Luke', '21'],
           -1, nil ],
    [ '+', -1, nil,
            4, [ '4', 'Matthew', '20' ] ]
  ]
    #
    # the "=" entries are not included

LICENSE

MIT, see LICENSE.txt