or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-strip-final-newline

Strip the final newline character from a string or Uint8Array

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/strip-final-newline@4.0.x

To install, run

npx @tessl/cli install tessl/npm-strip-final-newline@4.0.0

index.mddocs/

Strip Final Newline

Strip 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.

Package Information

  • Package Name: strip-final-newline
  • Package Type: npm
  • Language: JavaScript (ES Module)
  • Installation: npm install strip-final-newline

Core Imports

import stripFinalNewline from "strip-final-newline";

Note: This package is ES module only ("type": "module" in package.json) and does not support CommonJS require().

Basic Usage

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'

Capabilities

Strip Final Newline Function

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 process

Returns:

  • Same type as input (string or Uint8Array) with final newline removed

Behavior:

  • For strings: Removes final \n character or \r\n sequence
  • For Uint8Array: Returns subarray reference (no copying) with final newline bytes removed
  • Preserves all internal newlines and whitespace - only removes the final newline
  • If input has no final newline, returns the input unchanged
  • Throws Error with message "Input must be a string or a Uint8Array" for invalid input types

Performance Notes:

  • String processing creates a new string with final characters removed
  • Uint8Array processing returns a subarray reference for maximum efficiency
  • No memory copying for Uint8Array inputs - operates in constant time regardless of input size
  • For guaranteed immutability with Uint8Array results, use .slice() on the returned value

Usage 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();

Error Handling

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)

Types

// 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;