Line Ending Corrector - An utility that makes sure your files have consistent line endings.
npm install line-ending-corrector\r\n (a.k.a CRLF) line endings in Microsoft Windows operating systems into the more commonly used and recognized \n (a.k.a LF). Though it lets you do the opposite as well ( converting LF to CRLF ). It supports \r (a.k.a CR) as well for the sake of completion.
lec. See Here
bash
[sudo] npm install line-ending-corrector
`
Programmatic Usage
To require
CoffeeScript
`CoffeeScript
{ LineEndingCorrector } = require 'line-ending-corrector'
`
JavaScript
`JavaScript
LineEndingCorrector = require('line-ending-corrector').LineEndingCorrector
`
Methods
$3
returns [ wasAltered Boolean, modifiedContents String ]
CoffeeScript
`CoffeeScript
contentsOfSomeFile = arbitaryFunctionToLoadFile()
[ wasAltered, modifiedContents ] = LineEndingCorrector.correctSync contentsOfSomeFile
if wasAltered
arbitaryFunctionToSaveFile modifiedContents
`
JavaScript
`JavaScript
contentsOfSomeFile = arbitaryFunctionToLoadFile();
res = LineEndingCorrector.correctSync(contentsOfSomeFile);
if(res.wasAltered) {
arbitaryFunctionToSaveFile(res.modifiedContents);
}
`
ES6
`JavaScript
contentsOfSomeFile = arbitaryFunctionToLoadFile();
{ wasAltered, modifiedContents } = LineEndingCorrector.correctSync(contentsOfSomeFile);
if(wasAltered) {
arbitaryFunctionToSaveFile(modifiedContents);
}
`
$3
returns modifiedContentStream stream.Readable
CoffeeScript
`CoffeeScript
contentStream = arbitaryFunctionToLoadFileAsStream()
modifiedContentStream = LineEndingCorrector.correctStream contentStream, { encoding: 'utf8', eolc: 'LF'}
arbitaryFunctionToSaveFileFromStream modifiedContents
`
$3
callbackFunction is called with (err Error, wasAltered boolean, modifiedContent String)
CoffeeScript
`CoffeeScript
content = arbitaryFunctionToLoadFile()
LineEndingCorrector.correct content, { eolc: 'LF' }, (err, wasAltered, modifiedContent)=>
throw err if err
if wasAltered
arbitaryFunctionToSaveFileFromStream modifiedContent
`
Options
eolc
Desired End of Line character. can be CR (\r), LF(\n) (Default), CRLF(\r\n)
encoding
Any meaningful encoding that nodejs supports. Default utf8. It is advisable to use utf8 since others are not tested by the devs.
Gulp
See gulp-line-ending-corrector
Testing
You need mocha
npm test`