strip-final-newline

中文文档
4.0.0发布于 2023/12/14MITTypeScriptES Module零依赖近 30 天下载 5.6亿

Strip the final newline character from a string or Uint8Array

原始使用文档
文档目录3 节
  1. Install
  2. Usage
  3. Performance

strip-final-newline

Strip the final newline character 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. You cannot use stdout.trimEnd() for this as it removes all trailing newlines and whitespaces at the end.

Install

npm install strip-final-newline

Usage

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'

Performance

When using an Uint8Array, the original value is referenced, not copied. This is much more efficient, requires almost no memory, and remains milliseconds fast even on very large inputs.

If you'd like to ensure that modifying the return value does not also modify the value passed as input, please use .slice().

const value = new TextDecoder().decode(stripFinalNewline(uint8Array).slice());

版本与发布记录

strip-final-newline 共发布 3 个版本,首次发布于 2018/10/28;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.0.0latest2023/12/142 年前
3.0.02021/05/035 年前
2.0.02018/10/288 年前

关于 strip-final-newline 的常见问题

strip-final-newline 是什么?

Strip the final newline character from a string or Uint8Array

如何安装 strip-final-newline?

在项目目录运行 npm install strip-final-newline,或使用 pnpm add / yarn add / bun add。

strip-final-newline 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

strip-final-newline 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

strip-final-newline 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

strip-final-newline 可以商用吗?

strip-final-newline 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

strip-final-newline 的下载量有多少?

根据 npm 官方下载统计,strip-final-newline 在 2026/10/05 – 2026/11/03 这 30 天内被下载 557,466,457 次,日均约 18,582,215 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

strip-final-newline 还在维护吗?

最新版本 4.0.0 发布于 2023/12/14(2 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/10/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。