GithubHelp home page GithubHelp logo

sindresorhus / strip-final-newline Goto Github PK

View Code? Open in Web Editor NEW
33.0 7.0 10.0 16 KB

Strip the final newline character from a string/buffer

License: MIT License

JavaScript 88.76% TypeScript 11.24%

strip-final-newline's Introduction

strip-final-newline

Strip the final newline character from a string or Uint8Array.

This can be useful when parsing the output of, for example, ChildProcess#execFile(), as binaries usually output a newline at the end. You cannot use stdout.trimEnd() for this as it removes all trailing newlines and whitespaces at the end.

Install

npm install strip-final-newline

Usage

import stripFinalNewline from 'strip-final-newline';

stripFinalNewline('foo\nbar\n\n');
//=> 'foo\nbar\n'

const uint8Array = new TextEncoder().encode('foo\nbar\n\n')
new TextDecoder().decode(stripFinalNewline(uint8Array));
//=> 'foo\nbar\n'

Performance

When using an Uint8Array, the original value is referenced, not copied. This is much more efficient, requires almost no memory, and remains milliseconds fast even on very large inputs.

If you'd like to ensure that modifying the return value does not also modify the value passed as input, please use .slice().

const value = new TextDecoder().decode(stripFinalNewline(uint8Array).slice());

strip-final-newline's People

Contributors

coreyfarrell avatar ehmicky avatar richienb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

strip-final-newline's Issues

This package is misnamed

It doesn't strip out EOF (end of file) characters. It strips EOL (end of line) characters. An EOF character would mark the end of an entire file or stream, not just the end of a line. There isn't any EOF character in ASCII or Unicode, but EOT (0x04) and SUB (0x1a) come close.

This issue also exists in the readme for the execa package.

How about using `str.trimEnd()`

Normally, you would use stdout.trim(), but that would also remove newlines at the start and whitespace.

i would either recommend that ppl switch to using trimEnd or that you refrace this line to something in lines of:

Normally, you would use stdout.trimEnd(), but that would also remove all trailing newlines and whitespaces at the end.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.