From 13555b0395c3ffd59d16e52e02d62d4aaf818c05 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Tue, 12 Dec 2023 22:37:29 +0000 Subject: [PATCH] Add TypeScript types (#6) --- index.d.ts | 18 ++++++++++++++++++ index.test-d.ts | 7 +++++++ package.json | 15 ++++++++++----- readme.md | 5 +++-- 4 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 index.d.ts create mode 100644 index.test-d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..e8fa1d3 --- /dev/null +++ b/index.d.ts @@ -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(input: T): T; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..0308224 --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,7 @@ +import {expectType, expectError} from 'tsd'; +import stripFinalNewline from './index.js'; + +expectType<''>(stripFinalNewline('')); +expectType(stripFinalNewline(new TextEncoder().encode(''))); +expectError(stripFinalNewline(true)); +expectError(stripFinalNewline(new Uint16Array(new ArrayBuffer(0)))); diff --git a/package.json b/package.json index b3e920b..b5e6dd8 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -34,10 +38,11 @@ "linebreak", "character", "string", - "buffer" + "uint8array" ], "devDependencies": { "ava": "^6.0.1", + "tsd": "^0.29.0", "xo": "^0.56.0" } } diff --git a/readme.md b/readme.md index e5c0889..a6f6ad1 100644 --- a/readme.md +++ b/readme.md @@ -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. @@ -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' ```