2.4.5 • Published 1 year ago

csv-sorter v2.4.5

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

csv-sorter

What's that?

Naturally sort your CSV files by a column of your choice with delimiter auto recognition. Save the sorted result into a new .csv file or return a csv string or an array of sorted records for further manipulation.
Or use it from the CLI!

You can use the same module for node.js or the browser (e.g. react.js) with es5 compatibility, only the import syntax is different.

Usage in node.js:

import { sort } from 'csv-sorter'; //node.js environment

const config  = {
    src: './Source.csv',
    sortColumn: 1, //number of column to sort
    reverse: false, //sort in revserse order
    sortWithHeader: false //sort including first header line
};

sort(config, (result, err)=>{
    if(err) throw err;
    console.log(result); //array of sorted records for further manipulation
});

/* Async await */  😎 

async function foo(){
    const result = await sort(config);
    console.log(result); //array of sorted records for further manipulation, or error
} 



//Or to a file:

const config  = {
    src: './Source.csv',
    dest: './Result.csv',
    sortColumn: 1, //number of column to sort
    reverse: false, //sort in revserse order
    sortWithHeader: false //sort including first header line
};

sort(config, (result, err)=>{
    if(err) throw err;
    //your sorted Result.csv file was successfully saved...
    console.log(result); //also ready is your array of sorted records for further manipulation
});


//Or directly from a CSV string:

async function foo(){

    const config  = {
    csvString: 
    `a,"b","c","d","e",f,"g"
    3,"1","5","4","5",2,"F"
    2,"2","4","4","5",2.0,"F"
    4,"3","3","3","3",3.0,"F"
    1,"4","2","2","1",1.0,"F"`,
    dest: './Result.csv',
    sortColumn: 1,
    };

    const result = await sort(config);
    console.log(result); 
} 

// To stringify the output you can use the stringifyOutput option:

const config  = {
    src: './Source.csv',
    sortColumn: 1, //number of column to sort
    stringifyOutput: true //return a string instead of an array of records
};

sort(config, (result, err)=>{
    if(err) throw err;
    console.log(result); //csv string
});

Usage in browser:

import { sort } from 'csv-sorter/browser/umd'; //browser environment 

//in the browser, the input always comes from a csvString
async function foo(){

    const config  = {
    csvString: 
    `a,"b","c","d","e",f,"g"
    3,"1","5","4","5",2,"F"
    2,"2","4","4","5",2.0,"F"
    4,"3","3","3","3",3.0,"F"
    1,"4","2","2","1",1.0,"F"`, //required
    sortColumn: 1, //required
    stringifyOutput: true, //optional
    reverse: false, //optional
    sortWithHeader: false //optional
    };

    const result = await sort(config);
    console.log(result); 
} 


//or import via sctipt tag from CDN in html
<script src="https://cdn.jsdelivr.net/npm/csv-sorter@2.4.5/browser/umd.js"></script>

Or from CLI:

params:

--s path to source file\ --d path to destination file\ --c sortColumn

options:

-H sortWithHeader\ -O stdout\ -R reverse

from file

node ./node_modules/csv-sorter/lib/index.js --s=./Source.csv --d=./Dest.csv --c=1 -HOR

example from stdin

cat Source.csv | node ./node_modules/csv-sorter/lib/index.js --d=./Dest.csv --c=1 -HOR

Contributers welcome!

2.4.3

1 year ago

2.4.2

1 year ago

2.4.5

1 year ago

2.4.4

1 year ago

2.4.1

2 years ago

2.4.0

2 years ago

2.3.2

2 years ago

2.3.0

2 years ago

2.2.1

2 years ago

2.2.0

2 years ago

2.1.1

2 years ago

2.3.1

2 years ago

2.1.0

2 years ago

2.0.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago