Skip to content

Commit

Permalink
fix:title-abbreviations problem
Browse files Browse the repository at this point in the history
  • Loading branch information
fatteme committed Dec 17, 2020
1 parent d3183a8 commit 5b35868
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 27 deletions.
11 changes: 8 additions & 3 deletions dist/voca.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@
}

/**
* Converts the uppercase alpha caracters of `subject` to lowercase and lowercase
* Converts the uppercase alpha characters of `subject` to lowercase and lowercase
* characters to uppercase.
*
* @function swapCase
Expand Down Expand Up @@ -818,22 +818,27 @@
* @memberOf Case
* @param {string} [subject=''] The string to convert to title case.
* @param {Array} [noSplit] Do not split words at the specified characters.
* @param {boolean} [preserveUpperCase=false] preserve uppercases.
* @return {string} Returns the title case string.
* @example
* v.titleCase('learning to fly');
* // => 'Learning To Fly'
*
* v.titleCase('XMLHttpRequest', preserveUpperCase=true);
* // => 'XMLHttpRequest'
*
* v.titleCase('jean-luc is good-looking', ['-']);
* // => 'Jean-luc Is Good-looking'
*/

function titleCase(subject, noSplit) {
function titleCase(subject, noSplit, preserveUpperCase) {
var subjectString = coerceToString(subject);
var noSplitArray = Array.isArray(noSplit) ? noSplit : [];
var preserveUpperCaseBoolean = coerceToBoolean(preserveUpperCase);
var wordsRegExp = REGEXP_EXTENDED_ASCII.test(subjectString) ? REGEXP_LATIN_WORD : REGEXP_WORD;
return subjectString.replace(wordsRegExp, function(word, index) {
var isNoSplit = index > 0 && noSplitArray.indexOf(subjectString[index - 1]) >= 0;
return isNoSplit ? word.toLowerCase() : capitalize(word, true);
return isNoSplit ? word.toLowerCase() : capitalize(word, !preserveUpperCaseBoolean);
});
}

Expand Down
43 changes: 22 additions & 21 deletions dist/voca.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/voca.min.js.map

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions src/case/title_case.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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';
import coerceToBoolean from 'helper/boolean/coerce_to_boolean';

/**
* Converts the subject to title case.
Expand All @@ -11,20 +12,25 @@ import coerceToString from 'helper/string/coerce_to_string';
* @memberOf Case
* @param {string} [subject=''] The string to convert to title case.
* @param {Array} [noSplit] Do not split words at the specified characters.
* @param {boolean} [preserveUpperCase=false] preserve uppercases.
* @return {string} Returns the title case string.
* @example
* v.titleCase('learning to fly');
* // => 'Learning To Fly'
*
* v.titleCase('XMLHttpRequest', preserveUpperCase=true);
* // => 'XMLHttpRequest'
*
* v.titleCase('jean-luc is good-looking', ['-']);
* // => 'Jean-luc Is Good-looking'
*/
export default function titleCase(subject, noSplit) {
export default function titleCase(subject, noSplit, preserveUpperCase) {
const subjectString = coerceToString(subject);
const noSplitArray = Array.isArray(noSplit) ? noSplit : [];
const preserveUpperCaseBoolean = coerceToBoolean(preserveUpperCase);
const wordsRegExp = REGEXP_EXTENDED_ASCII.test(subjectString) ? REGEXP_LATIN_WORD : REGEXP_WORD;
return subjectString.replace(wordsRegExp, function(word, index) {
const isNoSplit = index > 0 && noSplitArray.indexOf(subjectString[index - 1]) >= 0;
return isNoSplit ? word.toLowerCase() : capitalize(word, true);
return isNoSplit ? word.toLowerCase() : capitalize(word, !preserveUpperCaseBoolean);
});
}
26 changes: 26 additions & 0 deletions test/case/title_case.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,30 @@ describe('titleCase', function() {
expect(v.titleCase(undefined)).toBe('');
expect(v.titleCase(null)).toBe('');
});

it('should return the title case of a string while preserving upper cases', function() {
expect(v.titleCase('hello world', [], true)).toBe('Hello World');
expect(v.titleCase('Hello world', [], true)).toBe('Hello World');
expect(v.titleCase('hello World', [], true)).toBe('Hello World');
expect(v.titleCase('Hello World', [], true)).toBe('Hello World');
expect(v.titleCase('HELLO WORLD', [], true)).toBe('HELLO WORLD');
expect(v.titleCase('bird', [], true)).toBe('Bird');
expect(v.titleCase('BIRD', [], true)).toBe('BIRD');
expect(v.titleCase('bird-flight', [], true)).toBe('Bird-Flight');
expect(v.titleCase('bird flight', [], true)).toBe('Bird Flight');
expect(v.titleCase('san diego zoo safari park', [], true)).toBe('San Diego Zoo Safari Park');
expect(v.titleCase('Who wants to try next?', [], true)).toBe('Who Wants To Try Next?');
expect(v.titleCase('WHO WANTS TO TRY NEXT?', [], true)).toBe('WHO WANTS TO TRY NEXT?');
expect(v.titleCase('-BIRD-FLIGHT-', [], true)).toBe('-BIRD-FLIGHT-');
expect(v.titleCase('__BIRD___FLIGHT___', [], true)).toBe('__BIRD___FLIGHT___');
expect(v.titleCase('Restless flycatcher', [], true)).toBe('Restless Flycatcher');
expect(v.titleCase('XMLHttpRequest', [], true)).toBe('XMLHttpRequest');
expect(v.titleCase('weight of up to 12 kg', [], true)).toBe('Weight Of Up To 12 Kg');
expect(v.titleCase('/home/dmitri/projects/voca', [], true)).toBe('/Home/Dmitri/Projects/Voca');
expect(v.titleCase('****', [], true)).toBe('****');
expect(v.titleCase('-----', [], true)).toBe('-----');
expect(v.titleCase(' ', [], true)).toBe(' ');
expect(v.titleCase('\n\n\n\n ***\t\t', [], true)).toBe('\n\n\n\n ***\t\t');
expect(v.titleCase('', [], true)).toBe('');
});
});

0 comments on commit 5b35868

Please sign in to comment.