1.54.0 • Published 1 year ago

@wmfs/smithereens v1.54.0

Weekly downloads
117
License
MIT
Repository
github
Last release
1 year ago

smithereens

Tymly Package npm (scoped) CircleCI codecov CodeFactor Dependabot badge Commitizen friendly JavaScript Style Guide license

Smash CSV files into more manageable files based on column values

Install

$ npm install smithereens --save

Usage

const smithereens = require('smithereens')

smithereens(
  [
    '/some/input/csv/files/people.csv'

    // people.csv:
    //
    // personNo,firstName,LastName,personType,action
    // 10,"Lisa","Simpson","c","u"
    // 20,"Homer","Simpson","a","u"
    // 30,"Bart","Simpson","c","d"
    // 40,"Marge","Simpson","a","d"
    // 50,"Maggie","Simpson","c","x"
    // 60,"Grampa","Simpson","x","u"
    // 70,"Milhouse","Van Houten","c","u"

  ],
  {

    outputDirRootPath: '/some/output/dir',

    parser: {
      quote: '"',
      delimiter: ',',
      newline: '\n',
      skipFirstLine: true,
      trimWhitespace: true
    },

    dirSplits: [
      {
        columnIndex: 3,
        valueToDirMap: {
          'c': 'children',
          'a': 'adults'
        }
      }
    ],

    fileSplits: {
      columnIndex: 4,
      valueToFileMap: {
        'u': {
          filename: 'changes',
          outputColumns: [
            {name: 'person_no', columnIndex: 0},
            {name: 'first_name', columnIndex: 1},
            {name: 'last_name', columnIndex: 2}
          ]
        },
        'd': {
          filename: 'deletes',
          outputColumns: [
            {name: 'person_no', columnIndex: 0}
          ]
        }
      }
    }
  },

  function (err, manifest) {

    // File output
    // -----------
    //   /some/output/dir
    //   ./adults
    //     changes.csv:
    //       person_no,first_name,last_name
    //       20,Homer,Simpson
    //     deletes.csv:
    //       person_no
    //       40
    //   ./children
    //     changes.csv:
    //       person_no,first_name,last_name
    //       10,Lisa,Simpson
    //       70,Milhouse,Van Houten
    //     deletes.csv:
    //       person_no
    //       30
    //     unknown.csv:
    //       50,Maggie,Simpson,c,x
    //   ./unknown
    //     changes.csv:
    //       person_no,first_name,last_name
    //       60,Grampa,Simpson

  }
)

smithereens(sourceFilePaths, options, callback)

ArgTypeDescription
sourceFilePathsstring | [string]A string or an array of strings identifying one or more files. Uses glob so /some/dir/*.csv style patterns are supported, as is directory recursion via /some/dir/**/*.csv
optionsobjectAn object configuring how output should be produced. See Options for more information.
callbackfunctionTo be of the form function(err, manifest). Manifest contains a summary of the output files produced.

Options

PropertyTypeDescription
outputDirRootPathstringAn absolute directory path where to write output to. All missing directories will be created.
parserobjectAn parser object for configuring how input CSV files should be parsed.
dirSplits[object]An array of of dirSplit objects
fileSplitsobjectA fileSplit object

parser object

Configures how to parse incoming CSV lines. Uses csv-streamify under the bonnet.

PropertyTypeDescription
skipFirstLinebooleanShould the first line of each file be ignored? Set to true if files include a header line, for example.
delimiterstringComma, semicolon, whatever - defaults to comma.
newlinestringNewline character (use \r\n for CRLF files).
quotestringWhat's considered a quote.
emptystringEmpty fields are replaced by this value.

dirSplit object

Smithereens can break CSV files across a nested set of directories based on values defined in each line.

PropertyTypeDescription
columnIndexintegerEach line of each CSV file will be parsed into an array of strings. This value identifies which value to split on.
valueToDirMapobjectA simple mapping of an expected string value (as identified by columnIndex) and the directory name that this line should be routed to.

fileSplit object

In a similar way, Smithereens can route lines to different files, based on the contents of a parsed CSV column.

PropertyTypeDescription
columnIndexintegerIdentifies which of the parsed string values from each CSV line should be used to determine a filename that a row should be routed to.
valueToFileMapobjectA key/value map where key is a string value that is expected via columnIndex and value is a file object.

file object

Defines which filename a CSV row should be routed to, along with some output-formatting configuration.

PropertyTypeDescription
filenamestringThe filename which a row should be routed to. All output files will be in CSV format. Note that the .csv extension is added automatically, so don't include it here.
outputColumns[object]An array of outputColumn objects - each defining a column that should appear in the output file.

outputColumn object

Defines the values for each column in the output.

PropertyTypeDescription
namestringThe name of the column, used in the first line of the CSV output
columnIndexintegerIdentifies the column in the corresponding row of the parsed incoming CSV array to copy to the output. If columnIndex is given any type parameter is ignored.
typestringDescribes other output - hash, uuid, constant. hash generates a hash value from the contents of the corresponding input row, uuid outputs a unique id, constant output a fix value, given by the value property.
valuenumber or stringA fixed value to put output when the type property is constant.

Testing

$ npm test

License

MIT

1.53.0

1 year ago

1.54.0

1 year ago

1.51.0

1 year ago

1.49.0

1 year ago

1.50.0

1 year ago

1.52.0

1 year ago

1.48.0

2 years ago

1.47.0

3 years ago

1.46.0

3 years ago

1.45.0

3 years ago

1.44.0

3 years ago

1.43.0

3 years ago

1.42.0

3 years ago

1.41.0

4 years ago

1.40.0

4 years ago

1.39.0

4 years ago

1.38.0

4 years ago

1.37.0

4 years ago

1.36.0

4 years ago

1.35.0

4 years ago

1.34.0

4 years ago

1.33.1

4 years ago

1.33.0

4 years ago

1.32.0

4 years ago

1.31.0

4 years ago

1.30.0

4 years ago

1.29.0

4 years ago

1.28.0

4 years ago

1.27.0

4 years ago

1.25.0

4 years ago

1.26.0

4 years ago

1.24.0

4 years ago

1.23.0

5 years ago

1.22.0

5 years ago

1.21.0

5 years ago

1.20.0

5 years ago

1.19.0

5 years ago

1.18.0

5 years ago

1.17.0

5 years ago

1.16.0

5 years ago

1.15.0

5 years ago

1.14.0

5 years ago

1.13.0

5 years ago

1.12.0

6 years ago

1.11.0

6 years ago

1.10.0

6 years ago

1.9.0

6 years ago

1.8.0

6 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago