Skip to content

jonschlinkert/file-normalize

Repository files navigation

file-normalize NPM version NPM monthly downloads NPM total downloads Linux Build Status Windows Build Status

File system utils for normalizing things like eol, encoding and BOM.

Install

Install with npm:

$ npm install --save file-normalize

Install with yarn:

$ yarn add file-normalize

API

Normalize slashes in the given filepath to forward slashes. Note that this is a simple replacement of \\ with /, and this method does not check for URL or windows drive characters.

Params

  • filepath {String}
  • returns {String}

Example

var file = require('file-normalize');
console.log(file.normalizeSlash('foo\\bar'));
//=> 'foo/bar'

Normalize line endings to use the given eol character, or the defaults of the current operating system.

Params

  • str {String}
  • returns {String}

Example

var fs = require('fs');
var file = require('file-normalize');
var str = fs.readFileSync('foo.txt', 'utf8');
console.log(file.normalizeEOL(str));

Normalize all line endings to unix newlines (strips carriage returns).

Params

  • str {String}
  • returns {String}

Example

var fs = require('fs');
var file = require('file-normalize');
var str = fs.readFileSync('foo.txt', 'utf8');
console.log(file.normalizeNL(str));

Strip byte-order marks.

Params

  • val {String|Buffer}
  • returns {String}

Example

var fs = require('fs');
var file = require('file-normalize');
var str = file.readFileSync('foo.txt', 'utf8');
console.log(file.stripBOM(str));

Append a string or buffer to another string or buffer ensuring to preserve line ending characters.

Params

  • prefix {String|Buffer}: String or Buffer that will be used to check for an existing line ending. The suffix is appended to this.
  • suffix {String|Buffer}: String or Buffer that will be appended to the prefix.
  • returns {String|Buffer}: Final String or Buffer

Example

console.log([append(new Buffer('abc\r\n'), new Buffer('def')).toString()]);
//=> [ 'abc\r\ndef\r\n' ]

console.log([append(new Buffer('abc\n'), new Buffer('def')).toString()]);
//=> [ 'abc\ndef\n' ]

// uses os.EOL when a line ending is not found
console.log([append(new Buffer('abc'), new Buffer('def')).toString()]);
//=> [ 'abc\ndef' ]

console.log([append('abc\r\n', 'def')]);
//=> [ 'abc\r\ndef\r\n' ]

console.log([append('abc\n', 'def')]);
//=> [ 'abc\ndef\n' ]

// uses os.EOL when a line ending is not found
console.log([append('abc', 'def')]);
//=> [ 'abc\ndef' ]

Append a string to another string ensuring to preserve line ending characters.

Params

  • str {String}: String that will be used to check for an existing line ending. The suffix is appended to this.
  • suffix {String}: String that will be appended to the str.
  • returns {String}: Final String

Example

console.log([appendString('abc\r\n', 'def')]);
//=> [ 'abc\r\ndef\r\n' ]

console.log([appendString('abc\n', 'def')]);
//=> [ 'abc\ndef\n' ]

// uses os.EOL when a line ending is not found
console.log([appendString('abc', 'def')]);
//=> [ 'abc\ndef' ]

Append a buffer to another buffer ensuring to preserve line ending characters.

Params

  • buf {Buffer}: Buffer that will be used to check for an existing line ending. The suffix is appended to this.
  • suffix {Buffer}: Buffer that will be appended to the buf.
  • returns {Buffer}: Final Buffer

Example

console.log([appendBuffer(new Buffer('abc\r\n'), new Buffer('def')).toString()]);
//=> [ 'abc\r\ndef\r\n' ]

console.log([appendBuffer(new Buffer('abc\n'), new Buffer('def')).toString()]);
//=> [ 'abc\ndef\n' ]

// uses os.EOL when a line ending is not found
console.log([appendBuffer(new Buffer('abc'), new Buffer('def')).toString()]);
//=> [ 'abc\ndef' ]

About

Related projects

  • fs-utils: fs extras and utilities to extend the node.js file system module. Used in Assemble and… more | homepage
  • normalize-path: Normalize file path slashes to be unix-like forward slashes. Also condenses repeat slashes to a… more | homepage
  • parse-filepath: Pollyfill for node.js path.parse, parses a filepath into an object. | homepage

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Contributors

Commits Contributor
15 jonschlinkert
6 doowb
1 phated

Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Running tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test

Author

Jon Schlinkert

License

Copyright © 2017, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on August 01, 2017.

Sponsor this project

 

Packages

No packages published