Codebase list node-read-file / b3a3043
Imported Upstream version 0.2.0 Thorsten Alteholz 8 years ago
4 changed file(s) with 214 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 The MIT License (MIT)
1
2 Copyright (c) 2014, 2015 Jon Schlinkert.
3
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation
6 files (the "Software"), to deal in the Software without
7 restriction, including without limitation the rights to use,
8 copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following
11 conditions:
12
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
0 # read-file [![NPM version](https://badge.fury.io/js/read-file.svg)](http://badge.fury.io/js/read-file)
1
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
4 Install with [npm](https://www.npmjs.com/)
5
6 ```sh
7 $ npm i read-file --save
8 ```
9
10 ## Usage
11
12 ```js
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>
23 ```
24
25 ### BOM
26
27 if `utf8` encoding is used, byte order marks will be stripped
28
29 **async**
30
31 ```js
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 });
40 ```
41
42 **sync**
43
44 ```js
45 read.sync('foo.txt', 'utf8');
46 // or
47 read('foo.txt', {encoding: 'utf8'});
48 ```
49
50 ### options.normalize
51
52 Pass `{ normalize: true }` on the options to strip windows carriage returns. This will also return a `utf8` string.
53
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
67 ```
68
69 ## Contributing
70
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)
72
73 ## Author
74
75 **Jon Schlinkert**
76
77 + [github/jonschlinkert](https://github.com/jonschlinkert)
78 + [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
79
80 ## License
81
82 Copyright © 2015 Jon Schlinkert
83 Released under the MIT license.
84
85 ***
86
87 _This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on July 17, 2015._
0 /**
1 * read-file <https://github.com/assemble/read-file>
2 *
3 * Copyright (c) 2014, 2015 Jon Schlinkert.
4 * Licensed under the MIT license.
5 */
6
7 var fs = require('fs');
8
9 function read(fp, opts, cb) {
10 if (typeof opts === 'function') {
11 cb = opts;
12 opts = {};
13 }
14
15 if (typeof cb !== 'function') {
16 throw new TypeError('read-file async expects a callback function.');
17 }
18
19 if (typeof fp !== 'string') {
20 cb(new TypeError('read-file async expects a string.'));
21 }
22
23 fs.readFile(fp, opts, function (err, buffer) {
24 if (err) return cb(err);
25 cb(null, normalize(buffer, opts));
26 });
27 }
28
29 read.sync = function(fp, opts) {
30 if (typeof fp !== 'string') {
31 throw new TypeError('read-file sync expects a string.');
32 }
33 try {
34 return normalize(fs.readFileSync(fp, opts), opts);
35 } catch (err) {
36 err.message = 'Failed to read "' + fp + '": ' + err.message;
37 throw new Error(err);
38 }
39 };
40
41 function normalize(str, opts) {
42 str = stripBom(str);
43 if (typeof opts === 'object' && opts.normalize === true) {
44 return String(str).replace(/\r\n|\n/g, '\n');
45 }
46 return str;
47 }
48
49 function stripBom(str) {
50 return typeof str === 'string' && str.charAt(0) === '\uFEFF'
51 ? str.slice(1)
52 : str;
53 }
54
55 /**
56 * Expose `read`
57 */
58
59 module.exports = read;
0 {
1 "name": "read-file",
2 "description": "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 "version": "0.2.0",
4 "homepage": "https://github.com/jonschlinkert/read-file",
5 "author": "Jon Schlinkert (https://github.com/jonschlinkert)",
6 "repository": "jonschlinkert/read-file",
7 "bugs": {
8 "url": "https://github.com/jonschlinkert/read-file/issues"
9 },
10 "license": "MIT",
11 "files": [
12 "index.js"
13 ],
14 "main": "index.js",
15 "engines": {
16 "node": ">=0.8"
17 },
18 "scripts": {
19 "test": "mocha"
20 },
21 "keywords": [
22 "bom",
23 "file",
24 "fs",
25 "path",
26 "read",
27 "util",
28 "readfile",
29 "readfilesync"
30 ],
31 "verb": {
32 "related": {
33 "list": [
34 "read-yaml",
35 "read-data",
36 "write",
37 "copy"
38 ]
39 }
40 }
41 }