Strip the final newline character from a string or Uint8Array
npx @tessl/cli install tessl/npm-strip-final-newline@4.0.0Strip Final Newline is a lightweight utility that removes the final newline character from strings and Uint8Array objects. It handles both string inputs and binary data, correctly identifying and removing trailing newline characters (\n) and carriage return + newline combinations (\r\n). The library is particularly useful for processing output from child processes and command-line tools that typically append trailing newlines.
npm install strip-final-newlineimport stripFinalNewline from "strip-final-newline";Note: This package is ES module only ("type": "module" in package.json) and does not support CommonJS require().
import stripFinalNewline from "strip-final-newline";
// String input
stripFinalNewline('foo\nbar\n\n');
//=> 'foo\nbar\n'
stripFinalNewline('hello world\n');
//=> 'hello world'
stripFinalNewline('no newline');
//=> 'no newline'
// Uint8Array input
const uint8Array = new TextEncoder().encode('foo\nbar\n\n');
const result = stripFinalNewline(uint8Array);
new TextDecoder().decode(result);
//=> 'foo\nbar\n'
// Handles both LF and CRLF
stripFinalNewline('windows line ending\r\n');
//=> 'windows line ending'Removes the final newline character from a string or Uint8Array input. For strings, it removes the final \n or \r\n sequence. For Uint8Array inputs, it uses efficient subarray operations that reference the original data rather than copying it.
/**
* Strip the final newline character from a string or Uint8Array
* @param input - The input to strip final newline from
* @returns The input without any final newline, preserving the original type
* @throws Error if input is not a string or Uint8Array
*/
function stripFinalNewline<T extends string | Uint8Array>(input: T): T;Parameters:
input: string | Uint8Array - The input data to processReturns:
Behavior:
\n character or \r\n sequenceError with message "Input must be a string or a Uint8Array" for invalid input typesPerformance Notes:
.slice() on the returned valueUsage Examples:
import stripFinalNewline from "strip-final-newline";
// Basic string processing
const output = stripFinalNewline('command output\n');
console.log(`"${output}"`); // "command output"
// Processing child process output
import { execSync } from 'child_process';
const rawOutput = execSync('echo "hello world"', { encoding: 'utf8' });
const cleanOutput = stripFinalNewline(rawOutput);
// Binary data processing (efficient - no copying)
const binaryData = new TextEncoder().encode('data\n');
const stripped = stripFinalNewline(binaryData);
// stripped is a subarray reference to binaryData
// Ensuring immutability for binary data
const immutableResult = stripFinalNewline(binaryData).slice();The function validates input types and throws descriptive errors:
// These will throw Error: "Input must be a string or a Uint8Array"
stripFinalNewline(123); // number
stripFinalNewline(true); // boolean
stripFinalNewline(['a', 'b']); // array
stripFinalNewline(new DataView(new ArrayBuffer(0))); // DataView
stripFinalNewline(new Uint16Array(0)); // Uint16Array (wrong element size)// Generic function signature with type preservation
function stripFinalNewline<T extends string | Uint8Array>(input: T): T;
// Union type for all valid inputs
type ValidInput = string | Uint8Array;