@gustavnikolaj/string-utils

中文文档
2.0.0发布于 2019/05/03ISCCommonJS零依赖近 30 天下载 1.9万

模板字符串工具

中文使用文档
文档目录4 节
  1. 缩进
  2. 崩溃
  3. 重新流动
  4. 单位

模板字符串工具

npm version Build Status Coverage Status

缩进

根据一行所发现的缩入来条缩入. 会离开的 额外的新线和额外的缩进完整。 然而,它将消除领先和 尾随行断.

import { deindent } from "@gustavnikolaj/string-utils";

console.log(deindent`
  foo bar
  qux baz
`); // => "foo bar\nqux baz"

console.log(deindent`
  foo bar
    qux baz
`); // => "foo bar\n  qux baz"

console.log(deindent`
  foo bar

    qux baz
`); // => "foo bar\n\n  qux baz"

可用作常规函数以及模板字符串标签:

console.log(deindent`
  foo bar
  qux baz
`); // => "foo bar\nqux baz"

console.log(deindent(`
  foo bar
  qux baz
`)); // => "foo bar\nqux baz"

也可直接要求:

import deindent from "@gustavnikolaj/string-utils/deindent";

崩溃

在多行相包裹的一行中折叠出白空间.

import { collapse } from "@gustavnikolaj/string-utils";

console.log(collapse`
  foo bar
  qux baz
`); // => "foo bar qux baz"

console.log(collapse`
  foo bar
    qux baz
`); // => "foo bar qux baz"

console.log(collapse`
  This is a very long string that I rather not have to put in a single line
  because then it would make my code spill over and take more than the 80
  characters per line that I prefer.
`);
// => "This is a very long string that I rather not have to put in a single " +
//    "line because then it would make my code spill over and take more " +
//    "than the 80 characters per line that I prefer."

也可直接要求:

import collapse from "@gustavnikolaj/string-utils/collapse";

重新流动

缩进为去缩出,但会坚持段落(以双相分隔) 将各段重新排出,使其符合 定义宽度.

import { reflow } from "@gustavnikolaj/string-utils";

console.log(reflow(8)`
  foo bar qux baz foo bar
`); // => "foo bar\nqux baz\nfoo bar"

console.log(reflow(80)`
  foo bar

  qux baz
`); // => "foo bar\n\nqux baz"

console.log(reflow(30)`
  This is a very long string that is for sure longer than 30 characters.

  This is a very long string that is for sure longer than 30 characters.
`);
/* => This is a very long string
      that is for sure longer than
      30 characters.

      This is a very long string
      that is for sure longer than
      30 characters. */

它还支持被调用为常规的非时空字符串函数:

reflow(
  "This is a very long string that is for sure longer than 30 characters.",
  30
);

也可直接要求:

import reflow from "@gustavnikolaj/string-utils/reflow";

单位

A 类 qw (引用单词) 助手 对于老, 不满的 perl 程序员 .

import { qw } from "@gustavnikolaj/string-utils";

const blah = "hey";

console.log(qw`   foo   bar
  quux   ${blah}   baz
`); // => [ 'foo', 'bar', 'quux', 'hey', 'baz' ]

也可以直接导入:

import qw from "@gustavnikolaj/string-utils/qw";

版本与发布记录

@gustavnikolaj/string-utils 共发布 9 个版本,首次发布于 2018/11/19;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.0latest2019/05/037 年前
1.3.12019/05/027 年前
1.3.02019/04/297 年前
1.2.22019/01/247 年前
1.2.12018/11/217 年前
1.2.02018/11/217 年前

关于 @gustavnikolaj/string-utils 的常见问题

@gustavnikolaj/string-utils 是什么?

模板字符串工具

如何安装 @gustavnikolaj/string-utils?

在项目目录运行 npm install @gustavnikolaj/string-utils,或使用 pnpm add / yarn add / bun add。

@gustavnikolaj/string-utils 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/gustavnikolaj__string-utils 是否存在。

@gustavnikolaj/string-utils 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@gustavnikolaj/string-utils 可以商用吗?

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

@gustavnikolaj/string-utils 的下载量有多少?

根据 npm 官方下载统计,@gustavnikolaj/string-utils 在 2026/10/17 – 2026/11/15 这 30 天内被下载 18,603 次,日均约 620 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@gustavnikolaj/string-utils 还在维护吗?

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