All files / to-case to-title-case.ts

100% Statements 33/33
100% Branches 4/4
100% Functions 1/1
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 341x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 100x 22x 22x 26x 22x 22x 2x 22x 22x 1x 1x  
import escape from "../libraries/escape-regexp-component";
import minors from "../libraries/title-case-minors";
import sentence from "./to-sentence-case";
 
/**
 * Matchers.
 */
 
var escaped = minors.map(escape);
var minorMatcher = new RegExp("[^^]\\b(" + escaped.join("|") + ")\\b", "ig");
var punctuationMatcher = /:\s*(\w)/g;
 
/**
 * Convert a `string` to title case.
 *
 * @param {String} string
 * @return {String}
 */
 
function toTitleCase(string) {
  return sentence(string)
    .replace(/(^|\s)(\w)/g, function (matches, previous, letter) {
      return previous + letter.toUpperCase();
    })
    .replace(minorMatcher, function (minor) {
      return minor.toLowerCase();
    })
    .replace(punctuationMatcher, function (letter) {
      return letter.toUpperCase();
    });
}
 
export default toTitleCase;