or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

index.md

index.mddocs/

0

# Strip Final Newline

1

2

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.

3

4

## Package Information

5

6

- **Package Name**: strip-final-newline

7

- **Package Type**: npm

8

- **Language**: JavaScript (ES Module)

9

- **Installation**: `npm install strip-final-newline`

10

11

## Core Imports

12

13

```javascript

14

import stripFinalNewline from "strip-final-newline";

15

```

16

17

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

18

19

## Basic Usage

20

21

```javascript

22

import stripFinalNewline from "strip-final-newline";

23

24

// String input

25

stripFinalNewline('foo\nbar\n\n');

26

//=> 'foo\nbar\n'

27

28

stripFinalNewline('hello world\n');

29

//=> 'hello world'

30

31

stripFinalNewline('no newline');

32

//=> 'no newline'

33

34

// Uint8Array input

35

const uint8Array = new TextEncoder().encode('foo\nbar\n\n');

36

const result = stripFinalNewline(uint8Array);

37

new TextDecoder().decode(result);

38

//=> 'foo\nbar\n'

39

40

// Handles both LF and CRLF

41

stripFinalNewline('windows line ending\r\n');

42

//=> 'windows line ending'

43

```

44

45

## Capabilities

46

47

### Strip Final Newline Function

48

49

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.

50

51

```typescript { .api }

52

/**

53

* Strip the final newline character from a string or Uint8Array

54

* @param input - The input to strip final newline from

55

* @returns The input without any final newline, preserving the original type

56

* @throws Error if input is not a string or Uint8Array

57

*/

58

function stripFinalNewline<T extends string | Uint8Array>(input: T): T;

59

```

60

61

**Parameters:**

62

- `input`: string | Uint8Array - The input data to process

63

64

**Returns:**

65

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

66

67

**Behavior:**

68

- For strings: Removes final `\n` character or `\r\n` sequence

69

- For Uint8Array: Returns subarray reference (no copying) with final newline bytes removed

70

- Preserves all internal newlines and whitespace - only removes the final newline

71

- If input has no final newline, returns the input unchanged

72

- Throws `Error` with message "Input must be a string or a Uint8Array" for invalid input types

73

74

**Performance Notes:**

75

- String processing creates a new string with final characters removed

76

- Uint8Array processing returns a subarray reference for maximum efficiency

77

- No memory copying for Uint8Array inputs - operates in constant time regardless of input size

78

- For guaranteed immutability with Uint8Array results, use `.slice()` on the returned value

79

80

**Usage Examples:**

81

82

```javascript

83

import stripFinalNewline from "strip-final-newline";

84

85

// Basic string processing

86

const output = stripFinalNewline('command output\n');

87

console.log(`"${output}"`); // "command output"

88

89

// Processing child process output

90

import { execSync } from 'child_process';

91

const rawOutput = execSync('echo "hello world"', { encoding: 'utf8' });

92

const cleanOutput = stripFinalNewline(rawOutput);

93

94

// Binary data processing (efficient - no copying)

95

const binaryData = new TextEncoder().encode('data\n');

96

const stripped = stripFinalNewline(binaryData);

97

// stripped is a subarray reference to binaryData

98

99

// Ensuring immutability for binary data

100

const immutableResult = stripFinalNewline(binaryData).slice();

101

```

102

103

## Error Handling

104

105

The function validates input types and throws descriptive errors:

106

107

```javascript

108

// These will throw Error: "Input must be a string or a Uint8Array"

109

stripFinalNewline(123); // number

110

stripFinalNewline(true); // boolean

111

stripFinalNewline(['a', 'b']); // array

112

stripFinalNewline(new DataView(new ArrayBuffer(0))); // DataView

113

stripFinalNewline(new Uint16Array(0)); // Uint16Array (wrong element size)

114

```

115

116

## Types

117

118

```typescript { .api }

119

// Generic function signature with type preservation

120

function stripFinalNewline<T extends string | Uint8Array>(input: T): T;

121

122

// Union type for all valid inputs

123

type ValidInput = string | Uint8Array;

124

```