Quick Take

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

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

assert.deepEqual(
  sort(`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

sort(str)

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

API - Output

  • Output - plain object:
output object Type Description
res Array Array of arrays, each containing a column's value.
msgContent String This application outputs the messages here.
msgType String Can 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 [['']].

Changelog

See it in the monorepo opens in a new tab, on GitHub.

Contributing

To report bugs or request features or assistance, raise an issue on GitHub opens in a new tab.

Any code contributions welcome! All Pull Requests will be dealt promptly.

Licence

MIT opens in a new tab

Copyright © 2010–2021 Roy Revelt and other contributors

Related packages:

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