Socket
Book a DemoInstallSign in
Socket

read-data

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

read-data - npm Package Compare versions

Comparing version

to
1.0.0

180

index.js

@@ -13,4 +13,66 @@ /*!

var yaml = require('read-yaml');
var extend = require('extend-shallow');
/**
* Asynchronously read a JSON or YAML file, automatically determining the
* reader based on extension.
*
* ```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);
* });
* ```
*
* @name read
* @param {String} `filepath` path of the file to read.
* @param {Object|String} `options` to pass to [js-yaml]
* @param {Function} `cb` callback function
* @return {Object} JSON
* @api public
*/
var read = module.exports = function read(filepath, options, cb) {
if (options && typeof options === 'function') {
cb = options;
options = {};
}
var opts = extend({}, options);
var ext = extname(opts.lang || path.extname(filepath));
read[ext](filepath, options, cb);
};
/**
* Synchronously read a `.json` or `.(yaml|yml)` file, automatically determining the
* reader based on extension.
*
* ```js
* var data = require('read-data').data;
*
* var yaml = data.sync('foo.yml');
* var json = data.sync('foo.json');
* ```
*
* @name .sync
* @param {String} `filepath` path of the file to read.
* @param {Object|String} `options` to pass to [js-yaml]
* @return {Object} JSON
* @api public
*/
read.sync = function(filepath, options) {
var opts = extend({}, options);
var ext = extname(opts.lang || path.extname(filepath));
return read[ext].sync(filepath, options);
};
/**
* Asynchronously read a YAML file.

@@ -28,3 +90,3 @@ *

* @name .yaml
* @param {String} `fp` path of the file to read.
* @param {String} `filepath` path of the file to read.
* @param {Object|String} `options` to pass to [js-yaml]

@@ -36,3 +98,3 @@ * @param {Function} `cb` callback function

exports.yaml = yaml;
read.yaml = yaml;

@@ -48,3 +110,3 @@ /**

* @name ..yaml.sync
* @param {String} `fp` path of the file to read.
* @param {String} `filepath` path of the file to read.
* @param {Object|String} `options` to pass to [js-yaml]

@@ -55,3 +117,3 @@ * @return {Object} JSON

exports.yaml.sync = yaml.sync;
read.yaml.sync = yaml.sync;

@@ -71,3 +133,3 @@ /**

* @name .json
* @param {String} `fp` path of the file to read.
* @param {String} `filepath` path of the file to read.
* @param {Function} `callback` callback function

@@ -78,11 +140,17 @@ * @return {Object} JSON

exports.json = function json(fp, opts, cb) {
if (typeof opts === 'function') {
cb = opts; opts = {};
read.json = function json(filepath, options, cb) {
if (typeof options === 'function') {
cb = options;
options = {};
}
// opts param exists to maintain the same arity as the
// yaml method, so we can dynamically choose the reader
fs.readFile(fp, 'utf8', function (err, data) {
if (err) cb(err);
cb(null, JSON.parse(data));
fs.readFile(filepath, options, function(err, buf) {
if (err) {
cb(err);
return;
}
cb(null, JSON.parse(buf.toString()));
});

@@ -100,3 +168,3 @@ };

* @name .json.sync
* @param {String} `fp` path of the file to read.
* @param {String} `filepath` path of the file to read.
* @return {Object} JSON

@@ -106,7 +174,7 @@ * @api public

exports.json.sync = function jsonSync(fp) {
read.json.sync = function jsonSync(filepath) {
try {
return JSON.parse(fs.readFileSync(fp, 'utf8'));
return JSON.parse(fs.readFileSync(filepath, 'utf8'));
} catch (err) {
err.message = 'read-data failed to parse "' + fp + '": ' + err.message;
err.message = 'read-data failed to parse "' + filepath + '": ' + err.message;
throw err;

@@ -117,79 +185,9 @@ }

/**
* Asynchronously read a JSON or YAML file, automatically determining the
* reader based on extension.
*
* ```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);
* });
* ```
*
* @name .data
* @param {String} `fp` path of the file to read.
* @param {Object|String} `options` to pass to [js-yaml]
* @param {Function} `cb` callback function
* @return {Object} JSON
* @api public
* Get the extname without leading `.`
*/
exports.data = function data(fp, opts, cb) {
if (opts && typeof opts === 'function') {
cb = opts;
opts = {};
}
opts = opts || {};
var ext = opts.lang || path.extname(fp);
var reader = exports.json;
switch (ext) {
case '.json':
reader = exports.json;
break;
case '.yml':
case '.yaml':
reader = exports.yaml;
break;
}
reader(fp, opts, cb);
};
/**
* Synchronously read a data file, automatically determining the
* reader based on extension.
*
* ```js
* var data = require('read-data').data;
*
* var yaml = data.sync('foo.yml');
* var json = data.sync('foo.json');
* ```
*
* @name .data.sync
* @param {String} `fp` path of the file to read.
* @param {Object|String} `options` to pass to [js-yaml]
* @return {Object} JSON
* @api public
*/
exports.data.sync = function dataSync(fp, opts) {
opts = opts || {};
var ext = opts.lang || path.extname(fp);
var reader = exports.json.sync;
switch(ext) {
case '.json':
reader = exports.json.sync;
break;
case '.yml':
case '.yaml':
reader = exports.yaml.sync;
break;
}
return reader(fp, opts);
};
function extname(ext) {
var str = ext.charAt(0) === '.' ? ext.slice(1) : ext;
if (str === 'yml') str = 'yaml';
return str;
}
{
"name": "read-data",
"description": "Read JSON or YAML files.",
"version": "0.3.0",
"version": "1.0.0",
"homepage": "https://github.com/jonschlinkert/read-data",
"author": {
"name": "Jon Schlinkert",
"url": "http://github.com/jonschlinkert"
},
"repository": {
"type": "git",
"url": "git://github.com/jonschlinkert/read-data.git"
},
"author": "Jon Schlinkert (http://github.com/jonschlinkert)",
"repository": "jonschlinkert/read-data",
"bugs": {
"url": "https://github.com/jonschlinkert/read-data/issues"
},
"license": {
"type": "MIT",
"url": "https://github.com/jonschlinkert/read-data/blob/master/LICENSE"
},
"license": "MIT",
"files": [
"index.js"
],
"main": "index.js",
"engines": {
"node": ">= 0.10.0"
},
"scripts": {

@@ -28,8 +23,9 @@ "test": "mocha"

"dependencies": {
"extend-shallow": "^2.0.1",
"read-yaml": "^1.0.0"
},
"devDependencies": {
"js-yaml": "^3.2.7",
"mocha": "^2.2.1",
"should": "^5.2.0"
"gulp-format-md": "^0.1.11",
"js-yaml": "^3.7.0",
"mocha": "^3.2.0"
},

@@ -47,3 +43,24 @@ "keywords": [

"yaml"
]
],
"verb": {
"toc": false,
"layout": "default",
"tasks": [
"readme"
],
"plugins": [
"gulp-format-md"
],
"lint": {
"reflinks": true
},
"related": {
"list": []
},
"reflinks": [
"js-yaml",
"verb",
"verb-generate-readme"
]
}
}

