# 转为 camelCase

> 逐行转为 camelCase，在空格、连字符、下划线和大小写变化处分词

版本：1.0.0 · 作者 Deck

- 安装到 Deck：deck://install-plugin?id=to-camel-case&url=https%3A%2F%2Fgithub.com%2Fyuzeguitarist%2Fdeck-plugins%2Freleases%2Fdownload%2Fplugins%2Fto-camel-case-1.0.0.zip
- 下载 .zip：https://github.com/yuzeguitarist/deck-plugins/releases/download/plugins/to-camel-case-1.0.0.zip
- 在 GitHub 上查看：https://github.com/yuzeguitarist/deck-plugins/tree/main/plugins/to-camel-case

也可以用终端安装：

```sh
mkdir -p ~/.deck/scripts/to-camel-case && curl -fsSL https://github.com/yuzeguitarist/deck-plugins/releases/download/plugins/to-camel-case-1.0.0.zip | tar -xf - -C ~/.deck/scripts/to-camel-case
```

## 脚本

```js
// Converts each line to camelCase, splitting words at spaces, hyphens, underscores, other punctuation and case changes.
function words(line) {
  const result = [];
  const runs = line.replace(/['\u2019]/g, "").match(/[\p{L}\p{M}\p{N}]+/gu) ?? [];
  for (const run of runs) {
    const spaced = run.replace(/([\p{Ll}\p{N}])(\p{Lu})/gu, "$1 $2").replace(/(\p{Lu})(\p{Lu}\p{Ll})/gu, "$1 $2");
    for (const word of spaced.split(" ")) result.push(word.toLowerCase());
  }
  return result;
}

function capitalize(word) {
  const first = String.fromCodePoint(word.codePointAt(0));
  return first.toUpperCase() + word.slice(first.length);
}

// A line without letters or digits stays as it is.
function convert(line) {
  const list = words(line);
  return list.length === 0 ? line : list.map((word, k) => (k === 0 ? word : capitalize(word))).join("");
}

function transform(input) {
  return String(input ?? "").split(/(\r\n|\r|\n)/).map((part, k) => (k % 2 === 0 ? convert(part) : part)).join("");
}
```

---

- HTML：https://deckclip.app/zh-cn/plugins/to-camel-case
- English：https://deckclip.app/plugins/to-camel-case.md

© 2026 潘禹泽
