https://github.com/hypercubed/angular-dsv
angularjs $http for tabular data
https://github.com/hypercubed/angular-dsv
Last synced: about 1 year ago
JSON representation
angularjs $http for tabular data
- Host: GitHub
- URL: https://github.com/hypercubed/angular-dsv
- Owner: Hypercubed
- Created: 2014-06-11T08:49:12.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2016-10-13T12:49:39.000Z (over 9 years ago)
- Last Synced: 2025-04-11T03:49:10.118Z (about 1 year ago)
- Language: JavaScript
- Homepage: https://github.com/Hypercubed/angular-dsv
- Size: 47.9 KB
- Stars: 5
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# angular-dsv [](http://badge.fury.io/bo/angular-dsv)
delimter-seperated-values
A angularjs service for reading tabular data from text files; for example tab-delimited and comma-delimited. angular-dsv combines the convenience of the [d3 csv/tsv module](https://github.com/mbostock/d3/wiki/CSV) with angular's [$http](https://docs.angularjs.org/api/ng/service/$http) service. angular-dsv is RFC4180-compliant and dependent only on angular itself.
## Install
1. `bower install angular-dsv` or `bower install Hypercubed/angular-dsv`
2. Include the `angular-dsv.js` into your app. By default at `bower_components/angular-dsv/angular-dsv.js`.
4. Add `hc.dsv` as a module dependency to your app.
## Usage
### dsv(delimiter)
The dsv service takes a single argument and returns a new `$http`-like service for handling text files of `delimiter`-seperated values. `dsv.tsv` and `dsv.csv` are shortcuts for `dsv('\t')` and `dsv(',')` respectively.
### dsv.tsv(config[, accessor])
The `dsv.tsv` service is an example of 'delimiter'-seperated value interface for tab-delimited tables. It is service which takes two arguments: a configuration object like that expected by angular's [$http](https://docs.angularjs.org/api/ng/service/$http), and an optional accessor function for transforming each row of the tabular data file. Like `$http` `dsv.tsv` returns a promise:
```(js)
dsv.tsv({method: 'GET', url: '/someUrl'}, function(d) { return {key: d.key, value: +d.value}; })
.then(function(response) {
console.log(response.data);
// this callback will be called asynchronously
// when the response is available
})
.catch(function(err) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
```
The data value is array of objects representing the parsed rows returned from the specified url. The first row of the returned data is used as column names; these column names become the attributes on the returned objects. For example if the http request returns:
```
Year Make Model Length
1997 Ford E350 2.34
2000 Mercury Cougar 2.38
```
The resulting JavaScript array is:
```
[
{"Year": "1997", "Make": "Ford", "Model": "E350", "Length": "2.34"},
{"Year": "2000", "Make": "Mercury", "Model": "Cougar", "Length": "2.38"}
]
```
#### dsv.tsv.get(url\[, config]\[, accessor])
Like `$http` `dsv.tsv` provides a shortcut method for HTTP GET:
```(js)
dsv.tsv.get('/someUrl', accessorFunction).then(successCallback);
```
#### dsv.tsv.getRows(url\[, config]\[, accessor])
Similar to the `dsv.tsv.get` shortcut except the returned value is an array of arrays and the header line is treated as a standard row. For example if the http request returns:
```
Year Make Model Length
1997 Ford E350 2.34
2000 Mercury Cougar 2.38
```
The resulting JavaScript array is:
```
[
["Year", "Make", "Model", "Length"],
["1997", "Ford", "E350", "2.34"],
["2000", "Mercury", "Cougar", "2.38"]
]
```
### dsv.csv
Like `dsv.tsv` except for comma-seperated-values.
## Testing
Install npm and bower dependencies:
```bash
npm install
bower install
npm test
```
## Acknowledgments
Previously portions of this code were taken from mbostock's [d3 csv/tsv module](https://github.com/mbostock/d3/wiki/CSV). Now using [d3-dsv](https://github.com/d3/d3-dsv).
## License
MIT