How to use inflection

Comprehensive inflection code examples:

How to use inflection.transform:

21
22
23
24
25
26
27
28
29
30
31
32
const singularTitle = (name) => {
  return inflection.transform(name, ['underscore', 'titleize', 'singularize'])
}


const pluralTitle = (name) => {
  return inflection.transform(name, ['underscore', 'titleize', 'pluralize'])
}


const singularLabel = (name) => {
  return inflection.transform(name, ['underscore', 'singularize', 'humanize'])

How to use inflection.humanize:

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) {

How to use inflection.dasherize:

108
109
110
111
112
113
114
115
116
117
118
119
  return deferred.promise;
}


function getName(name) {
  name = name.toLowerCase();
  name = inflection.dasherize(name).replace(/-/g, '_');
  name = inflection.camelize(name, true);


  return name;
}

How to use inflection.classify:

0
1
2
3
4
5
6
7
8
9
10
const createError = require('http-errors')
const { classify } = require('inflection')


module.exports = options => {
  return async (req, res, next) => {
    const modelName = classify(req.params.resource)
    try {
      req.Model = require(`../models/${modelName}`)
      next()
    } catch (err) {

How to use inflection.underscore:

95
96
97
98
99
100
101
102
103
104
105
106
  return str.trim().replace(/[-_\s]+(.)?/g, (match, c) => c.toUpperCase());
}
exports.camelize = camelize;


function underscore(str) {
  return inflection.underscore(str);
}
exports.underscore = underscore;


function singularize(str) {

How to use inflection.capitalize:

354
355
356
357
358
359
360
361
362
363
for (const [key, value] of Object.entries(githubCustomEmojis)) {
  if (CustomKeyWords[key] === undefined)
    throw new Error(`“${key}” doesn’t have a keyword.`);
  const emoji = {
    id: key,
    name: inflection.capitalize(key),
    keywords: CustomKeyWords[key].keywords,
    skins: [{ src: value }],
  };
  githubEmojis.emojis.push(emoji);

How to use inflection.camelize:

70
71
72
73
74
75
76
77
78
79
const logger = get(options, 'logger', { trace: noop });
const trace = logger.trace.bind(logger);

const modelName = get(options, 'modelName', ref);
const humanModelName = get(options, 'humanModelName');
const property = get(options, 'property', camelize(ref, true));
const hiddenApiIdProperty = get(options, 'hiddenApiIdProperty', `_${property}Id`);
const hiddenDocumentProperty = get(options, 'hiddenDocumentProperty', `_${property}`);
const loadRelatedMethod = get(options, 'loadRelatedMethod', `loadRelated${ref}`);
const virtualHrefProperty = get(options, 'virtualHrefProperty', `${property}Href`);

How to use inflection.pluralize:

105
106
107
108
109
110
111
112
113
114
115
116
  return inflection.singularize(str);
}
exports.singularize = singularize;


function pluralize(str) {
  return inflection.pluralize(str);
}
exports.pluralize = pluralize;


function format(arr, dialect) {

How to use inflection.singularize:

46
47
48
49
50
51
52
53
54
55

// normalize aggregatable
const $aggregatable = common.mergeObjects({}, aggregatable);

// normalize unwind
const unwindPathName = isArray ? inflection.singularize(pathName) : pathName;
const unwindDefaults = {
  path: `$${unwindPathName}`,
  preserveNullAndEmptyArrays: true,
};

How to use inflection.titleize:

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) {