0 | |
# read-file [![NPM version](https://badge.fury.io/js/read-file.png)](http://badge.fury.io/js/read-file)
|
|
0 |
# read-file [![NPM version](https://badge.fury.io/js/read-file.svg)](http://badge.fury.io/js/read-file)
|
1 | 1 |
|
2 | |
> Lightweight methods for reading from the file system, async and sync, with extras for stripping byte order marks and normalizing newlines.
|
|
2 |
> Thin wrapper around fs.readFile and fs.readFileSync that also strips byte order marks when `utf8` encoding is chosen. Also optionally replaces windows newlines with unix newlines.
|
3 | 3 |
|
4 | |
## Install
|
5 | |
Install with [npm](npmjs.org):
|
|
4 |
Install with [npm](https://www.npmjs.com/)
|
6 | 5 |
|
7 | |
```bash
|
8 | |
npm i read-file --save-dev
|
|
6 |
```sh
|
|
7 |
$ npm i read-file --save
|
9 | 8 |
```
|
10 | 9 |
|
11 | |
|
12 | |
## api
|
13 | |
#### readFile
|
14 | |
|
15 | |
Read files asynchronously.
|
|
10 |
## Usage
|
16 | 11 |
|
17 | 12 |
```js
|
18 | |
var file = require('read-file');
|
19 | |
file.readFileSync('foo.txt');
|
|
13 |
var read = require('read-file');
|
|
14 |
|
|
15 |
// async
|
|
16 |
read('foo.txt', function(err, buffer) {
|
|
17 |
//=> <Buffer 74 68 69 73 20 69 73 20 66 6f 6f>
|
|
18 |
});
|
|
19 |
|
|
20 |
// sync
|
|
21 |
var buffer = read.sync('foo.txt');
|
|
22 |
//=> <Buffer 74 68 69 73 20 69 73 20 66 6f 6f>
|
20 | 23 |
```
|
21 | 24 |
|
22 | |
#### readFileSync
|
|
25 |
### BOM
|
23 | 26 |
|
24 | |
Read files synchronously.
|
|
27 |
if `utf8` encoding is used, byte order marks will be stripped
|
|
28 |
|
|
29 |
**async**
|
25 | 30 |
|
26 | 31 |
```js
|
27 | |
var file = require('read-file');
|
28 | |
file.readFile('foo.txt', callback);
|
|
32 |
read('foo.txt', 'utf8', function(err, buffer) {
|
|
33 |
//=> 'some contents...'
|
|
34 |
});
|
|
35 |
|
|
36 |
// or
|
|
37 |
read('foo.txt', {encoding: 'utf8'} function(err, buffer) {
|
|
38 |
//=> 'some contents...'
|
|
39 |
});
|
29 | 40 |
```
|
30 | 41 |
|
31 | |
#### encoding
|
32 | |
|
33 | |
Default encoding is `utf8`, this can be changed by passing an encoding as a second param:
|
|
42 |
**sync**
|
34 | 43 |
|
35 | 44 |
```js
|
36 | |
var file = require('read-file');
|
37 | |
file.readFileSync('foo.txt', 'utf8');
|
|
45 |
read.sync('foo.txt', 'utf8');
|
38 | 46 |
// or
|
39 | |
file.readFile('foo.txt', 'utf8', callback);
|
|
47 |
read('foo.txt', {encoding: 'utf8'});
|
40 | 48 |
```
|
41 | 49 |
|
42 | |
#### normalizeNL
|
|
50 |
### options.normalize
|
43 | 51 |
|
44 | |
Normalize all line endings to newlines, `\n`.
|
|
52 |
Pass `{ normalize: true }` on the options to strip windows carriage returns. This will also return a `utf8` string.
|
45 | 53 |
|
46 | |
```js
|
47 | |
var file = require('read-file');
|
48 | |
var str = file.readFileSync('foo.txt');
|
49 | |
file.normalizeNL(str);
|
|
54 |
## Related projects
|
|
55 |
|
|
56 |
* [copy](https://github.com/jonschlinkert/copy): Copy files or directories using globs.
|
|
57 |
* [read-yaml](https://github.com/jonschlinkert/read-yaml): Very thin wrapper around js-yaml for directly reading in YAML files.
|
|
58 |
* [read-data](https://github.com/jonschlinkert/read-data): Read JSON or YAML files.
|
|
59 |
* [write](https://github.com/jonschlinkert/write): Write files to disk, creating intermediate directories if they don't exist.
|
|
60 |
|
|
61 |
## Running tests
|
|
62 |
|
|
63 |
Install dev dependencies:
|
|
64 |
|
|
65 |
```sh
|
|
66 |
$ npm i -d && npm test
|
50 | 67 |
```
|
51 | 68 |
|
52 | |
#### stripBOM
|
|
69 |
## Contributing
|
53 | 70 |
|
54 | |
Strip byte order marks.
|
|
71 |
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/read-file/issues/new)
|
55 | 72 |
|
56 | |
```js
|
57 | |
var file = require('read-file');
|
58 | |
var str = file.readFileSync('foo.txt');
|
59 | |
file.stripBOM(str);
|
60 | |
```
|
61 | |
|
62 | |
|
63 | |
## Authors
|
|
73 |
## Author
|
64 | 74 |
|
65 | 75 |
**Jon Schlinkert**
|
66 | 76 |
|
67 | 77 |
+ [github/jonschlinkert](https://github.com/jonschlinkert)
|
68 | 78 |
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
|
69 | 79 |
|
70 | |
**Brian Woodward**
|
|
80 |
## License
|
71 | 81 |
|
72 | |
+ [github/doowb](https://github.com/doowb)
|
73 | |
+ [twitter/doowb](http://twitter.com/jonschlinkert)
|
74 | |
|
75 | |
## License
|
76 | |
Copyright (c) 2014 Jon Schlinkert, contributors.
|
77 | |
Released under the MIT license
|
|
82 |
Copyright © 2015 Jon Schlinkert
|
|
83 |
Released under the MIT license.
|
78 | 84 |
|
79 | 85 |
***
|
80 | 86 |
|
81 | |
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on April 09, 2014._⏎
|
|
87 |
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on July 17, 2015._⏎
|