Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

file-reader

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

file-reader - npm Package Compare versions

Comparing version 0.2.2 to 1.0.0

LICENSE

82

index.js
/*!
* file-reader <https://github.com/jonschlinkert/file-reader>
*
* Copyright (c) 2014 Jon Schlinkert, contributors.
* Copyright (c) 2014-2015, Jon Schlinkert.
* Licensed under the MIT license.

@@ -10,21 +10,36 @@ */

var fs = require('fs');
var path = require('path');
var fs = require('fs-utils');
var mapFiles = require('map-files');
var extend = require('extend-shallow');
var yaml = require('read-yaml');
module.exports = function(patterns, options) {
/**
* Expose `readFiles`
*/
module.exports = readFiles;
function readFiles(patterns, options) {
return mapFiles(patterns, extend({
name: camelize,
read: function(fp) {
var ext = path.extname(fp);
if (!reader.hasOwnProperty(ext)) {
ext = '.txt';
}
return reader[ext](path.resolve(fp));
}
renameKey: camelize,
read: readFile
}, options));
};
}
/**
* Expose `readFile`
*/
module.exports.file = readFile;
function readFile(fp, options) {
var ext = path.extname(fp);
if (!reader.hasOwnProperty(ext)) {
ext = '.txt';
}
return reader[ext](path.resolve(fp), options);
}
/**
* This is just a minimal start, pull requests welcome

@@ -39,15 +54,28 @@ * for adding extensions/readers to the list.

var reader = {
// Functions
'.js' : require,
// requireable
'.js': require,
'.json': require,
// Strings
'.hbs' : fs.readFileSync,
'.md' : fs.readFileSync,
'.tmpl': fs.readFileSync,
'.txt' : fs.readFileSync,
// common string formats
'.txt': readString,
'.md': readString,
'.markdown': readString,
'.mdown': readString,
// Objects
'.json': require,
'.yaml': fs.readYAMLSync,
'.yml' : fs.readYAMLSync,
'.hbs': readString,
'.htm': readString,
'.html': readString,
'.slim': readString,
'.swig': readString,
'.tmpl': readString,
'.css': readString,
'.less': readString,
'.sass': readString,
'.scss': readString,
'.styl': readString,
// common object formats
'.yaml': readYaml,
'.yml': readYaml,
};

@@ -75,1 +103,9 @@

}
function readString(fp) {
return fs.readFileSync(fp, 'utf8');
}
function readYaml(fp, options) {
return yaml.sync(fp);
}
{
"name": "file-reader",
"description": "Read a glob of files, dynamically choosing the reader or requiring the files based on the file extension.",
"version": "0.2.2",
"version": "1.0.0",
"homepage": "https://github.com/jonschlinkert/file-reader",

@@ -17,8 +17,6 @@ "author": {

},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/jonschlinkert/file-reader/blob/master/LICENSE-MIT"
}
],
"license": {
"type": "MIT",
"url": "https://github.com/jonschlinkert/file-reader/blob/master/LICENSE"
},
"main": "index.js",

@@ -31,6 +29,10 @@ "engines": {

},
"dependencies": {
"extend-shallow": "^0.2.0",
"map-files": "^0.3.0",
"read-yaml": "^1.0.0"
},
"devDependencies": {
"mocha": "*",
"should": "^4.0.4",
"verb": ">= 0.2.6"
"should": "^4.6.1"
},

@@ -53,8 +55,3 @@ "keywords": [

"yaml"
],
"dependencies": {
"extend-shallow": "^0.1.1",
"fs-utils": "^0.6.0",
"map-files": "^0.2.0"
}
}
]
}

@@ -5,30 +5,48 @@ # file-reader [![NPM version](https://badge.fury.io/js/file-reader.svg)](http://badge.fury.io/js/file-reader)

## Install
#### Install with [npm](npmjs.org):
## Install with [npm](npmjs.org)
```bash
npm i file-reader --save-dev
npm i file-reader --save
```
## Run tests
## Usage
```bash
npm test
```
Read a glob of files:
## Usage
```js
var reader = require('file-reader');
var read = require('file-reader');
console.log(reader('*.js'));
read('*.js');
//=> { a: [Function: aaa], b: [Function: bbb], c: [Function: ccc] }
console.log(reader('*.txt'));
read('*.txt');
//=> { a: 'AAA', b: 'BBB', c: 'CCC' }
console.log(reader('*.{yml,json}'));
read('*.{yml,json}');
//=> { a: { a: 'a' }, b: { b: 'b' }, c: { c: 'c' } }
```
Read a single file (you must supply the full file path, no glob patterns):
```js
var read = require('file-reader');
read.file('a.js');
//=> { a: [Function: foo] }
read.file('a.txt');
//=> { a: 'foo' }
read('a.yml');
//=> { a: { foo: 'bar' } }
```
## Run tests
Install dev dependencies:
```bash
npm i -d && npm test
```
## Author

@@ -42,3 +60,3 @@

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

@@ -48,2 +66,2 @@

_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on November 02, 2014._
_This file was generated by [verb](https://github.com/assemble/verb) on January 21, 2015._
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