Skip to content

Commit

Permalink
Add TypeScript types (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored Dec 12, 2023
1 parent 077250c commit 13555b0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
18 changes: 18 additions & 0 deletions index.d.ts
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;
7 changes: 7 additions & 0 deletions index.test-d.ts
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))));
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "strip-final-newline",
"version": "3.0.0",
"description": "Strip the final newline character from a string/buffer",
"description": "Strip the final newline character from a string or Uint8Array",
"license": "MIT",
"repository": "sindresorhus/strip-final-newline",
"funding": "https://github.com/sponsors/sindresorhus",
Expand All @@ -11,15 +11,19 @@
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"engines": {
"node": ">=18"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"strip",
Expand All @@ -34,10 +38,11 @@
"linebreak",
"character",
"string",
"buffer"
"uint8array"
],
"devDependencies": {
"ava": "^6.0.1",
"tsd": "^0.29.0",
"xo": "^0.56.0"
}
}
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# strip-final-newline

> Strip the final [newline character](https://en.wikipedia.org/wiki/Newline) from a string/buffer
> Strip the final [newline character](https://en.wikipedia.org/wiki/Newline) from a string or Uint8Array.
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.

Expand All @@ -18,6 +18,7 @@ import stripFinalNewline from 'strip-final-newline';
stripFinalNewline('foo\nbar\n\n');
//=> 'foo\nbar\n'

stripFinalNewline(Buffer.from('foo\nbar\n\n')).toString();
const uint8Array = new TextEncoder().encode('foo\nbar\n\n')
new TextDecoder().decode(stripFinalNewline(uint8Array));
//=> 'foo\nbar\n'
```

0 comments on commit 13555b0

Please sign in to comment.