-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
226 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,31 @@ | ||
import { REGEXP_EXTENDED_ASCII, REGEXP_LATIN_WORD, REGEXP_WORD } from 'helper/reg_exp/const_extended'; | ||
import capitalize from 'case/capitalize'; | ||
import coerceToString from 'helper/string/coerce_to_string'; | ||
|
||
/** | ||
* Converts the subject to title case. | ||
* | ||
* @function snakeCase | ||
* @function titleCase | ||
* @static | ||
* @since 1.0.0 | ||
* @since 1.1.0 | ||
* @memberOf Case | ||
* @param {string} [subject=''] The string to convert to title case. | ||
* @param {string} [ignoreWords] The list of words that should not be changed. | ||
* @return {string} Returns the title case string. | ||
* @param {Array} [ignoreWords] The words that should not be capitalized. | ||
* @return {string} Returns the title cased string. | ||
* @example | ||
* v.snakeCase('learning to fly'); | ||
* // => 'learning_to_fly' | ||
* v.titleCase('learning to fly'); | ||
* // => 'Learning To Fly' | ||
* | ||
* v.snakeCase('LearningToFly'); | ||
* // => 'learning_to_fly' | ||
* v.titleCase('another brick in the wall', ['in', 'the']); | ||
* // => 'Another Brick in the Wall' | ||
* | ||
* v.snakeCase('-Learning-To-Fly-'); | ||
* // => 'learning_to_fly' | ||
*/ | ||
export default function titleCase(subject) { | ||
return subject; | ||
export default function titleCase(subject, ignoreWords) { | ||
const subjectString = coerceToString(subject); | ||
const ignoreWordsArray = Array.isArray(ignoreWords) ? ignoreWords : []; | ||
const wordsRegExp = REGEXP_EXTENDED_ASCII.test(subjectString) ? REGEXP_LATIN_WORD : REGEXP_WORD; | ||
return subjectString.replace(wordsRegExp, function(word) { | ||
const lowerCaseWord = word.toLowerCase(); | ||
return ignoreWordsArray.indexOf(lowerCaseWord) !== -1 ? lowerCaseWord : capitalize(lowerCaseWord, true); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { expect } from 'chai'; | ||
import v from '../voca'; | ||
|
||
describe('titleCase', function() { | ||
|
||
it('should return the title case of a string', function() { | ||
expect(v.titleCase('hello world')).to.be.equal('Hello World'); | ||
expect(v.titleCase('Hello world')).to.be.equal('Hello World'); | ||
expect(v.titleCase('hello World')).to.be.equal('Hello World'); | ||
expect(v.titleCase('Hello World')).to.be.equal('Hello World'); | ||
expect(v.titleCase('HELLO WORLD')).to.be.equal('Hello World'); | ||
expect(v.titleCase('bird')).to.be.equal('Bird'); | ||
expect(v.titleCase('BIRD')).to.be.equal('Bird'); | ||
expect(v.titleCase('bird-flight')).to.be.equal('Bird-Flight'); | ||
expect(v.titleCase('bird flight')).to.be.equal('Bird Flight'); | ||
expect(v.titleCase('san diego zoo safari park')).to.be.equal('San Diego Zoo Safari Park'); | ||
expect(v.titleCase('Who wants to try next?')).to.be.equal('Who Wants To Try Next?'); | ||
expect(v.titleCase('WHO WANTS TO TRY NEXT?')).to.be.equal('Who Wants To Try Next?'); | ||
expect(v.titleCase('-BIRD-FLIGHT-')).to.be.equal('-Bird-Flight-'); | ||
expect(v.titleCase('__BIRD___FLIGHT___')).to.be.equal('__Bird___Flight___'); | ||
expect(v.titleCase('Restless flycatcher')).to.be.equal('Restless Flycatcher'); | ||
expect(v.titleCase('XMLHttpRequest')).to.be.equal('XmlHttpRequest'); | ||
expect(v.titleCase('weight of up to 12 kg')).to.be.equal('Weight Of Up To 12 Kg'); | ||
expect(v.titleCase('/home/dmitri/projects/voca')).to.be.equal('/Home/Dmitri/Projects/Voca'); | ||
expect(v.titleCase('****')).to.be.equal('****'); | ||
expect(v.titleCase('-----')).to.be.equal('-----'); | ||
expect(v.titleCase(' ')).to.be.equal(' '); | ||
expect(v.titleCase('\n\n\n\n ***\t\t')).to.be.equal('\n\n\n\n ***\t\t'); | ||
expect(v.titleCase('')).to.be.equal(''); | ||
}); | ||
|
||
it('should return the title case of a non-latin string', function() { | ||
expect(v.titleCase('zborul păsării')).to.be.equal('Zborul Păsării'); | ||
expect(v.titleCase('полет птицы')).to.be.equal('Полет Птицы'); | ||
expect(v.titleCase('fuerza de sustentación')).to.be.equal('Fuerza De Sustentación'); | ||
expect(v.titleCase('skrzydło ptaka składa się')).to.be.equal('Skrzydło Ptaka Składa Się'); | ||
}); | ||
|
||
it('should return the title case and not capitalize specific words', function() { | ||
expect(v.titleCase('Who wants to try next?', ['to'])).to.be.equal('Who Wants to Try Next?'); | ||
expect(v.titleCase('WHO WANTS TO TRY NEXT?', ['to'])).to.be.equal('Who Wants to Try Next?'); | ||
expect(v.titleCase('Well, congratulations! You got yourself caught! Now what\'s the next step in your master plan?', ["s", 'the', 'in'])) | ||
.to.be.equal('Well, Congratulations! You Got Yourself Caught! Now What\'s the Next Step in Your Master Plan?'); | ||
}); | ||
|
||
it('should not modify numbers', function() { | ||
expect(v.titleCase(0)).to.be.equal('0'); | ||
expect(v.titleCase(1200)).to.be.equal('1200'); | ||
expect(v.titleCase('8965')).to.be.equal('8965'); | ||
}); | ||
|
||
it('should return the title case of a string representation of an object', function() { | ||
expect(v.titleCase(['bird flight'])).to.be.equal('Bird Flight'); | ||
expect(v.titleCase({ | ||
toString: function() { | ||
return 'bird flight'; | ||
} | ||
})).to.be.equal('Bird Flight'); | ||
}); | ||
|
||
it('should return empty string for null or undefined', function() { | ||
expect(v.titleCase()).to.be.equal(''); | ||
expect(v.titleCase(undefined)).to.be.equal(''); | ||
expect(v.titleCase(null)).to.be.equal(''); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.