Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
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 0.2.0 to 0.3.0

LICENSE

263

index.js

@@ -1,6 +0,6 @@

/**
* read-data <https://github.com/assemble/read-data>
/*!
* read-data <https://github.com/jonschlinkert/read-data>
*
* Copyright (c) 2014 Jon Schlinkert, Brian Woodward, contributors.
* Licensed under the MIT license.
* Copyright (c) 2014-2015, Jon Schlinkert.
* Licensed under the MIT License.
*/

@@ -10,178 +10,177 @@

var fs = require('fs');
var path = require('path');
var fs = require('graceful-fs');
var async = require('async');
var YAML = require('js-yaml');
var yaml = require('read-yaml');
/**
* Read JSON file synchronously and parse content as JSON
* Asynchronously read a YAML file.
*
* @param {String} `filepath`
* @return {String}
* @api public
*/
exports.readJSONSync = function(filepath) {
return JSON.parse(read(filepath));
};
/**
* Read JSON file asynchronously and parse content as JSON
* ```js
* var yaml = require('read-data').yaml;
*
* @param {String} `filepath`
* @param {Function} `callback`
* yaml('foo.yml', function(err, data) {
* if (err) throw err;
* console.log(data);
* });
* ```
*
* @name .yaml
* @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
*/
exports.readJSON = function(filepath, callback) {
async.waterfall([
function(next) {
fs.readFile(filepath, 'utf8', next);
},
function(contents, next) {
try {
next(null, JSON.parse(contents));
} catch (err) {
err.message = 'Failed to parse "' + filepath + '": ' + err.message;
next(err);
}
}
], callback);
};
exports.yaml = yaml;
/**
* Read YAML file synchronously and parse content as JSON
* Synchronously read a YAML file.
*
* @param {String} `filepath`
* @param {Function} `callback`
* ```js
* var yaml = require('read-data').yaml;
* var data = yaml.sync('foo.yml');
* ```
*
* @name ..yaml.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.readYAMLSync = function(filepath) {
return YAML.safeLoad(read(filepath));
};
exports.yaml.sync = yaml.sync;
/**
* Read YAML file synchronously and parse content as JSON
* Asynchronously read a JSON file.
*
* @param {String} `filepath`
* @param {Function} `callback`
* ```js
* var json = require('read-data');
*
* json('foo.json', function(err, data) {
* if (err) throw err;
* console.log(data);
* });
* ```
*
* @name .json
* @param {String} `fp` path of the file to read.
* @param {Function} `callback` callback function
* @return {Object} JSON
* @api public
*/
exports.readYAML = function(filepath, callback) {
async.waterfall([
function(next) {
fs.readFile(filepath, 'utf8', next);
},
function(contents, next) {
try {
next(null, YAML.safeLoad(contents));
} catch (err) {
err.message = 'Failed to parse "' + filepath + '": ' + err.message;
next(err);
}
}
], callback);
exports.json = function json(fp, opts, cb) {
if (typeof opts === 'function') {
cb = opts; opts = {};
}
// 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));
});
};
/**
* Determine the reader based on extension.
* Synchronously read a JSON file.
*
* @param {String} `filepath`
* @param {Function} `callback`
* ```js
* var json = require('read-data').json;
* var data = json.sync('foo.json');
* ```
*
* @name .json.sync
* @param {String} `fp` path of the file to read.
* @return {Object} JSON
* @api public
*/
exports.readDataSync = function(filepath, options) {
options = options || {};
var ext = options.lang || path.extname(filepath).replace(/\./, '');
var reader = exports.readJSONSync;
switch (ext) {
case 'json':
reader = exports.readJSONSync;
break;
case 'yml':
case 'yaml':
reader = exports.readYAMLSync;
break;
exports.json.sync = function jsonSync(fp) {
try {
return JSON.parse(fs.readFileSync(fp, 'utf8'));
} catch (err) {
err.message = 'read-data failed to parse "' + fp + '": ' + err.message;
throw err;
}
return reader(filepath, options);
};
/**
* Determine the reader based on extension (async).
* Asynchronously read a JSON or YAML file, automatically determining the
* reader based on extension.
*
* @param {String} `filepath`
* @param {Function} `callback`
* ```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
*/
exports.readData = function(filepath, options, callback) {
if (options && typeof options === 'function') {
callback = options;
options = {};
exports.data = function data(fp, opts, cb) {
if (opts && typeof opts === 'function') {
cb = opts;
opts = {};
}
var ext = options.lang || path.extname(filepath).replace(/\./, '');
var reader = exports.readJSON;
opts = opts || {};
var ext = opts.lang || path.extname(fp);
var reader = exports.json;
switch (ext) {
case 'json':
reader = exports.readJSON;
case '.json':
reader = exports.json;
break;
case 'yml':
case 'yaml':
reader = exports.readYAML;
case '.yml':
case '.yaml':
reader = exports.yaml;
break;
}
reader(filepath, callback);
reader(fp, opts, cb);
};
/**
* Read optional JSON (Ben Alman <https://gist.github.com/2876125>)
* Synchronously read a data file, automatically determining the
* reader based on extension.
*
* @param {String} `filepath`
* @return {String}
* @api public
*/
exports.readOptionalJSON = function(filepath) {
var buffer = {};
try {
buffer = exports.readJSONSync(filepath);
} catch (e) {}
return buffer;
};
/**
* Read optional YAML (Ben Alman <https://gist.github.com/2876125>)
* ```js
* var data = require('read-data').data;
*
* @param {String} `filepath`
* @return {String}
* 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.readOptionalYAML = function(filepath) {
var buffer = {};
try {
buffer = exports.readYAMLSync(filepath);
} catch (e) {}
return buffer;
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);
};
/**
* Read a file synchronously.
*
* @param {String} `filepath`
* @return {String}
* @api private
*/
function read(filepath) {
try {
return fs.readFileSync(filepath, 'utf8');
} catch (err) {
err.message = 'Failed to parse "' + filepath + '": ' + err.message;
throw err;
}
}
{
"name": "read-data",
"description": "Utils for reading JSON and YAML data files.",
"version": "0.2.0",
"description": "Read JSON or YAML files.",
"version": "0.3.0",
"homepage": "https://github.com/jonschlinkert/read-data",
"author": {
"name": "Jon Schlinkert",
"url": "https://github.com/jonschlinkert"
"url": "http://github.com/jonschlinkert"
},

@@ -17,36 +17,32 @@ "repository": {

},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/jonschlinkert/read-data/blob/master/LICENSE-MIT"
}
"license": {
"type": "MIT",
"url": "https://github.com/jonschlinkert/read-data/blob/master/LICENSE"
},
"files": [
"index.js"
],
"main": "index.js",
"engines": {
"node": ">=0.8"
},
"scripts": {
"test": "mocha -R test"
"test": "mocha"
},
"dependencies": {
"read-yaml": "^1.0.0"
},
"devDependencies": {
"should": "^4.1.0",
"verb": "^0.2.15",
"verb-tag-jscomments": "^0.2.2"
"js-yaml": "^3.2.7",
"mocha": "^2.2.1",
"should": "^5.2.0"
},
"dependencies": {
"async": "^0.9.0",
"graceful-fs": "^3.0.4",
"js-yaml": "^3.2.2"
},
"keywords": [
"async",
"data",
"file",
"filepath",
"fs",
"json",
"parse",
"read",
"reader",
"sync",
"system",
"yaml"
]
}
}

@@ -1,7 +0,6 @@

# read-data [![NPM version](https://badge.fury.io/js/read-data.svg)](http://badge.fury.io/js/read-data)
# 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)
> Utils for reading JSON and YAML data files.
> Read JSON or YAML files.
## Install
#### Install with [npm](npmjs.org)
## Install with [npm](npmjs.org)

@@ -12,63 +11,123 @@ ```bash

## api
### [.readJSONSync](index.js#L23)
## API
### [.yaml](./index.js#L34)
* `filepath` **{String}**
* `returns`: {String}
Asynchronously read a YAML file.
Read JSON file synchronously and parse content as JSON
* `fp` **{String}**: path of the file to read.
* `options` **{Object|String}**: to pass to [js-yaml]
* `cb` **{Function}**: callback function
* `returns` **{Object}**: JSON
### [.readJSON](index.js#L35)
```js
var yaml = require('read-data').yaml;
* `filepath` **{String}**
* `callback` **{Function}**
yaml('foo.yml', function(err, data) {
if (err) throw err;
console.log(data);
});
```
Read JSON file asynchronously and parse content as JSON
### [.yaml.sync](./index.js#L51)
### [.readYAMLSync](index.js#L59)
Synchronously read a YAML file.
* `filepath` **{String}**
* `callback` **{Function}**
* `fp` **{String}**: path of the file to read.
* `options` **{Object|String}**: to pass to [js-yaml]
* `returns` **{Object}**: JSON
Read YAML file synchronously and parse content as JSON
```js
var yaml = require('read-data').yaml;
var data = yaml.sync('foo.yml');
```
### [.readYAML](index.js#L71)
### [.json](./index.js#L72)
* `filepath` **{String}**
* `callback` **{Function}**
Asynchronously read a JSON file.
Read YAML file synchronously and parse content as JSON
* `fp` **{String}**: path of the file to read.
* `callback` **{Function}**: callback function
* `returns` **{Object}**: JSON
### [.readDataSync](index.js#L95)
```js
var json = require('read-data');
* `filepath` **{String}**
* `callback` **{Function}**
json('foo.json', function(err, data) {
if (err) throw err;
console.log(data);
});
```
Determine the reader based on extension.
### [.json.sync](./index.js#L98)
### [.readData](index.js#L119)
Synchronously read a JSON file.
* `filepath` **{String}**
* `callback` **{Function}**
* `fp` **{String}**: path of the file to read.
* `returns` **{Object}**: JSON
Determine the reader based on extension (async).
```js
var json = require('read-data').json;
var data = json.sync('foo.json');
```
### [.readOptionalJSON](index.js#L147)
### [.data](./index.js#L133)
* `filepath` **{String}**
* `returns`: {String}
Asynchronously read a JSON or YAML file, automatically determining the reader based on extension.
Read optional JSON (Ben Alman <https://gist.github.com/2876125>)
* `fp` **{String}**: path of the file to read.
* `options` **{Object|String}**: to pass to [js-yaml]
* `cb` **{Function}**: callback function
* `returns` **{Object}**: JSON
### [.readOptionalYAML](index.js#L163)
```js
var read = require('read-data');
* `filepath` **{String}**
* `returns`: {String}
read('foo.json', function(err, data) {
if (err) throw err;
console.log(data);
});
Read optional YAML (Ben Alman <https://gist.github.com/2876125>)
read('foo.yml', function(err, data) {
if (err) throw err;
console.log(data);
});
```
## Authors
### [.data.sync](./index.js#L171)
Synchronously read a data file, automatically determining the reader based on extension.
* `fp` **{String}**: path of the file to read.
* `options` **{Object|String}**: to pass to [js-yaml]
* `returns` **{Object}**: JSON
```js
var data = require('read-data').data;
var yaml = data.sync('foo.yml');
var json = data.sync('foo.json');
```
## 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
Install dev dependencies:
```bash
npm i -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
**Jon Schlinkert**
+ [github/jonschlinkert](https://github.com/jonschlinkert)

@@ -78,3 +137,3 @@ + [twitter/jonschlinkert](http://twitter.com/jonschlinkert)

## License
Copyright (c) 2014 Jon Schlinkert, contributors.
Copyright (c) 2015 Jon Schlinkert
Released under the MIT license

@@ -84,2 +143,4 @@

_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on October 24, 2014._
_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
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc