How to use the humanize function from inflection

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

In the Inflection.js library, the humanize function is used to convert a string of words into a more human-readable format, with capitalization and word separation.

96
97
98
99
100
101
102
103
104
105
schema.methods[loadRelatedMethod] = loadRelatedHref;

schema.pre('validate', loadRelatedHref);

function getHumanModelName() {
  return humanModelName || humanize(mongoose.model(ref).modelName, true);
}

function getRelatedHref() {
  if (!this[property] || !this[property].href) {
fork icon3
star icon0
watch icon9

238
239
240
241
242
243
244
245
246
247
  .split('\n')
  .map(function (line) {
    line = line.trim();
    let parts = line.split(':');
    return {
      key: inflection.humanize(parts[0]),
      value: parts.slice(1).join(':')
    };
  });
return callback(null, connectionInfo);
fork icon0
star icon0
watch icon1

How does inflection.humanize work?

inflection.humanize is a function in the Inflection.js library that converts a string of words into a more human-readable format.

When inflection.humanize is called with a string as input, it performs the following transformations:

  • It converts the string to lowercase.
  • It replaces underscores and dashes with spaces.
  • It capitalizes the first letter of each word.
  • It leaves all other letters in each word unchanged.

By using inflection.humanize, developers can transform machine-generated text into a more natural, human-readable format that is easier to read and understand. This can be particularly useful in applications where the text is displayed to end users, such as in user interfaces or reports.

101
102
103
104
105
106
107
108
109
110
111


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
5
6
7
const inflection = require("inflection");

// Converting a string to a more human-readable format
const inputString = "customer_id";
const humanizedString = inflection.humanize(inputString);

console.log(humanizedString); // Outputs: Customer id

In this example, we're using inflection.humanize to convert a string (customer_id) to a more human-readable format (Customer id). The humanizedString variable contains the transformed string after applying the transformations described above. Note that inflection.humanize is designed to work with English-language text, and may not produce expected results when used with text in other languages.