@gustavnikolaj/string-utils

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

Template string utilities

原始使用文档
文档目录4 节
  1. deindent
  2. collapse
  3. reflow
  4. qw

Template String Utilities

npm version Build Status Coverage Status

deindent

Strips indentation based on the indentation found on the first line. Will leave extra newlines and extra indentation intact. It will however remove leading and trailing line breaks.

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"

Can be used as a regular function as well as a tag for template strings:

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

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

Can also be required directly:

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

collapse

Collapses whitespace in a line wrapped over multiple lines.

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."

Can also be required directly:

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

reflow

Strips indentation as deindent, but will persist paragraphs (separated by double newlines like in markdown) and reflow the paragraphs so that they fit within a defined width.

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. */

It also supports being called as a regular non-template-string function:

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

Can also be required directly:

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

qw

A qw (quote word) helper for old, disgruntled perl programmers.

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

const blah = "hey";

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

Can also be imported directly:

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 是什么?

Template string utilities

如何安装 @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 和提交记录判断。