How to use the titleize function from inflection

Find comprehensive JavaScript inflection.titleize code examples handpicked from public code repositorys.

inflection.titleize is a function that capitalizes the first letter of each word in a string.

85
86
87
88
89
90
91
92
93
94
95
96
  if (stripNumbers) {
    // "001.guide" or "001-guide" or "001_guide" or "001 guide" -> "guide"
    name = name.replace(/^\d+[.\-_ ]?/, "");
  }


  return titleize(name.replace("-", " "));
}


// Load all MD files in a specified directory and order by metadata 'order' value
function getChildren(parent_path, dir, filter, recursive = true) {
fork icon42
star icon90
watch icon2

+ 56 other calls in file

219
220
221
222
223
224
225
226
227
228
  throw new Error(`“${datum.short_name}” doesn’t have a category.`);

// Retrieve emoji information.
let unified = datum.unified.toLowerCase();
let native = unifiedToNative(unified);
let name = inflection.titleize(
  datum.name || datum.short_name.replace(/-/g, " ") || ""
);
let unicodeEmojiName = inflection.titleize(
  unicodeEmoji[native]?.name || ""
fork icon19
star icon153
watch icon1

+ 87 other calls in file

How does inflection.titleize work?

inflection.titleize is a function that takes a string and capitalizes the first letter of each word, while leaving the rest of the word in lowercase, following title-case formatting rules. It is often used for formatting titles, headings, and subtitles. Internally, it splits the string into words by detecting spaces and underscores, capitalizes the first letter of each word using String.prototype.toUpperCase(), and converts the rest of the word to lowercase using String.prototype.toLowerCase(). It then joins the words back together with spaces and underscores in their original positions.

100
101
102
103
104
105
106
107
108
109
110
}


const createEnumerator = (enumerator) => {
  const newEnumerator = {
    name: enumerator.name,
    title: inflection.titleize(enumerator.name.toLowerCase()),
    label: inflection.humanize(enumerator.name.toLowerCase()),
    comment: enumerator.comment
  }
  return newEnumerator
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
const inflection = require("inflection");

const title = inflection.titleize("the great gatsby");
console.log(title); // "The Great Gatsby"

In this example, the inflection.titleize() method is used to convert the string "the great gatsby" into title case, resulting in the string "The Great Gatsby".