Easy And Powerful Library For Parsing CSV – ACsv

Category: Javascript | April 30, 2022
Author:amin2312
Views Total:89 views
Official Page:Go to website
Last Update:April 30, 2022
License:MIT

Preview:

Easy And Powerful Library For Parsing CSV – ACsv

Description:

ACsv is an easy, fast, and powerful CSV parsing library for JavaScript, Typescript, Haxe, PHP, and JavaScript.

It supports both standard CSV data (comma-separated values) and enhanced CSV format (containing bool, int, numbers, strings, JSON, etc).

How to use it (JavaScript):

1. Download and load the ACsv library.

<script src="/path/to/javascript/ACsv.min.js"></script>

2. Read data from CSV.

/**
* Standard csv format text
*/
var standard_format_text = "id,id2,id3,name,brief\n1,20,100,John,He is a googd man\n2,20,100,\u5F20\u4E09,\"\u4ED6\u662F\u4E00\u4E2A\u597D\u4EBA\n\u6211\u4EEC\u90FD\u559C\u6B22\u4ED6\"\n3,21,100,\u043C\u043E\u0440\u0435,\"\u041E\u043D \u0445\u043E\u0440\u043E\u0448\u0438\u0439 \u0447\u0435\u043B\u043E\u0432\u0435\u043A\n\u043C\u044B \u0432\u0441\u0435 \u043B\u044E\u0431\u0438\u043C \u0435\u0433\u043E\n\u0415\u0433\u043E \u0434\u0435\u0432\u0438\u0437:\n\"\"\u0434\u043E\u0432\u0435\u0440\u044F\u0439 \u0441\u0435\u0431\u0435\"\"\"\n4,21,200,\u0627\u0644\u0634\u0645\u0633,\u0635\u0628\u0627\u062D \u0627\u0644\u062E\u064A\u0631\n5,22,200,\u091A\u0902\u0926\u094D\u0930\u092E\u093E,\u0938\u0941\u0938\u0902\u0927\u094D\u092F\u093E\n6,22,200,\u0E14\u0E32\u0E27,";
/**
* Enhanced csv format text
*/
var enhanced_format_text = "id:int,id2:int,id3:int,name:string,weight:number,marry:bool,education:json,tags:strings,brief\n1,21,100,John,120.1,true,\"[\"\"AB\"\"]\",\"good,cool\",\"Today is good day\nTomorrow is good day too\"\n2,21,100,\u5F20\u4E09,121.2,false,\"[\"\"CD\"\",\"\"EF\"\"]\",good,\u4ECA\u5929\u662F\u4E2A\u597D\u65E5\u5B50\n3,22,100,\u043C\u043E\u0440\u0435,123.4,true,\"[\"\"GH\"\",\"\"AB\"\",\"\"CD\"\"]\",good,\"\u0421\u0435\u0433\u043E\u0434\u043D\u044F \u0445\u043E\u0440\u043E\u0448\u0438\u0439 \u0434\u0435\u043D\u044C\n\u0417\u0430\u0432\u0442\u0440\u0430 \u0442\u043E\u0436\u0435 \u0445\u043E\u0440\u043E\u0448\u0438\u0439 \u0434\u0435\u043D\u044C\"\n4,22,200,\u0627\u0644\u0634\u0645\u0633,124.5,false,\"{\"\"AA\"\":12}\",strong,\u0635\u0628\u0627\u062D \u0627\u0644\u062E\u064A\u0631\n5,23,200,\u091A\u0902\u0926\u094D\u0930\u092E\u093E,126.7,1,\"{\"\"BB\"\":12}\",strong,\u0938\u0941\u0938\u0902\u0927\u094D\u092F\u093E\n6,23,200,Emilia,,0,\"{\"\"CC\"\":67,\"\"DD\"\":56}\",\"strong,cool\",Hoje \u00E9 um bom dia\n7,24,300,Ay\u015Fe,128.9,0,\"{\"\"EE\"\":68,\"\"FF\"\":56}\",\"strong,cool\",Bug\u00FCn g\u00FCzel bir g\u00FCn\n8,24,300,\u967D\u83DC\u4E43,129.01,,\"{\"\"AC\"\":78,\"\"BD\"\":[90,12]}\",\"height,strong\",\u4ECA\u65E5\u306F\u3044\u3044\u65E5\u3067\u3059\n9,25,300,Dwi,130.12,1,\"{\"\"EF\"\":78,\"\"CF\"\":[90,12]}\",,\"Hari ini adalah hari yang baik\nBesok juga hari yang baik\"\n10,25,400,B\u1EA3o,131.23,1,\"[\"\"BC\"\",{\"\"AT\"\":34}]\",\"thin,good\",\n11,26,400,\uBBFC\uC900,132.34,0,\"[\"\"FG\"\",{\"\"AG\"\":34}]\",\"hot,thin,good\",\uC624\uB298\uC740 \uC88B\uC740 \uB0A0\uC774\uB2E4\n12,26,400,\u0E14\u0E32\u0E27,133.456,0,,,";
// standard CSV
var table = acsv.Table.Parse(standard_format_text);
// enhanced CSV
var table2 = acsv.Table.Parse(enhanced_csv_content);

