Skip to content

directus/format-title

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 
 
 

Title Formatter

Custom string formatter that converts any string into Title Case for the rules

This package converts any string into title case. This means only using capital letters for the principal words. Articles, conjunctions, and prepositions do not get capital letters unless they start or end the title

input output
snowWhiteAndTheSevenDwarfs Snow White and the Seven Dwarfs
NewcastleUponTyne Newcastle Upon Tyne
brighton_on_sea Brighton on Sea
apple_releases_new_ipad Apple Releases New iPad
7-food-trends 7 Food Trends

The package contains a list of words that use some sort of special casing, for example: McDonalds, iPhone, and YouTube.

Installation

$ npm install @directus/format-title

Usage

The package by default converts camelCase, PascalCase, underscore, and "regular" sentences to Title Case

formatTitle(string, [separator]);

formatTitle('snowWhiteAndTheSevenDwarfs');
// => Snow White and the Seven Dwarfs

You can provide an optional separator regex as a second parameter to support splitting the string on different characters. By default, this regex is set to /\s|-|_/g.

Contributing

If your favorite specially cased word isn't being capitalized properly, please open an issue or submit a pull request!

Requirements

  • NodeJS LTS
  • pnpm 7.5.0 or newer

Commands

The following pnpm scripts are available:

  • pnpm lint – Lint the code using Eslint / Prettier
  • pnpm test – Run the unit tests

Make sure that both commands pass locally before creating a Pull Request.

Pushing a Release

This applies to maintainers only

  1. Create a new version / tag by running pnpm version <version>. Tip: use pnpm version patch|minor|major to auto-bump the version number
  2. Push the version commit / tag to GitHub (git push && git push --tags)

The CI will automatically build and release to npm, and generate the release notes.

About

Custom string formatter that converts any string into Title Case

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  
  •  
  •  

Packages

No packages published