├── .npmrc ├── .gitattributes ├── .gitignore ├── .github ├── security.md └── workflows │ └── main.yml ├── .editorconfig ├── index.test-d.ts ├── index.d.ts ├── index.js ├── package.json ├── license ├── readme.md └── test.js /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType, expectError} from 'tsd'; 2 | import stripFinalNewline from './index.js'; 3 | 4 | expectType<''>(stripFinalNewline('')); 5 | expectType(stripFinalNewline(new TextEncoder().encode(''))); 6 | expectError(stripFinalNewline(true)); 7 | expectError(stripFinalNewline(new Uint16Array(new ArrayBuffer(0)))); 8 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Strip the final [newline character](https://en.wikipedia.org/wiki/Newline) from a string or Uint8Array. 3 | 4 | @returns The input without any final newline. 5 | 6 | @example 7 | ``` 8 | import stripFinalNewline from 'strip-final-newline'; 9 | 10 | stripFinalNewline('foo\nbar\n\n'); 11 | //=> 'foo\nbar\n' 12 | 13 | const uint8Array = new TextEncoder().encode('foo\nbar\n\n') 14 | new TextDecoder().decode(stripFinalNewline(uint8Array)); 15 | //=> 'foo\nbar\n' 16 | ``` 17 | */ 18 | export default function stripFinalNewline(input: T): T; 19 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} on ${{ matrix.os }} 8 | runs-on: ${{ matrix.os }} 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 23 14 | - 18 15 | os: 16 | - ubuntu-latest 17 | - macos-latest 18 | - windows-latest 19 | steps: 20 | - uses: actions/checkout@v4 21 | - uses: actions/setup-node@v4 22 | with: 23 | node-version: ${{ matrix.node-version }} 24 | - run: npm install 25 | - run: npm test 26 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export default function stripFinalNewline(input) { 2 | if (typeof input === 'string') { 3 | return stripFinalNewlineString(input); 4 | } 5 | 6 | if (!(ArrayBuffer.isView(input) && input.BYTES_PER_ELEMENT === 1)) { 7 | throw new Error('Input must be a string or a Uint8Array'); 8 | } 9 | 10 | return stripFinalNewlineBinary(input); 11 | } 12 | 13 | const stripFinalNewlineString = input => 14 | input.at(-1) === LF 15 | ? input.slice(0, input.at(-2) === CR ? -2 : -1) 16 | : input; 17 | 18 | const stripFinalNewlineBinary = input => 19 | input.at(-1) === LF_BINARY 20 | ? input.subarray(0, input.at(-2) === CR_BINARY ? -2 : -1) 21 | : input; 22 | 23 | const LF = '\n'; 24 | const LF_BINARY = LF.codePointAt(0); 25 | const CR = '\r'; 26 | const CR_BINARY = CR.codePointAt(0); 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "strip-final-newline", 3 | "version": "4.0.0", 4 | "description": "Strip the final newline character from a string or Uint8Array", 5 | "license": "MIT", 6 | "repository": "sindresorhus/strip-final-newline", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "sideEffects": false, 19 | "engines": { 20 | "node": ">=18" 21 | }, 22 | "scripts": { 23 | "test": "xo && ava && tsd" 24 | }, 25 | "files": [ 26 | "index.js", 27 | "index.d.ts" 28 | ], 29 | "keywords": [ 30 | "strip", 31 | "trim", 32 | "remove", 33 | "delete", 34 | "final", 35 | "last", 36 | "end", 37 | "file", 38 | "newline", 39 | "linebreak", 40 | "character", 41 | "string", 42 | "uint8array" 43 | ], 44 | "devDependencies": { 45 | "ava": "^6.0.1", 46 | "tsd": "^0.31.0", 47 | "xo": "^0.58.0" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # strip-final-newline 2 | 3 | > Strip the final [newline character](https://en.wikipedia.org/wiki/Newline) from a string or Uint8Array. 4 | 5 | This can be useful when parsing the output of, for example, `ChildProcess#execFile()`, as [binaries usually output a newline at the end](https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline). You cannot use `stdout.trimEnd()` for this as it removes all trailing newlines and whitespaces at the end. 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install strip-final-newline 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import stripFinalNewline from 'strip-final-newline'; 17 | 18 | stripFinalNewline('foo\nbar\n\n'); 19 | //=> 'foo\nbar\n' 20 | 21 | const uint8Array = new TextEncoder().encode('foo\nbar\n\n') 22 | new TextDecoder().decode(stripFinalNewline(uint8Array)); 23 | //=> 'foo\nbar\n' 24 | ``` 25 | 26 | ## Performance 27 | 28 | 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. 29 | 30 | If you'd like to ensure that modifying the return value does not also modify the value passed as input, please use `.slice()`. 31 | 32 | ```js 33 | const value = new TextDecoder().decode(stripFinalNewline(uint8Array).slice()); 34 | ``` 35 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import stripFinalNewline from './index.js'; 3 | 4 | const invalidType = async (t, input) => { 5 | t.throws(() => { 6 | stripFinalNewline(input); 7 | }, { 8 | message: /Input must be/, 9 | }); 10 | }; 11 | 12 | test('Invalid type - boolean', invalidType, true); 13 | test('Invalid type - DataView', invalidType, new DataView(new ArrayBuffer(0))); 14 | test('Invalid type - Uint16Array', invalidType, new Uint16Array(new ArrayBuffer(0))); 15 | 16 | const assertStrip = async (t, convert, input, output) => { 17 | t.deepEqual(stripFinalNewline(convert(input)), convert(output)); 18 | }; 19 | 20 | const inputs = [ 21 | 'foo\n', 22 | 'foo\nbar\n', 23 | 'foo\n\n\n', 24 | 'foo\r\n', 25 | 'foo\r', 26 | 'foo\n\r\n', 27 | ]; 28 | 29 | const outputs = [ 30 | 'foo', 31 | 'foo\nbar', 32 | 'foo\n\n', 33 | 'foo', 34 | 'foo\r', 35 | 'foo\n', 36 | ]; 37 | 38 | const identity = input => input; 39 | 40 | test('string - LF', assertStrip, identity, inputs[0], outputs[0]); 41 | test('string - LF text LF', assertStrip, identity, inputs[1], outputs[1]); 42 | test('string - LF LF LF', assertStrip, identity, inputs[2], outputs[2]); 43 | test('string - CR LF', assertStrip, identity, inputs[3], outputs[3]); 44 | test('string - CR', assertStrip, identity, inputs[4], outputs[4]); 45 | test('string - LF CR LF', assertStrip, identity, inputs[5], outputs[5]); 46 | 47 | const textEncoder = new TextEncoder(); 48 | const toUint8Array = textEncoder.encode.bind(textEncoder); 49 | 50 | test('Uint8Array - LF', assertStrip, toUint8Array, inputs[0], outputs[0]); 51 | test('Uint8Array - LF text LF', assertStrip, toUint8Array, inputs[1], outputs[1]); 52 | test('Uint8Array - LF LF LF', assertStrip, toUint8Array, inputs[2], outputs[2]); 53 | test('Uint8Array - CR LF', assertStrip, toUint8Array, inputs[3], outputs[3]); 54 | test('Uint8Array - CR', assertStrip, toUint8Array, inputs[4], outputs[4]); 55 | test('Uint8Array - LF CR LF', assertStrip, toUint8Array, inputs[5], outputs[5]); 56 | --------------------------------------------------------------------------------