1.0.9 • Published 5 years ago

csv-filter-sort v1.0.9

Weekly downloads
10
License
ISC
Repository
github
Last release
5 years ago

csv-filter-sort

npm version Codecov CircleCI

Filter CSV rows using criteria for column values and save the result as a new CSV.

Getting Started

To use the csv-filter-sort library, just import it into your project:

const csvFilterSort = require('csv-filter-sort');

In the example above, the library is imported as csvFilterSort through which you can access the library's filter and sort (coming soon!) functions.

Reference

filter(csv, filterOptions, callback)

Parameters:

NameType
csvString
filterOptionsObject
callbackFunction

csv: A comma seperated list of values with each comma representing a new column. Use \n to represent the end of a row.

'Address Number,Address Street,City,State,Zip\n11111,De Anza Blvd,Cupertino,CA,95014\n'22222,Main St,Chicago,IL,60605'

filterOptions: An object containing options to be included within the filter configuration. Valid options include hasHeader (BOOLEAN), columnToFilter (STRING or INTEGER), filterCriteria (STRING or INTEGER), and filterType (STRING).

{
    hasHeader: true,
    columnToFilter: 'City',
    filterCriteria: 'Chicago',
    filterType: 'EXACT'
}

OR

{
    hasHeader: false,
    columnToFilter: 2,
    filterCriteria: 10,
    filterType: 'LESS'
}
NameTypeDescriptionRequiredDefault
hasHeaderBooleanSpecfies whether or not the first CSV row is a header.falsefalse
columnToFilterString or integerThe column name or number to filter by.true
filterCriteriaString or integerThe criteria to filter rows by. If string is provided, CSV will be filtered by character length of the value.true
filterTypeStringOptions include EXACT, LESS, and GREATER.false'EXACT'

sort(csv, sortOptions, callback)

Parameters:

NameType
csvString
sortOptionsObject
callbackFunction

csv: A comma seperated list of values with each comma representing a new column. Use \n to represent the end of a row.

'Address Number,Address Street,City,State,Zip\n11111,De Anza Blvd,Cupertino,CA,95014\n'22222,Main St,Chicago,IL,60605'

sortOptions: An object containing options to be included within the sort configuration. Valid options include hasHeader (BOOLEAN), sortByColumn (STRING or INTEGER), orderBy, (STRING).

{
    hasHeader: true,
    sortByColumn: 'Zip',
    orderBy: 'ASC'
}

OR

{
    hasHeader: false,
    sortByColumn: 4,
    orderBy: 'DESC'
}
NameTypeDescriptionRequiredDefault
hasHeaderBooleanSpecfies whether or not the first CSV row is a header.falsefalse
sortByColumnString or integerThe column name or number to sort by.true
orderByStringOptions include ASC and DESC for ascending and descending respectively.false'ASC'

Examples

const csvFilterSort = require('csv-filter-sort');

const csv = 'Address Number,Address Street,City,State,Zip\n' +
            '11111,De Anza Blvd,Cupertino,CA,95014\n' +
            '22222,Main St,Chicago,IL,60605\n' +
            '22211,Michigan Ave,Chicago,IL,60607\n' +
            '33333,Woodward Ave,Detroit,MI,48048\n' +
            '44444,Mission St,San Francisco,CA,95001';

const filterOptions = {
    hasHeader: true,
    columnToFilter: 'City',
    filterCriteria: 'Chicago',
    filterType: 'EXACT'
}

const sortOptions = {
    hasHeader: true,
    sortByColumn: 4,
    orderBy: 'DESC'
}

csvFilterSort.filter(csv, filterOptions, function (err, filteredCsv) {
    if (err) {
        return err;
    }
    return filteredCsv;

    // Output: 'Address Number,Address Street,City,State,Zip\n22222,Main St,Chicago,IL,60605\n22211,Michigan Ave,Chicago,IL,60607'
});

csvFilterSort.sort(csv, sortOptions, function (err, sortedCsv) {
    if (err) {
        return err;
    }
    return sortedCsv;

    // Output: 'Address Number,Address Street,City,State,Zip\n11111,De Anza Blvd,Cupertino,CA,95014\n44444,Mission St,San Francisco,CA,95001\n22211,Michigan Ave,Chicago,IL,60607\n22222,Main St,Chicago,IL,60605\n33333,Woodward Ave,Detroit,MI,48048'
});

Running the tests

In the command line, run npm run mocha or npm run test to begin the test suite.

Authors

License

This project is licensed under the MIT License - see the LICENSE.md file for details

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago