npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

csv-filter-sort

v1.0.9

Published

Filter and sort CSV rows using criteria for columns values and save the result as a new CSV

Downloads

265

Readme

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:

| Name | Type | | --------------- | -------- | | csv | String | | filterOptions | Object | | callback | Function |

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'
}

| Name | Type | Description | Required | Default | | --------------- | ------------------ | -------------------------------------------------------- | -------- | --------- | | hasHeader | Boolean | Specfies whether or not the first CSV row is a header. | false | false | | columnToFilter| String or integer | The column name or number to filter by. | true | | | filterCriteria| String or integer | The criteria to filter rows by. If string is provided, CSV will be filtered by character length of the value. | true | | | filterType | String | Options include EXACT, LESS, and GREATER. | false | 'EXACT' |

sort(csv, sortOptions, callback)

Parameters:

| Name | Type | | --------------- | -------- | | csv | String | | sortOptions | Object | | callback | Function |

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'
}

| Name | Type | Description | Required | Default | | --------------- | ------------------ | -------------------------------------------------------- | -------- | ------- | | hasHeader | Boolean | Specfies whether or not the first CSV row is a header. | false | false | | sortByColumn | String or integer | The column name or number to sort by. | true | | | orderBy | String | Options 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