csv-string
Advanced tools
Comparing version 4.0.1 to 4.1.0
import { Streamer } from './Streamer'; | ||
import { Comma, ForEachCallback, LineBreak, PristineInput, Quote, ReadAllCallback, ReadCallback } from './types'; | ||
import { Comma, ForEachCallback, LineBreak, PristineInput, Quote, ReadAllCallback, ReadCallback, Value, ParseOptions } from './types'; | ||
declare const EOL: LineBreak; | ||
@@ -7,3 +7,11 @@ declare const SEPARATOR: Comma; | ||
declare const stringify: (input?: PristineInput, sep?: Comma) => string; | ||
declare const parse: (input: string, sep?: "," | ";" | "|" | "\t" | undefined, quo?: string | undefined) => string[][]; | ||
declare function parse(input: string, sep?: Comma, quo?: Quote): Value[][]; | ||
declare function parse(input: string, opts?: Partial<ParseOptions & { | ||
output: 'tuples'; | ||
}>): Value[][]; | ||
declare function parse(input: string, opts: Partial<ParseOptions> & { | ||
output: 'objects'; | ||
}): { | ||
[k: string]: Value; | ||
}[]; | ||
declare function read(input: string, callback: ReadCallback): number; | ||
@@ -10,0 +18,0 @@ declare function read(input: string, sep: Comma, callback: ReadCallback): number; |
@@ -87,11 +87,28 @@ "use strict"; | ||
exports.stringify = stringify; | ||
var parse = function (input, sep, quo) { | ||
if (sep === undefined) { | ||
// try to detect the separator if not provided | ||
sep = detect(input); | ||
function parse(input, sepOrOpts, quo) { | ||
// Create an options hash, using positional parameters or the passed in options hash for values | ||
var opts = typeof sepOrOpts === "object" ? sepOrOpts : {}; | ||
if (typeof sepOrOpts === "string") { | ||
opts.comma = sepOrOpts; | ||
} | ||
var csv = new Parser_1.Parser(input, sep, quo); | ||
return csv.File(); | ||
}; | ||
if (quo) { | ||
opts.quote = quo; | ||
} | ||
// try to detect the separator if not provided | ||
if (opts.comma === undefined) { | ||
opts.comma = detect(input); | ||
} | ||
// Clean characters, if necessary | ||
// TODO: We should probably throw an error here to signal bad input to the user | ||
if (opts.comma) { | ||
opts.comma = opts.comma[0]; | ||
} | ||
if (opts.quote) { | ||
opts.quote = opts.quote[0]; | ||
} | ||
var csv = new Parser_1.Parser(input, opts.comma, opts.quote); | ||
return csv.File(opts.output); | ||
} | ||
exports.parse = parse; | ||
; | ||
function read(input, sep, quo, callback) { | ||
@@ -98,0 +115,0 @@ if (callback === undefined) { |
@@ -12,3 +12,9 @@ import { Comma, Quote, Value } from './types'; | ||
constructor(input: string, comma?: Comma, quote?: Quote); | ||
File(): Value[][]; | ||
File(output: "objects"): { | ||
[k: string]: Value; | ||
}[]; | ||
File(output?: "tuples"): Value[][]; | ||
File(output?: "tuples" | "objects"): { | ||
[k: string]: Value; | ||
}[] | Value[][]; | ||
Row(): Value[]; | ||
@@ -15,0 +21,0 @@ private Value; |
@@ -33,15 +33,14 @@ "use strict"; | ||
} | ||
Parser.prototype.File = function () { | ||
var files = []; | ||
var row; | ||
Parser.prototype.File = function (output) { | ||
var rows = []; | ||
while (true) { | ||
var tempointer = this.pointer; | ||
row = this.Row(); | ||
var row = this.Row(); | ||
if (row.length > 0) { | ||
this.linePointer = tempointer; | ||
files.push(row); | ||
rows.push(row); | ||
} | ||
else { | ||
if (this.linePointer && this.pointer !== this.input.length) { | ||
files.pop(); | ||
rows.pop(); | ||
this.pointer = this.linePointer; | ||
@@ -53,3 +52,3 @@ } | ||
if (this.linePointer && this.pointer !== this.input.length) { | ||
files.pop(); | ||
rows.pop(); | ||
this.pointer = this.linePointer; | ||
@@ -60,3 +59,15 @@ } | ||
} | ||
return files; | ||
if (output && output === "objects") { | ||
if (rows.length === 0) { | ||
return []; | ||
} | ||
var headers_1 = rows.shift(); | ||
return rows.map(function (row) { return headers_1.reduce(function (data, k, i) { | ||
data[k] = row[i]; | ||
return data; | ||
}, {}); }); | ||
} | ||
else { | ||
return rows; | ||
} | ||
}; | ||
@@ -63,0 +74,0 @@ Parser.prototype.Row = function () { |
@@ -12,1 +12,6 @@ export declare type LineBreak = '\r\n' | '\n' | '\r'; | ||
export declare type ForEachCallback = (row: Value[], index: number) => void; | ||
export declare type ParseOptions = { | ||
comma: Comma; | ||
quote: Quote; | ||
output: 'objects' | 'tuples'; | ||
}; |
{ | ||
"name": "csv-string", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "PARSE and STRINGIFY for CSV strings. It's like JSON object but for CSV. It can also work row by row. And, if can parse strings, it can be use to parse files or streams too.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -30,2 +30,3 @@ # Javascript CSV Strings | ||
- [Kael Shipman](https://github.com/kael-shipman) | ||
- [Mehul Mohan](https://github.com/mehulmpt) | ||
@@ -59,2 +60,3 @@ - [Hossam Magdy](https://github.com/hossam-magdy) | ||
### parse(input: String, [options: Object]): Object | ||
### parse(input: string, [separator: string], [quote: string]): Object | ||
@@ -64,5 +66,15 @@ | ||
Options : | ||
- `comma` **String** to indicate the CSV separator. (optional, default `,`) | ||
- `quote` **String** to indicate the CSV quote if need. (optional, default `"`) | ||
- `output` **String** choose 'objects' or 'tuples' to change output for Array or Object. (optional, default `tuples`) | ||
Example 1 : | ||
```js | ||
const CSV = require('csv-string'); | ||
const parsedCsv = CSV.parse('a,b,c\na,b,c'); | ||
const parsedCsv = CSV.parse('a;b;c\nd;e;f', ';'); | ||
console.log(parsedCsv); | ||
@@ -76,6 +88,24 @@ ``` | ||
["a", "b", "c"], | ||
["a", "b", "c"] | ||
["d", "e", "f"] | ||
] | ||
``` | ||
Example 2 : | ||
```js | ||
const CSV = require('csv-string'); | ||
const parsedCsv = CSV.parse('a,b,c\n1,2,3\n4,5,6', { output: 'objects' }); | ||
console.log(parsedCsv); | ||
``` | ||
Output: | ||
```json | ||
[ | ||
{ a: '1', b: '2', c: '3' }, | ||
{ a: '4', b: '5', c: '6' } | ||
] | ||
``` | ||
If separator parameter is not provided, it is automatically detected. | ||
@@ -199,3 +229,3 @@ | ||
Calls `callback` when all CSV rows are read. The Array passed to callback contains the rows of the file. | ||
Returns the offset of the end of parsing (generaly it's the end of the input string). | ||
Returns the offset of the end of parsing (generally it's the end of the input string). | ||
@@ -202,0 +232,0 @@ ```js |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
30306
591
340
1