3. API methods.

// select ALL to rows
table.selectAll().toRows()
// => [["1","20","100","John","He is a googd man"],["2","20","100","张三","他是一个好人\n我们都喜欢他"],["3","21","100","море","Он хороший человек\nмы все любим его\nЕго девиз:\n\"доверяй себе\""],["4","21","200","الشمس","صباح الخير"],["5","22","200","चंद्रमा","सुसंध्या"],["6","22","200","ดาว",""]]
// select ALL to objs
table.selectAll().toObjs()
// =>[{"id":"1","id2":"20","id3":"100","name":"John","brief":"He is a googd man"},{"id":"2","id2":"20","id3":"100","name":"张三","brief":"他是一个好人\n我们都喜欢他"},{"id":"3","id2":"21","id3":"100","name":"море","brief":"Он хороший человек\nмы все любим его\nЕго девиз:\n\"доверяй себе\""},{"id":"4","id2":"21","id3":"200","name":"الشمس","brief":"صباح الخير"},{"id":"5","id2":"22","id3":"200","name":"चंद्रमा","brief":"सुसंध्या"},{"id":"6","id2":"22","id3":"200","name":"ดาว","brief":""}]
// select the first row
table.selectFirstRow().toFirstRow()
// => ["1","20","100","John","He is a googd man"]
// select the first row (obj)
table.selectFirstRow().toFirstObj()
// => {"id":"1","id2":"20","id3":"100","name":"John","brief":"He is a googd man"}
// select the last row
table.selectLastRow().toFirstRow()
// => ["6","22","200","ดาว",""]
// select the last row (obj)
table.selectLastRow().toFirstObj()
// => {"id":"6","id2":"22","id3":"200","name":"ดาว","brief":""}
// select rows when equal to...
table.selectWhenE(1, "2").toFirstObj()
// => {"id":"2","id2":"20","id3":"100","name":"张三","brief":"他是一个好人\n我们都喜欢他"}
table.selectWhenE2(1, "3", "21").toFirstObj()
// => "21"{"id":"3","id2":"21","id3":"100","name":"море","brief":"Он хороший человек\nмы все любим его\nЕго девиз:\n\"доверяй себе\""}
table.selectWhenE3(1, "4", "21", "200").toFirstObj()
// => "21" and (id3) = "200"{"id":"4","id2":"21","id3":"200","name":"الشمس","brief":"صباح الخير"}
table.selectWhenE(0, "20", 1).toObjs()
// => "20"[{"id":"1","id2":"20","id3":"100","name":"John","brief":"He is a googd man"},{"id":"2","id2":"20","id3":"100","name":"张三","brief":"他是一个好人\n我们都喜欢他"}]
// merge tables
table.merge(table).selectAll().toRows()
// select rows when greater than ...
table2.selectWhenG(0, false, 25, 1).toObjs()
// => [{"id":11,"id2":26,"id3":400,"name":"민준","weight":132.34,"marry":false,"education":["FG",{"AG":34}],"tags":["hot","thin","good"],"brief":"오늘은 좋은 날이다"},{"id":12,"id2":26,"id3":400,"name":"ดาว","weight":133.456,"marry":false,"education":null,"tags":[],"brief":""}]
// select rows when less than ...
table2.selectWhenL(0, false, 22, 1).toObjs()
// => [{"id":1,"id2":21,"id3":100,"name":"John","weight":120.1,"marry":true,"education":["AB"],"tags":["good","cool"],"brief":"Today is good day\nTomorrow is good day too"},{"id":2,"id2":21,"id3":100,"name":"张三","weight":121.2,"marry":false,"education":["CD","EF"],"tags":["good"],"brief":"今天是个好日子"}]
// select rows when greater and less than ...
table2.selectWhenGreaterAndLess(0, false, false, 21, 24, 1).toObjs()
// => [{"id":3,"id2":22,"id3":100,"name":"море","weight":123.4,"marry":true,"education":["GH","AB","CD"],"tags":["good"],"brief":"Сегодня хороший день\nЗавтра тоже хороший день"},{"id":4,"id2":22,"id3":200,"name":"الشمس","weight":124.5,"marry":false,"education":{"AA":12},"tags":["strong"],"brief":"صباح الخير"},{"id":5,"id2":23,"id3":200,"name":"चंद्रमा","weight":126.7,"marry":true,"education":{"BB":12},"tags":["strong"],"brief":"सुसंध्या"},{"id":6,"id2":23,"id3":200,"name":"Emilia","weight":0,"marry":false,"education":{"CC":67,"DD":56},"tags":["strong","cool"],"brief":"Hoje é um bom dia"}]
// select rows when greater or less than ...
table2.selectWhenLessOrGreater(0, false, false, 22, 25, 1).toObjs()
// => [{"id":1,"id2":21,"id3":100,"name":"John","weight":120.1,"marry":true,"education":["AB"],"tags":["good","cool"],"brief":"Today is good day\nTomorrow is good day too"},{"id":2,"id2":21,"id3":100,"name":"张三","weight":121.2,"marry":false,"education":["CD","EF"],"tags":["good"],"brief":"今天是个好日子"},{"id":11,"id2":26,"id3":400,"name":"민준","weight":132.34,"marry":false,"education":["FG",{"AG":34}],"tags":["hot","thin","good"],"brief":"오늘은 좋은 날이다"},{"id":12,"id2":26,"id3":400,"name":"ดาว","weight":133.456,"marry":false,"education":null,"tags":[],"brief":""}]
// select rows when in specific id
table2.selectWhenIn(1, [3, 4, 5]).toObjs()
// => [{"id":3,"id2":22,"id3":100,"name":"море","weight":123.4,"marry":true,"education":["GH","AB","CD"],"tags":["good"],"brief":"Сегодня хороший день\nЗавтра тоже хороший день"},{"id":4,"id2":22,"id3":200,"name":"الشمس","weight":124.5,"marry":false,"education":{"AA":12},"tags":["strong"],"brief":"صباح الخير"},{"id":5,"id2":23,"id3":200,"name":"चंद्रमा","weight":126.7,"marry":true,"education":{"BB":12},"tags":["strong"],"brief":"सुसंध्या"}]
// select at...
table2.selectAt([0, 1, 10]).toObjs()
// => [{"id":1,"id2":21,"id3":100,"name":"John","weight":120.1,"marry":true,"education":["AB"],"tags":["good","cool"],"brief":"Today is good day\nTomorrow is good day too"},{"id":2,"id2":21,"id3":100,"name":"张三","weight":121.2,"marry":false,"education":["CD","EF"],"tags":["good"],"brief":"今天是个好日子"},{"id":11,"id2":26,"id3":400,"name":"민준","weight":132.34,"marry":false,"education":["FG",{"AG":34}],"tags":["hot","thin","good"],"brief":"오늘은 좋은 날이다"}]
// multiple select
table2.selectWhenE(0, 100, 2).selectWhenL(0, false, 22, 1).toObjs()
// => [{"id":1,"id2":21,"id3":100,"name":"John","weight":120.1,"marry":true,"education":["AB"],"tags":["good","cool"],"brief":"Today is good day\nTomorrow is good day too"},{"id":2,"id2":21,"id3":100,"name":"张三","weight":121.2,"marry":false,"education":["CD","EF"],"tags":["good"],"brief":"今天是个好日子"}]
// sort data
table2.selectWhenE(0, 300, 2).sortBy(0, 1).toObjs()
// => [{"id":9,"id2":25,"id3":300,"name":"Dwi","weight":130.12,"marry":true,"education":{"EF":78,"CF":[90,12]},"tags":[],"brief":"Hari ini adalah hari yang baik\nBesok juga hari yang baik"},{"id":8,"id2":24,"id3":300,"name":"陽菜乃","weight":129.01,"marry":false,"education":{"AC":78,"BD":[90,12]},"tags":["height","strong"],"brief":"今日はいい日です"},{"id":7,"id2":24,"id3":300,"name":"Ayşe","weight":128.9,"marry":false,"education":{"EE":68,"FF":56},"tags":["strong","cool"],"brief":"Bugün güzel bir gün"}]

You Might Be Interested In:


Leave a Reply