@gouch/to-title-case

中文文档
3.0.2发布于 2026/11/01MITESM + CJS零依赖

Intelligently format titles and headings into consistent title case.

原始使用文档
文档目录11 节
  1. Installation
  2. Browser
  3. Node
  4. Usage
  5. Options
  6. What Is Title Case?
  7. The Capitalization Rules
  8. Inspiration and Resources
  9. FAQ
  10. Dealing With ALL CAPS Input
  11. Defining a String Method

To Title Case for JavaScript

Intelligently format titles and headings into consistent title case.

Installation

Browser

Each year brings new ways to add JS to HTML. One modern option is:

<script type="module">
  import { toTitleCase } from './to-title-case.js'

  toTitleCase('make me a title') //→ Make Me a Title
</script>

Or for a classic, global vibe, you can try:

<script src="to-title-case.cjs"></script>
<script>
  toTitleCase('make me a title') //→ Make Me a Title
</script>

Node

Install with npm install @gouch/to-title-case then import:

import { toTitleCase } from '@gouch/to-title-case'

toTitleCase('make me a title') //→ Make Me a Title

Usage

The script defines the function toTitleCase(), which applies title case rules to an input string. For best results, input is assumed to be sentence case or naive title case.

toTitleCase('make me a title') //→ Make Me a Title

Options

The lowercase word list can be replaced with your own:

toTitleCase('make me a title', { lowercase: ['me'] }) //→ Make me A Title

What Is Title Case?

Title case, or headline case, is a convention for formatting the titles of articles and the headings within. It does basically amount to capitalizing each word, but with a few lowercase exceptions that look odd if you miss them.

Most well-known style guides follow dynamic rules that switch depending on how a word is being used in context. Evaluating grammar is outside the scope of a small script, so this script follows static, consistent rules. Those rules are a close match to the New York Times style guide.

The Capitalization Rules

  1. Capitalize the first and last word. Do the same for subtitles.
  2. Capitalize both ends of hyphenated phrases.
  3. Preserve the formatting of proper nouns and technical terms where possible.
  4. Lowercase these words:
    • Articles: a, an, the
    • Conjunctions: and, but, if, nor, or
    • Short prepositions: as, at, by, en, for, in, of, on, per, to, via
    • Versus: vs., vs, v., v
    • Borrowed: à
  5. Capitalize everything else.

Inspiration and Resources

I built this in response to John Gruber’s post in 2008.

Matt Belz maintains an excellent site documenting the differing rules of well-known style guides.

FAQ

Dealing With ALL CAPS Input

If you’re stuck with uppercase input, it is technically possible to brute force it by applying a lowercase transform:

toTitleCase('CNN REPORTS ON NASA CENTER IN HOUSTON, TX'.toLowerCase())
//→ Cnn Reports on Nasa Center in Houston, Tx

But consider carefully before doing this. As frustrating as data stuck in uppercase might be, turning acronyms into distracting typos might be worse.

Defining a String Method

Version 2 assigned itself as a method on the built-in String object. If you want to restore that behavior, you can use this code:

String.prototype.toTitleCase = function (options) {
  return toTitleCase(this, options)
}

'make me a title'.toTitleCase() //→ Make Me a Title

Note: arrow functions won’t work here since they change how this is handled.

版本与发布记录

@gouch/to-title-case 共发布 6 个版本,首次发布于 2018/09/08;过去 12 个月发布了 3 个版本。

版本发布日期距今
3.0.2latest2026/11/012 天前
3.0.12026/10/304 天前
3.0.02026/10/1816 天前
2.2.12018/09/088 年前
2.3.12018/09/088 年前
2.2.02018/09/088 年前

关于 @gouch/to-title-case 的常见问题

@gouch/to-title-case 是什么?

智能地将标题和标题格式化为一致的标题大小写。

如何安装 @gouch/to-title-case?

在项目目录运行 npm install @gouch/to-title-case,或使用 pnpm add / yarn add / bun add。

@gouch/to-title-case 支持 TypeScript 吗?

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

@gouch/to-title-case 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@gouch/to-title-case 可以商用吗?

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

@gouch/to-title-case 还在维护吗?

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