1. line-ending-corrector
Line Ending Corrector - An utility that makes sure your files have consistent line endings.
line-ending-corrector
Package: line-ending-corrector
Created by: iShafayet
Last modified: Sun, 19 Jun 2022 12:28:14 GMT
Version: 1.0.1
License: MIT
Downloads: 21,052
Repository: https://github.com/iShafayet/line-ending-corrector

Install

npm install line-ending-corrector
yarn add line-ending-corrector

line-ending-corrector

Line Ending Corrector - A utility that makes sure your files have consistent line endings.

It converts all those pesky \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.

You should definitely have this in your build process especially if someone in your team works from a non UNIX system.

Features

  • Supports recusive mode for all files in a directory
  • Can be used from the command line using lec. See Here
  • Has a gulp module
  • Exposes a programmatic API.

Installation

For using programmatically

 [sudo] npm install line-ending-corrector

Programmatic Usage

To require

CoffeeScript

 { LineEndingCorrector } = require 'line-ending-corrector'

JavaScript

 LineEndingCorrector = require('line-ending-corrector').LineEndingCorrector

Methods

LineEndingCorrector.correctSync(contents, options)

returns [ wasAltered Boolean, modifiedContents String ]

CoffeeScript

 contentsOfSomeFile = arbitaryFunctionToLoadFile()
[ wasAltered, modifiedContents ] = LineEndingCorrector.correctSync contentsOfSomeFile
if wasAltered
  arbitaryFunctionToSaveFile modifiedContents

JavaScript

 contentsOfSomeFile = arbitaryFunctionToLoadFile();
res = LineEndingCorrector.correctSync(contentsOfSomeFile);
if(res.wasAltered) {
  arbitaryFunctionToSaveFile(res.modifiedContents);
}

ES6

 contentsOfSomeFile = arbitaryFunctionToLoadFile();
{ wasAltered, modifiedContents } = LineEndingCorrector.correctSync(contentsOfSomeFile);
if(wasAltered) {
  arbitaryFunctionToSaveFile(modifiedContents);
}

LineEndingCorrector.correctStream(contentStream, options)

returns modifiedContentStream stream.Readable

CoffeeScript

 contentStream = arbitaryFunctionToLoadFileAsStream()
modifiedContentStream = LineEndingCorrector.correctStream contentStream, { encoding: 'utf8', eolc: 'LF'}
arbitaryFunctionToSaveFileFromStream modifiedContents

LineEndingCorrector.correct(content, options, callbackFunction)

callbackFunction is called with (err Error, wasAltered boolean, modifiedContent String)

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

Contributing

We actively check for issues even for the least used repositories (unless explicitly abandoned). All of our opensource repositories are being used in commercial projects by teamO4 or bbsenterprise. So, it is very likely that we will sort out important issues not long after they are posted.

Please create a github issue if you find a bug or have a feature request.

Pull requests are always welcome for any of our public repos.

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code