版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

30 lines
1.2 KiB

  1. var SMALL_WORDS = /\b(?:an?d?|a[st]|because|but|by|en|for|i[fn]|neither|nor|o[fnr]|only|over|per|so|some|tha[tn]|the|to|up|upon|vs?\.?|versus|via|when|with|without|yet)\b/i;
  2. var TOKENS = /[^\s:–—-]+|./g;
  3. var WHITESPACE = /\s/;
  4. var IS_MANUAL_CASE = /.(?=[A-Z]|\..)/;
  5. var ALPHANUMERIC_PATTERN = /[A-Za-z0-9\u00C0-\u00FF]/;
  6. export function titleCase(input) {
  7. var result = "";
  8. var m;
  9. // tslint:disable-next-line
  10. while ((m = TOKENS.exec(input)) !== null) {
  11. var token = m[0], index = m.index;
  12. if (
  13. // Ignore already capitalized words.
  14. !IS_MANUAL_CASE.test(token) &&
  15. // Ignore small words except at beginning or end.
  16. (!SMALL_WORDS.test(token) ||
  17. index === 0 ||
  18. index + token.length === input.length) &&
  19. // Ignore URLs.
  20. (input.charAt(index + token.length) !== ":" ||
  21. WHITESPACE.test(input.charAt(index + token.length + 1)))) {
  22. // Find and uppercase first word character, skips over *modifiers*.
  23. result += token.replace(ALPHANUMERIC_PATTERN, function (m) { return m.toUpperCase(); });
  24. continue;
  25. }
  26. result += token;
  27. }
  28. return result;
  29. }
  30. //# sourceMappingURL=index.js.map