Skip to content

Commit

Permalink
papa-parse.start will return a Promise if parse via URL or File
Browse files Browse the repository at this point in the history
  • Loading branch information
eterna2 committed Oct 10, 2017
1 parent 8b8c218 commit 932aef7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ PolymerVis is a personal project and is NOT in any way affliated with Papaparse,
`papa-parse` is a Polymer 2.0 element to parse CSV files into JSON object(s)
with [Papa parse](http://papaparse.com/).

`papa-parse` can download and parse a csv file via `url`, or from raw csv strings via `raw`, and File object via `file`. If the `auto` flag is set, `papa-parse` will automatically start the job, otherwise a manual call to the function `parse` will be needed.
`papa-parse` can download and parse a csv file via `url`, or from raw csv strings via `raw`, and File object via `file`. If the `auto` flag is set, `papa-parse` will automatically start the job, otherwise a manual call to the function `start` will be needed.

Parse from URL.
```html
Expand Down
64 changes: 39 additions & 25 deletions papa-parse.html
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,9 @@
/**
* If `auto` flag is not set, use this function to start the job manually.
* Will resume parsing instead if parsing was paused previously.
* Only return results if `raw` is being parsed.
* @return {Array[]|Object[]|undefined}
* return results if `raw` is being parsed, otherwise return a Promise to the results
*
* @return {Array[]|Object[]|Promise}
*/
start() {
if (this.stream && this._parser) return this.resume();
Expand Down Expand Up @@ -425,30 +426,34 @@
}

_parseRaw(raw, _config, run) {
if (!raw || !this._attached || !run) return;
if (!raw || !run) return;
var results = Papa.parse(raw, _config);
this._syncProperties(results);
return results;
}

_parseFile(file, _config, run) {
if (!file || !this._attached || !run) return;
var config = Object.assign(
{complete: this._complete.bind(this)},
this._config
);

Papa.parse(file, config);
if (!file || !run) return;
return new Promise((resolve, reject) => {
let config = Object.assign(
{complete: this._complete(this, resolve, reject)},
this._config
);

Papa.parse(file, config);
});
}

_parseUrl(url, _config, run) {
if (!url || !this._attached || !run) return;
var config = Object.assign(
{download: true, complete: this._complete.bind(this)},
this._config
);

Papa.parse(url, config);
if (!url || !run) return;
return new Promise((resolve, reject) => {
let config = Object.assign(
{download: true, complete: this._complete(this, resolve, reject)},
this._config
);

Papa.parse(url, config);
});
}

_syncProperties(results) {
Expand All @@ -467,17 +472,26 @@
this.dispatchEvent(new CustomEvent('stream', {detail: results}));
}

_complete(results) {
this._syncProperties(results);
_complete(self, resolve, reject) {
return function(results) {
this._syncProperties(results);

if (this.errors && this.errors.length > 0)
return this.dispatchEvent(new CustomEvent('errored', {detail: results}));
if (this.errors && this.errors.length > 0) {
this.dispatchEvent(new CustomEvent('errored', {detail: results}));
return reject(results);
}

if (results && results.meta && results.meta.aborted) {
this.dispatchEvent(new CustomEvent('aborted', {detail: results}));
return reject(results);
}

if (results && results.meta && results.meta.aborted)
return this.dispatchEvent(new CustomEvent('aborted', {detail: results}));
if (results) {
this.dispatchEvent(new CustomEvent('completed', {detail: results}));
}

if (results)
this.dispatchEvent(new CustomEvent('completed', {detail: results}));
resolve(results);
}.bind(self);
}
}

Expand Down

0 comments on commit 932aef7

Please sign in to comment.