-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
38 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
Strip the final [newline character](https://en.wikipedia.org/wiki/Newline) from a string or Uint8Array. | ||
@returns The input without any final newline. | ||
@example | ||
``` | ||
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' | ||
``` | ||
*/ | ||
export default function stripFinalNewline<T extends string | Uint8Array>(input: T): T; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {expectType, expectError} from 'tsd'; | ||
import stripFinalNewline from './index.js'; | ||
|
||
expectType<''>(stripFinalNewline('')); | ||
expectType<Uint8Array>(stripFinalNewline(new TextEncoder().encode(''))); | ||
expectError(stripFinalNewline(true)); | ||
expectError(stripFinalNewline(new Uint16Array(new ArrayBuffer(0)))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters