csv-sort3.1.0

Sorts double-entry bookkeeping CSV coming from internet banking

§ Quick Take

import { strict as assert } from "assert";
import cSort from "csv-sort";

// Sorts double-entry bookkeeping CSV's - bank statements for example
// see https://en.wikipedia.org/wiki/Double-entry_bookkeeping

assert.deepEqual(
  cSort(`Acc Number,Description,Debit Amount,Credit Amount,Balance,
123456,Client #1 payment,,1000,1940
123456,Bought carpet,30,,950
123456,Bought table,10,,940
123456,Bought pens,10,,1000
123456,Bought chairs,20,,980
`),
  {
    res: [
      [
        "Acc Number",
        "Description",
        "Debit Amount",
        "Credit Amount",
        "Balance",
      ],
      [
        "123456",
        "Client #1 payment",
        "",
        "1000",
        "1940",
      ],
      ["123456", "Bought table", "10", "", "940"],
      ["123456", "Bought carpet", "30", "", "950"],
      ["123456", "Bought chairs", "20", "", "980"],
      ["123456", "Bought pens", "10", "", "1000"],
    ],
    msgContent: null,
    msgType: null,
  }
);
// you'll have to join elements and lines from the array yourself

§ Purpose

  • Sorts rows in correct order that follows the double-entry format.
  • Trims the empty columns and rows (so-called 2D-Trim^).
2D trim of a CSV contents

In later releases, we would like to be able to recognise and fix any offset columns caused by misinterpreted commas as values.

^ 1D-Trim would be trim of a string. 3D-Trim would be some sort of spatial data trim.

§ API - Input

cSort(str)

In other words, it's a function which takes one input argument, string (CSV contents).

§ API - Output

  • Output - plain object:
output objectTypeDescription
resArrayArray of arrays, each containing a column's value.
msgContentStringThis application outputs the messages here.
msgTypeStringCan be either alert or info. That's similar to an icon on the hypothetical UI.

If the input is anything else than a string, it will throw. If the input is an empty string, the output object's res key will be equal to [['']].

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 csv-sort-cli 1.9.84
Command line app to sort double-entry CSVs coming from internet banking statements
📦 csv-split-easy 3.1.0
Splits the CSV string into array of arrays, each representing a row of columns