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

angular-papa-promise

v0.0.4

Published

This module provides an AngularJS service wrapper for the PapaParse (http://papaparse.com/) CSV parsing library.

Downloads

318

Readme

angular-papa-promise

This module provides an AngularJS service wrapper for the PapaParse (http://papaparse.com/) CSV parsing library so it can be injected via dependency injection. It uses promises instead of the default callback mechanism so you don't need to kick off the $digest cycle manually after parsing has finished.

Installation

Download angular-papa-promise to your project from github (https://github.com/of17/angular-papa-promise/blob/master/dist/angular-papa-promise.js) or add it via bower:

bower install --save angular-papa-promise

angular-papa-promise depends on the PapaParse library, so be sure to grab that one as well (which bower will take care of for you).

Usage / Example##

Once downloaded include PapaParse and angular-papa-promise in your HTML page

<script src="bower_components/papaparse/papaparse.js"></script>
<script src="bower_components/angular-papa-promise/dist/angular-papa-promise.js"></script>

then reference the papa-promise sub-module in your module and have the injector inject the 'Papa' service into your controller, directive, etc.

angular.module('myapp', ['papa-promise'])

The sample code below demonstrates how to use the papa-promise service in a controller to parse CSV to JSON (and vice versa).

var app = angular.module('myapp', ['papa-promise']);

function AppController(Papa) {

    function handleParseResult(result) {
        // do something useful with the parsed data
    }

    function handleParseError(result) {
        // display error message to the user
    }

    function parsingFinished() {
        // whatever needs to be done after the parsing has finished
    }

    function parseCSV(data) {
        Papa.parse(data)
            .then(handleParseResult)
            .catch(handleParseError)
            .finally(parsingFinished);
    }

    function jsonToCSV(json) {
        Papa.unparse(data)
            .then(handleParseResult)
            .catch(handleParseError)
            .finally(parsingFinished);
    }

    angular.extend(this, {
        parseCSV: parseCSV,
        jsonToCSV: jsonToCSV
    });

}
AppController.$inject = ['Papa'];
app.controller('AppController', AppController);

Handling parse errors

PapaParse communicates parse errors via the errors property of the result object that's being passed to the complete callback function. Errors related to loading the CSV file via URL or FileAPI result in the error callback function being invoked. http://papaparse.com/docs#errors

As a result you'll need to handle errors in the complete callback as well as in the error callback. angular-papa-promise by default sticks with this behavior but you can instruct it to reject the promise in case an error occurred by adding the rejectOnError flag to the configuration object.

Papa.parse(csv, { rejectOnError: true })
    .then(handleParseResult)
    .catch(handleParseError);

Note

angular-papa-promise does't support PapaParse's fancy features like worker threads or streaming. If you need these, you can grab the original PapaParse object via Papa.Papa and invoke parse and unparse on it directly.

var result = Papa.Papa.parse(csv, config);

For further details on the configuration options refer to the Config Options section of the PapaParse docs (http://papaparse.com/docs#config-details).