Skip to content

Commit

Permalink
Implemented titleCase() function
Browse files Browse the repository at this point in the history
  • Loading branch information
panzerdp committed Jan 9, 2017
1 parent 49a48b9 commit c3004f2
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"plugins": [
"add-module-exports",
"transform-object-assign",
"transform-es2015-block-scoping",
"add-module-exports",
"transform-es2015-modules-commonjs",
"transform-es2015-parameters",
"transform-es2015-shorthand-properties",
Expand Down
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ brings and possible use cases.

### Creating pull requests
Pull requests are great :+1:.
When contributing to this repository, please **first discuss the change** you wish to make via issue, email to [dmitri@rainsoft.io](email),
When contributing to this repository, please **first discuss the change** you wish to make via issue, email to [dmitri@rainsoft.io][email],
or any other method with the owners of this repository before making a change.

When submitting the request please be sure:
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"babel-root-import": "4.1.5",
"chai": "3.5.0",
"codecov.io": "0.1.6",
"eslint": "3.12.2",
"eslint": "3.13.0",
"glob": "7.1.1",
"grunt": "1.0.1",
"grunt-contrib-connect": "1.0.2",
Expand All @@ -76,10 +76,10 @@
"jsdoc": "3.4.3",
"mkdirp": "0.5.1",
"mocha": "3.2.0",
"rollup": "0.38.0",
"rollup": "0.41.1",
"rollup-plugin-babel": "2.7.1",
"rollup-plugin-uglify": "1.0.1",
"source-map-support": "0.4.8"
},
"dependencies": {}
}
}
32 changes: 20 additions & 12 deletions src/case/title_case.js
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);
});
}
2 changes: 2 additions & 0 deletions src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kebabCase from './case/kebab_case';
import lowerCase from './case/lower_case';
import snakeCase from './case/snake_case';
import upperCase from './case/upper_case';
import titleCase from './case/title_case';
import truncate from './chop/truncate';

/**
Expand Down Expand Up @@ -134,6 +135,7 @@ export default {
kebabCase,
lowerCase,
snakeCase,
titleCase,
upperCase,

count,
Expand Down
1 change: 0 additions & 1 deletion test/case/snake_case.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ describe('snakeCase', function() {
expect(v.snakeCase('/home/dmitri/projects/voca')).to.be.equal('home_dmitri_projects_voca');
expect(v.snakeCase(PRINTABLE_ASCII)).to.be.equal('0123456789_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz');
expect(v.snakeCase('****')).to.be.equal('');
expect(v.snakeCase('****')).to.be.equal('');
expect(v.snakeCase('-----')).to.be.equal('');
expect(v.snakeCase(' ')).to.be.equal('');
expect(v.snakeCase('\n\n\n\n ***\t\t')).to.be.equal('');
Expand Down
67 changes: 67 additions & 0 deletions test/case/title_case.js
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('');
});

});
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import './case/decapitalize';
import './case/kebab_case';
import './case/lower_case';
import './case/snake_case';
import './case/title_case';
import './case/upper_case';

//chain
Expand Down
6 changes: 6 additions & 0 deletions test/modules_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ describe('CommonJS modules', function() {
expect(snakeCase('-BIRD-FLIGHT-')).to.be.equal('bird_flight');
});

it('should require titleCase()', function() {
const titleCase = require('../dist_mod/title_case');
expect(titleCase('BIRD FLIGHT')).to.be.equal('Bird Flight');
});


it('should require upperCase()', function() {
const upperCase = require('../dist_mod/upper_case');
expect(upperCase('Earth')).to.be.equal('EARTH');
Expand Down
5 changes: 5 additions & 0 deletions test/modules_es2015.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import capitalize from '../dist_mod/capitalize';
import decapitalize from '../dist_mod/decapitalize';
import kebabCase from '../dist_mod/kebab_case';
import lowerCase from '../dist_mod/lower_case';
import titleCase from '../dist_mod/title_case';
import snakeCase from '../dist_mod/snake_case';

// Chop
Expand Down Expand Up @@ -132,6 +133,10 @@ describe('CommonJS modules', function() {
expect(snakeCase('-BIRD-FLIGHT-')).to.be.equal('bird_flight');
});

it('should require titleCase()', function() {
expect(titleCase('BIRD FLIGHT')).to.be.equal('Bird Flight');
});

it('should require upperCase()', function() {
expect(upperCase('Earth')).to.be.equal('EARTH');
});
Expand Down
Loading

0 comments on commit c3004f2

Please sign in to comment.