Lodash 제목 대소 문자 (모든 단어의 첫 번째 대문자)
이 작업을 수행하는 몇 가지 기본 JavaScript 방법이 있지만 lodash 문서 및 기타 Stack Overflow 질문을 살펴보고 있습니다. 순전히 lodash 함수 (또는 최소한 기존 프로토 타입 함수)를 사용하여 문자열을 제목 케이스로 변환 할 수있는 방법이 있습니까? 정규식을 사용하거나 새 함수를 정의 할 필요가 없도록?
예 :
This string ShouLD be ALL in title CASe
되어야한다
This String Should Be All In Title Case
다음을 약간 수정하면됩니다 startCase.
_.startCase(_.toLower(str));
console.log(_.startCase(_.toLower("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>
_.startCase(_.camelCase(str))
사용자가 생성하지 않은 텍스트의 경우 허용 된 답변보다 더 많은 케이스를 처리합니다.
> 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(' ');
이 질문에 대한 대답은 엇갈립니다. 일부는 사용 _.upperFirst을 권장 하고 일부는 _.startCase.
그들 사이의 차이점을 아십시오.
i) _.upperFirst문자열의 첫 번째 문자를 변환 한 다음 문자열은 단일 단어 또는 여러 단어로 구성 될 수 있지만 문자열의 첫 번째 문자 만 대문자로 변환됩니다.
_.upperFirst('jon doe')
산출:
Jon doe
문서 https://lodash.com/docs/4.17.10#upperFirst를 확인 하십시오.
ii) _.startCase문자열 안에있는 모든 단어의 첫 글자를 변환합니다.
_.startCase('jon doe')
산출:
Jon Doe
https://lodash.com/docs/4.17.10#startCase
다음은 lodash 메서드 만 사용하고 내장 메서드는 사용하지 않는 방법입니다.
_.reduce(_.map(_.split("Hello everyOne IN the WOrld", " "), _.capitalize), (a, b) => a + " " + b)
var s = 'This string ShouLD be ALL in title CASe';
_.map(s.split(' '), (w) => _.capitalize(w.toLowerCase())).join(' ')
내가 놓치지 않는 한 lodash에는 자체 소문자 / 대문자 방법이 없습니다.
@ 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>
내 사용 사례에 대한 또 다른 솔루션이 있습니다. "악마의 백본"
간단히:
function titleCase (str) {
return _.map(str.split(' '), _.upperFirst).join(' ');
}
Using startCase would remove the apostrophe, so I had to work around that limitation. 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());
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
This can be done with only lodash
properCase = string =>
words(string)
.map(capitalize)
.join(' ');
const proper = properCase('make this sentence propercase');
console.log(proper);
//would return 'Make This Sentence Propercase'
with lodash 4, you can use _.capitalize()
_.capitalize('JOHN') It returns 'John'
See https://lodash.com/docs/4.17.5#capitalize for details
참고URL : https://stackoverflow.com/questions/38084396/lodash-title-case-uppercase-first-letter-of-every-word
'Program Club' 카테고리의 다른 글
| colid 6에 대해 bcp 클라이언트로부터 유효하지 않은 열 길이를 받았습니다. (0) | 2020.10.22 |
|---|---|
| 파이썬 변수 선언 (0) | 2020.10.22 |
| Base64로 이미지 변환 (0) | 2020.10.22 |
| Ruby On Rails가 엔터프라이즈에 적합합니까? (0) | 2020.10.22 |
| View.GONE이있는 Android View는 여전히 onTouch 및 onClick을 수신합니다. (0) | 2020.10.22 |