@@ -1,20 +0,95 @@

# read-data [![NPM version](https://badge.fury.io/js/read-data.svg)](http://badge.fury.io/js/read-data) [![Build Status](https://travis-ci.org/jonschlinkert/read-data.svg)](https://travis-ci.org/jonschlinkert/read-data)
# 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 with [npm](npmjs.org)
## Install
```bash
npm i read-data --save
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
### [.yaml](./index.js#L34)
### [read](index.js#L41)
Asynchronously read a JSON or YAML file, automatically determining the reader based on extension.
**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);
});
```
**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
### [.sync](index.js#L70)
Synchronously read a `.json` or `.(yaml|yml)` file, automatically determining the reader based on extension.
**Example**
```js
var data = require('read-data').data;
var yaml = data.sync('foo.yml');
var json = data.sync('foo.json');
```
**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
### [.yaml](index.js#L96)
Asynchronously read a YAML file.
* `fp` **{String}**: path of the file to read.
* `options` **{Object|String}**: to pass to [js-yaml]
* `cb` **{Function}**: callback function
* `returns` **{Object}**: JSON
**Example**

@@ -30,9 +105,14 @@ ```js

### [.yaml.sync](./index.js#L51)
**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
### [.yaml.sync](index.js#L113)
Synchronously read a YAML file.
* `fp` **{String}**: path of the file to read.
* `options` **{Object|String}**: to pass to [js-yaml]
* `returns` **{Object}**: JSON
**Example**

@@ -44,9 +124,13 @@ ```js

