# read-data [![NPM version](https://img.shields.io/npm/v/read-data.svg?style=flat)](https://www.npmjs.com/package/read-data) [![NPM monthly downloads](https://img.shields.io/npm/dm/read-data.svg?style=flat)](https://npmjs.org/package/read-data)  [![NPM total downloads](https://img.shields.io/npm/dt/read-data.svg?style=flat)](https://npmjs.org/package/read-data) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/read-data.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/read-data)

> Read JSON or YAML files.

## Install

Install with [npm](https://www.npmjs.com/):

```sh
$ npm install --save read-data
```

**Heads up!**

Please read the [release history](#history), there were breaking changes in 1.0.0!

## Usage

```js
var read = require('read-data');

// sync
console.log(read.sync('foo.yml'));
console.log(read.sync('foo.yaml'));
console.log(read.sync('foo.json'));

// async
read('foo.yml', function(err, data) {
  if (err) return console.log(err);
  console.log(data);
});
read('foo.yaml', function(err, data) {
  if (err) return console.log(err);
  console.log(data);
});
read('foo.json', function(err, data) {
  if (err) return console.log(err);
  console.log(data);
});
```

## API

### [read](index.js#L41)

Asynchronously read a JSON or YAML file, automatically determining the reader based on extension.

**Params**

* `filepath` **{String}**: path of the file to read.
* `options` **{Object|String}**: to pass to [js-yaml](https://github.com/nodeca/js-yaml)
* `cb` **{Function}**: callback function
* `returns` **{Object}**: JSON

**Example**

```js
var read = require('read-data');

read('foo.json', function(err, data) {
  if (err) throw err;
  console.log(data);
});

read('foo.yml', function(err, data) {
  if (err) throw err;
  console.log(data);
});
```

### [.sync](index.js#L70)

Synchronously read a `.json` or `.(yaml|yml)` file, automatically determining the reader based on extension.

**Params**

* `filepath` **{String}**: path of the file to read.
* `options` **{Object|String}**: to pass to [js-yaml](https://github.com/nodeca/js-yaml)
* `returns` **{Object}**: JSON

**Example**

```js
var data = require('read-data').data;

var yaml = data.sync('foo.yml');
var json = data.sync('foo.json');
```

### [.yaml](index.js#L96)

Asynchronously read a YAML file.

**Params**

* `filepath` **{String}**: path of the file to read.
* `options` **{Object|String}**: to pass to [js-yaml](https://github.com/nodeca/js-yaml)
* `cb` **{Function}**: callback function
* `returns` **{Object}**: JSON

**Example**

```js
var yaml = require('read-data').yaml;

yaml('foo.yml', function(err, data) {
  if (err) throw err;
  console.log(data);
});
```

### [.yaml.sync](index.js#L113)

Synchronously read a YAML file.

**Params**

* `filepath` **{String}**: path of the file to read.
* `options` **{Object|String}**: to pass to [js-yaml](https://github.com/nodeca/js-yaml)
* `returns` **{Object}**: JSON

**Example**

```js
var yaml = require('read-data').yaml;
var data = yaml.sync('foo.yml');
```

### [.json](index.js#L134)

Asynchronously read a JSON file.

**Params**

* `filepath` **{String}**: path of the file to read.
* `callback` **{Function}**: callback function
* `returns` **{Object}**: JSON

**Example**

```js
var json = require('read-data');

json('foo.json', function(err, data) {
  if (err) throw err;
  console.log(data);
});
```

### [.json.sync](index.js#L166)

Synchronously read a JSON file.

**Params**

* `filepath` **{String}**: path of the file to read.
* `returns` **{Object}**: JSON

**Example**

```js
var json = require('read-data').json;
var data = json.sync('foo.json');
```

## History

### 1.0.0

**Breaking changes**

* The main export is now a function
* Use `read()` instead of `read.data()`
* Use `read.sync()` instead of `read.data.sync()`

Everything else is the same.

## About

### Related projects

* [copy](https://www.npmjs.com/package/copy): Copy files or directories using globs. | [homepage](https://github.com/jonschlinkert/copy "Copy files or directories using globs.")
* [read-yaml](https://www.npmjs.com/package/read-yaml): Very thin wrapper around js-yaml for directly reading in YAML files. | [homepage](https://github.com/jonschlinkert/read-yaml "Very thin wrapper around js-yaml for directly reading in YAML files.")
* [write](https://www.npmjs.com/package/write): Write files to disk, creating intermediate directories if they don't exist. | [homepage](https://github.com/jonschlinkert/write "Write files to disk, creating intermediate directories if they don't exist.")

### Contributing

Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).

### Contributors

| **Commits** | **Contributor** | 
| --- | --- |
| 14 | [jonschlinkert](https://github.com/jonschlinkert) |
| 1 | [tunnckoCore](https://github.com/tunnckoCore) |

### Building docs

_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_

To generate the readme, run the following command:

```sh
$ npm install -g verbose/verb#dev verb-generate-readme && verb
```

### Running tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

```sh
$ npm install && npm test
```

### Author

**Jon Schlinkert**

* [github/jonschlinkert](https://github.com/jonschlinkert)
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)

### License

Copyright © 2017, [Jon Schlinkert](http://github.com/jonschlinkert).
Released under the [MIT License](LICENSE).

***

_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.3, on April 02, 2017._