How to use the camelize function from underscore.string

Find comprehensive JavaScript underscore.string.camelize code examples handpicked from public code repositorys.

Underscore.string.camelize is a JavaScript function that converts a string to camel case format by removing spaces and replacing them with capital letters on the following words.

47
48
49
50
51
52
53
54
55
56
57
/**
 * 将下划线或者中划线字符转换成 camelized
 * @param str 
 */
function camelize(str) {
    return _s.camelize(str);
}


/**
 * 将camelized 或者中划线转化成下划线
fork icon183
star icon970
watch icon0

+ 4 other calls in file

27
28
29
30
31
32
33
34
35
36
reverse: defaultFunction("reverse"),
decapitalize: defaultFunction("decapitalize"),
capitalize: defaultFunction("capitalize"),
sentence: defaultFunction("capitalize", true),
camelize: (str) =>
  _string.camelize(str.match(/[a-z]/) ? str : str.toLowerCase()),
slugify: slugify,
swapCase: defaultFunction("swapCase"),
snake: (str) =>
  _string
fork icon5
star icon21
watch icon0

+ 6 other calls in file

How does underscore.string.camelize work?

Underscore.string.camelize works by converting a string to camel case format, which is a convention for writing variable names in which the first word is lowercase and subsequent words are capitalized, with no spaces between words. The function takes a string as input and replaces any spaces, dashes, or underscores with empty strings, while capitalizing the first letter of each subsequent word. This creates a new string that is formatted in camel case. Underscore.string.camelize can be useful in situations where it is necessary to convert a string to a specific naming convention, such as when parsing data from an external source or when generating code from a template. By standardizing the naming convention, this can help to simplify code and make it more consistent and easier to read.

984
985
986
987
988
989
990
991
992
993
994
995
s.rjust     = s.lpad;
s.ljust     = s.rpad;
s.contains  = s.include;
s.q         = s.quote;
s.toBool    = s.toBoolean;
s.camelcase = s.camelize;
s.mapChars  = s.map;




// Implement chaining
fork icon0
star icon6
watch icon0

45
46
47
48
49
50
51
52
53
54
55
56
exports.titleize = function(value, options, callback){
    callback(null, str.titleize(String(value)));
};


exports.camelize = function(value, options, callback){
    callback(null, str.camelize(String(value)));
};


exports.classify = function(value, options, callback){
    callback(null, str.classify(String(value)));
fork icon2
star icon2
watch icon0

+ 2 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
const _ = require("underscore.string");

// Define a string to convert to camel case
const originalString = "this_is-a_string";

// Convert the string to camel case format
const camelCaseString = _.camelize(originalString);

// Output the result to the console
console.log(`Original string: ${originalString}`);
console.log(`Camel case string: ${camelCaseString}`);

In this example, we use underscore.string.camelize() to convert a string (originalString) to camel case format. The function replaces the underscore and dash characters with empty strings and capitalizes the first letter of each subsequent word, creating a new string that is formatted in camel case. We output both the original string and the camel case string to the console. In this case, the original string is this_is-a_string, and the camel case string is thisIsAString.

35
36
37
38
39
40
41
42
43
44
    this.props = answers;
  });;
}

writing() {
  let field_name_php_camel_case = s.camelize(this.props.project_name);
  let field_name_class_php = s.classify(field_name_php_camel_case) + '_Field';

  this.registerTransformStream([
    rename((path) => {
fork icon1
star icon4
watch icon0

2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
key: "render",
value: function render() {
  var subject = this.props.subject;
  var answerProps = {
    answer: subject,
    className: s.camelize(subject.question().formControl)
  };

  switch (subject.question().formControl) {
    case 'checkbox':
fork icon1
star icon2
watch icon3

+ 9 other calls in file

68
69
70
71
72
73
74
75
76
77
const babel = or('babel');
const updateNotifier = or('updateNotifier');

const tpl = {
	repoName: props.repoName,
	camelModuleName: _s.camelize(props.repoName),
	moduleDescription: props.moduleDescription,
	githubUsername: props.githubUsername,
	name: this.user.git.name(),
	email: this.user.git.email(),
fork icon0
star icon2
watch icon1

+ 8 other calls in file

125
126
127
128
129
130
131
132
133
134
  </div>`;
},

dasherize: (str) => dasherize(str),

camelize: (str) => camelize(str, true),

link(str, locals) {
  if (!locals.href) {
    throw new Error('externalLink is missing href attribute');
fork icon0
star icon2
watch icon2

+ 3 other calls in file

79
80
81
82
83
84
85
86
87
88
const repoName = utils.repoName(props.moduleName);

const tpl = {
	moduleName: props.moduleName,
	moduleDescription: props.moduleDescription,
	camelModuleName: _s.camelize(repoName),
	githubUsername: this.options.org || props.githubUsername,
	repoName,
	name: this.user.git.name(),
	email: this.user.git.email(),
fork icon0
star icon1
watch icon0

+ 4 other calls in file

69
70
71
72
73
74
75
76
77
78
])
  .pipe($.sourcemaps.init())
  .pipe($.if(isProd, htmlFilter))
  .pipe($.if(isProd, $.ngHtml2js({
    // lower camel case all app names
    moduleName: _.camelize(_.slugify(_.humanize(require('../package.json').name))),
    declareModule: false
  })))
  .pipe($.if(isProd, htmlFilter.restore))
  .pipe(jsFilter)
fork icon0
star icon1
watch icon0

189
190
191
192
193
194
195
196
197
198
199
  }
  // If we have filesystem separators, use them to build the full path
  let pathArray = path.split('/');
  // Build the full components name
  return pathArray.map((path) => {
      return _.camelize(_.slugify(_.humanize(path)));
    }).join('/') + _.capitalize(suffix);
};


/**
fork icon0
star icon1
watch icon0

54
55
56
57
58
59
60
61
62
63
function transformName(name) {
    var nameMatch = name.match(/^([-_]*)(.*?)([-_]*)$/);
    if (nameMatch == null) {
        return name + "Rule";
    }
    return nameMatch[1] + underscore_string_1.camelize(nameMatch[2]) + nameMatch[3] + "Rule";
}
function loadRule(directory, ruleName) {
    var fullPath = path.join(directory, ruleName);
    if (fs.existsSync(fullPath + ".js")) {
fork icon1
star icon0
watch icon1

12
13
14
15
16
17
18
19
20
21
function findFormatter(name, formattersDirectory) {
    if (isFunction(name)) {
        return name;
    }
    else if (isString(name)) {
        var camelizedName = underscore_string_1.camelize(name + "Formatter");
        var Formatter = loadFormatter(CORE_FORMATTERS_DIRECTORY, camelizedName);
        if (Formatter != null) {
            return Formatter;
        }
fork icon1
star icon0
watch icon1

10
11
12
13
14
15
16
17
18
19
20
const fs = require('fs');
const path = require('path');


// Function fo generate object path
function generateObjectPath(filePath) {
    return _string.camelize(
        _string.trim(
            filePath.replace(
                path.extname(filePath), '')
                .replace(/([^a-zA-Z0-9//])/g, "-")
fork icon0
star icon0
watch icon1

11
12
13
14
15
16
17
18
19
    this.prompt({
    type    : 'input',
    name    : 'nomeModulo',
    message : 'Qual o nome do módulo?'
}, function (resposta) {
    var nomeModulo = _.camelize(_.humanize(resposta.nomeModulo));

    this._copiarTemplateModulo(nomeModulo);
    this._registrarModulo(nomeModulo);
fork icon0
star icon0
watch icon0

33
34
35
36
37
38
39
40
41
42
	message : 'Qual o nome da aplicação?',
	default : this.appname
}, function (resposta) {
	this.nomeAplicacao = resposta.appName;

	var nomeAplicacaoTratado = _.camelize(_.humanize(resposta.appName));
	this.nomeAplicacaoTratado = nomeAplicacaoTratado;

	done();
}.bind(this));
fork icon0
star icon0
watch icon0