[英]Lodash title case (uppercase first letter of every word)
I'm looking through the lodash docs and other Stack Overflow questions - while there are several native JavaScript ways of accomplishing this task , is there a way I can convert a string to title case using purely lodash functions (or at least existing prototypal functions) so that I don't have to use a regular expression or define a new function?我正在查看 lodash 文档和其他 Stack Overflow 问题 - 虽然有几种原生 JavaScript 方法可以完成此任务,但有没有一种方法可以使用纯粹的lodash 函数(或至少现有的原型函数)将字符串转换为标题大小写这样我就不必使用正则表达式或定义新函数?
eg例如
This string ShouLD be ALL in title CASe
should become应该成为
This String Should Be All In Title Case
_.startCase(_.camelCase(str))
For non-user-generated text, this handles more cases than the accepted answer对于非用户生成的文本,这比接受的答案处理更多的情况
> startCase(camelCase('myString'))
'My String'
> startCase(camelCase('my_string'))
'My String'
> startCase(camelCase('MY_STRING'))
'My String'
> startCase(camelCase('my string'))
'My String'
> startCase(camelCase('My string'))
'My String'
使用 lodash 版本 4。
_.upperFirst(_.toLower(str))
'This string ShouLD be ALL in title CASe'
.split(' ')
.map(_.capitalize)
.join(' ');
There are mixed answers to this question.这个问题有多种答案。 Some are recommending using _.upperFirst
while some recommending _.startCase
.有些人推荐使用_.upperFirst
而有些人推荐_.startCase
。
Know the difference between them.知道它们之间的区别。
i) _.upperFirst
will transform the first letter of your string, then string might be of a single word or multiple words but the only first letter of your string is transformed to uppercase. i) _.upperFirst
将转换您的字符串的第一个字母,然后字符串可能是单个单词或多个单词,但您的字符串唯一的第一个字母被转换为大写。
_.upperFirst('jon doe')
output:输出:
Jon doe
check the documentation https://lodash.com/docs/4.17.10#upperFirst检查文档https://lodash.com/docs/4.17.10#upperFirst
ii) _.startCase
will transform the first letter of every word inside your string. ii) _.startCase
将转换字符串中每个单词的第一个字母。
_.startCase('jon doe')
output:输出:
Jon Doe
https://lodash.com/docs/4.17.10#startCase https://lodash.com/docs/4.17.10#startCase
这是一种只使用 lodash 方法而不使用内置方法的方法:
_.reduce(_.map(_.split("Hello everyOne IN the WOrld", " "), _.capitalize), (a, b) => a + " " + b)
That's the cleanest & most flexible implementation imo from testing it on my own use cases.这是在我自己的用例上测试的最干净和最灵活的实现。
import { capitalize, map } from "lodash";
const titleCase = (str) => map(str.split(" "), capitalize).join(" ");
// titleCase("ALFRED NÚÑEZ") => "Alfred Núñez"
// titleCase("alfred núñez") => "Alfred Núñez"
// titleCase("AlFReD nÚñEZ") => "Alfred Núñez"
// titleCase("-") => "-"
const titleCase = str =>
str
.split(' ')
.map(str => {
const word = str.toLowerCase()
return word.charAt(0).toUpperCase() + word.slice(1)
})
.join(' ')
You can also split out the map function to do separate words也可以拆分map函数做单独的词
This can be done with only lodash这可以只用 lodash 来完成
properCase = string =>
words(string)
.map(capitalize)
.join(' ');
const proper = properCase('make this sentence propercase');
console.log(proper);
//would return 'Make This Sentence Propercase'
var s = 'This string ShouLD be ALL in title CASe';
_.map(s.split(' '), (w) => _.capitalize(w.toLowerCase())).join(' ')
Unless i missed it, lodash doesnt have its own lower/upper case methods.除非我错过了,否则 lodash 没有自己的小写/大写方法。
Not as concise as @4castle's answer, but descriptive and lodash-full, nonetheless...不像@4castle 的回答那么简洁,但描述性和 lodash-full,尽管如此......
var basicTitleCase = _ .chain('This string ShouLD be ALL in title CASe') .toLower() .words() .map(_.capitalize) .join(' ') .value() console.log('Result:', basicTitleCase) console.log('Exact Match:' , basicTitleCase === 'This String Should Be All In Title Case')
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>
Here's another solution for my use case: "devil's backbone"这是我的用例的另一个解决方案:“魔鬼的脊梁”
Simply:简单地说:
function titleCase (str) {
return _.map(str.split(' '), _.upperFirst).join(' ');
}
Using startCase would remove the apostrophe, so I had to work around that limitation.使用 startCase 会删除撇号,所以我必须解决这个限制。 The other solutions seemed pretty convoluted.其他解决方案似乎相当复杂。 I like this as it's clean, easy to understand.我喜欢这个,因为它干净,易于理解。
Below code will work perfectly:下面的代码将完美运行:
var str = "TITLECASE";
_.startCase(str.toLowerCase());
From https://github.com/lodash/lodash/issues/3383#issuecomment-430586750来自https://github.com/lodash/lodash/issues/3383#issuecomment-430586750
'JHON&JOHN C/O DR. BLah'.replace(/\w+/g, _.capitalize);
result:结果:
'Jhon&John C/O Dr. Blah'
with lodash 4, you can use _.capitalize()使用 lodash 4,你可以使用 _.capitalize()
_.capitalize('JOHN')
It returns 'John' _.capitalize('JOHN')
它返回 'John'
See https://lodash.com/docs/4.17.5#capitalize for details有关详细信息,请参阅https://lodash.com/docs/4.17.5#capitalize
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.