@adobe/helix-shared-string

中文文档
2.2.0发布于 2025/12/09Apache-2.0ES Module零依赖近 30 天下载 2935

Shared modules of the Helix Project - string

原始使用文档
文档目录8 节
  1. Installation
  2. Usage
  3. Functions
  4. multiline(str)
  5. splitByExtension(name)
  6. sanitizeName(name)
  7. sanitizePath(filepath, opts)
  8. editDistance(s0, s1)

Helix Shared - String

A collection of utility functions for string manipulation, including multiline string handling, filename sanitization, path processing, and edit distance calculation.

Installation

npm install @adobe/helix-shared-string

Usage

import {
  multiline,
  splitByExtension,
  sanitizeName,
  sanitizePath,
  editDistance
} from '@adobe/helix-shared-string';

Functions

multiline(str)

Declares multiline strings with proper indentation handling. This function strips empty first and last lines, and removes common whitespace prefixes from all lines.

const text = multiline(`
  Hello
  World
    Indented
  Back
`);
// Result: "Hello\nWorld\n  Indented\nBack"

The function automatically detects and removes the common indentation level, making it perfect for template strings in code.

splitByExtension(name)

Splits a filename at the last dot, returning the base name and extension as a two-element array.

splitByExtension('document.txt');
// Returns: ['document', 'txt']

splitByExtension('archive.tar.gz');
// Returns: ['archive.tar', 'gz']

splitByExtension('README');
// Returns: ['README', '']

splitByExtension('.gitignore');
// Returns: ['.gitignore', '']

sanitizeName(name)

Sanitizes a string by:

  • Converting to lowercase
  • Normalizing all Unicode characters
  • Replacing all non-alphanumeric characters with dashes
  • Removing consecutive dashes
  • Removing leading and trailing dashes
sanitizeName('My Document');
// Returns: 'my-document'

sanitizeName('Föhren Smürd');
// Returns: 'fohren-smurd'

sanitizeName('.My 2. Document-');
// Returns: 'my-2-document'

sanitizePath(filepath, opts)

Sanitizes a file path using the same rules as sanitizeName, but only affects the basename. The directory path and extension are preserved.

sanitizePath('/Untitled Folder/My Document.docx');
// Returns: '/Untitled Folder/my-document.docx'

sanitizePath('Föhren Smürd.txt');
// Returns: 'fohren-smurd.txt'

sanitizePath('.My 2. Document!.docx');
// Returns: 'my-2-document.docx'

Options:

  • ignoreExtension (boolean): If true, treats the entire filename as the basename without preserving any extension
sanitizePath('.MyDocument', { ignoreExtension: true });
// Returns: 'mydocument'

editDistance(s0, s1)

Computes the Levenshtein edit distance between two strings using a recursive algorithm. Returns the minimum number of single-character edits (insertions, deletions, or substitutions) required to transform one string into another.

editDistance('foo', 'foo');
// Returns: 0

editDistance('foo', 'foo123');
// Returns: 3

editDistance('My Document', 'my-document');
// Returns: 3

editDistance('My 1. Document', 'my-1-document');
// Returns: 5

This function is optimized for relatively short strings like filenames and performs well even with moderately long inputs.

版本与发布记录

@adobe/helix-shared-string 共发布 16 个版本,首次发布于 2021/04/29;过去 12 个月发布了 1 个版本。

版本发布日期距今
2.2.0latest2025/12/0910 个月前
2.1.02024/12/071.9 年前
2.0.22024/04/172 年前
2.0.12024/01/242 年前
2.0.02023/01/313 年前
1.0.92022/05/034 年前

关于 @adobe/helix-shared-string 的常见问题

@adobe/helix-shared-string 是什么?

Shared modules of the Helix Project - string

如何安装 @adobe/helix-shared-string?

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

@adobe/helix-shared-string 支持 TypeScript 吗?

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

@adobe/helix-shared-string 支持 ESM 还是 CommonJS?

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

@adobe/helix-shared-string 可以商用吗?

@adobe/helix-shared-string 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@adobe/helix-shared-string 的下载量有多少?

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

@adobe/helix-shared-string 还在维护吗?

最新版本 2.2.0 发布于 2025/12/09(10 个月前);至今共发布 16 个版本,其中过去 12 个月发布了 1 个;首次发布于 2021/04/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。