### [.json](./index.js#L72)
**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
### [.json](index.js#L134)
Asynchronously read a JSON file.
* `fp` **{String}**: path of the file to read.
* `callback` **{Function}**: callback function
* `returns` **{Object}**: JSON
**Example**

@@ -62,8 +146,13 @@ ```js

### [.json.sync](./index.js#L98)
**Params**
* `filepath` **{String}**: path of the file to read.
* `callback` **{Function}**: callback function
* `returns` **{Object}**: JSON
### [.json.sync](index.js#L166)
Synchronously read a JSON file.
* `fp` **{String}**: path of the file to read.
* `returns` **{Object}**: JSON
**Example**

@@ -75,73 +164,64 @@ ```js

### [.data](./index.js#L133)
**Params**
Asynchronously read a JSON or YAML file, automatically determining the reader based on extension.
* `filepath` **{String}**: path of the file to read.
* `returns` **{Object}**: JSON
* `fp` **{String}**: path of the file to read.
* `options` **{Object|String}**: to pass to [js-yaml]
* `cb` **{Function}**: callback function
* `returns` **{Object}**: JSON
## History
```js
var read = require('read-data');
### 1.0.0
read('foo.json', function(err, data) {
if (err) throw err;
console.log(data);
});
**Breaking changes**
read('foo.yml', function(err, data) {
if (err) throw err;
console.log(data);
});
```
* The main export is now a function
* Use `read()` instead of `read.data()`
* Use `read.sync()` instead of `read.data.sync()`
### [.data.sync](./index.js#L171)
Everything else is the same.
Synchronously read a data file, automatically determining the reader based on extension.
## About
* `fp` **{String}**: path of the file to read.
* `options` **{Object|String}**: to pass to [js-yaml]
* `returns` **{Object}**: JSON
### Contributing
```js
var data = require('read-data').data;
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
var yaml = data.sync('foo.yml');
var json = data.sync('foo.json');
### Contributors
| **Commits** | **Contributor**<br/> |
| --- | --- |
| 10 | [jonschlinkert](https://github.com/jonschlinkert) |
| 1 | [tunnckoCore](https://github.com/tunnckoCore) |
### Building docs
_(This document was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme) (a [verb](https://github.com/verbose/verb) generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_
To generate the readme and API documentation with [verb](https://github.com/verbose/verb):
```sh
$ npm install -g verb verb-generate-readme && verb
```
## Related projects
* [read-yaml](https://github.com/jonschlinkert/read-yaml): Very thin wrapper around js-yaml for directly reading in YAML files.
* [map-files](https://github.com/jonschlinkert/map-files): Return an object for a glob of files. Pass a `rename` function for the keys, or a `parse` function for the content, allowing it to be used for readable or require-able files.
* [data-store](https://github.com/jonschlinkert/data-store): Easily get, set and persist config data.
* [write-json](https://github.com/jonschlinkert/write-json): Write a JSON to file disk, also creates directories in the dest path if they don't already exist.
* [write-yaml](https://github.com/jonschlinkert/write-yaml): Write YAML. Converts JSON to YAML writes it to the specified file.
* [plasma](https://github.com/jonschlinkert/plasma): Load data from globs or files or directly from objects.
### Running tests
## Running tests
Install dev dependencies:
```bash
npm i -d && npm test
```sh
$ npm install -d && npm test
```
## Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/read-data/issues)
### Author
## Author
**Jon Schlinkert**
+ [github/jonschlinkert](https://github.com/jonschlinkert)
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
* [github/jonschlinkert](https://github.com/jonschlinkert)
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
## License
Copyright (c) 2015 Jon Schlinkert
Released under the MIT license
### License
Copyright © 2016, [Jon Schlinkert](http://github.com/jonschlinkert).
Released under the [MIT license](LICENSE).
***
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on March 31, 2015._
[read-yaml]: https://github.com/jonschlinkert/read-yaml
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.2.1, on December 30, 2016._

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.