change-case
Advanced tools
Comparing version 1.0.1 to 1.0.2
61
index.js
@@ -70,4 +70,4 @@ /** | ||
*/ | ||
e.lowerCase = e.lower = acceptString(function (string) { | ||
return string.toLocaleLowerCase(); | ||
e.lowerCase = acceptString(function (string) { | ||
return string.toLowerCase(); | ||
}); | ||
@@ -81,4 +81,4 @@ | ||
*/ | ||
e.upperCase = e.upper = acceptString(function (string) { | ||
return string.toLocaleUpperCase(); | ||
e.upperCase = acceptString(function (string) { | ||
return string.toUpperCase(); | ||
}); | ||
@@ -92,3 +92,3 @@ | ||
*/ | ||
e.upperCaseFirst = e.upperFirst = acceptString(function (string) { | ||
e.upperCaseFirst = acceptString(function (string) { | ||
return e.upperCase(string.charAt(0)) + string.substr(1); | ||
@@ -103,3 +103,3 @@ }); | ||
*/ | ||
e.lowerCaseFirst = e.lowerFirst = acceptString(function (string) { | ||
e.lowerCaseFirst = acceptString(function (string) { | ||
return e.lowerCase(string.charAt(0)) + string.substr(1); | ||
@@ -112,26 +112,33 @@ }); | ||
* | ||
* @param {String} string | ||
* @param {String} string | ||
* @param {Boolean} significant | ||
* @return {String} | ||
*/ | ||
e.titleCase = e.title = acceptString(function (string) { | ||
var prevSeparator = ''; | ||
e.titleCase = e.title = acceptString(function (string, significant) { | ||
return string | ||
// Remove prefixed whitespace. | ||
.replace(/^\s/, '') | ||
// Remove suffixed whitespace. | ||
.replace(/\s$/, '') | ||
// Turn all whitespace into a single space character. | ||
.replace(/\s+/g, ' ') | ||
// Replace word strings. Matches "W.H.O", "tests'", "test's", "test", etc. | ||
.replace( | ||
/(?:(?:[a-zA-Z]\.)+[a-zA-Z]|[a-zA-Z\'\-]+[a-zA-Z]*)/g, | ||
function (word, index) { | ||
// Uppercase "W.H.O" | ||
if (/(?:[a-zA-Z]\.)+[a-zA-Z]/g.test(word)) { | ||
return e.upperCase(word); | ||
} | ||
return sanitizeString(string, function (word, index) { | ||
// The first word of the string should always be capitalized. | ||
if (index > 0 && (word.length < 3 || ~e.insignificantWords.indexOf(word))) { | ||
return prevSeparator === '.' && word.length === 1 ? | ||
e.upperCase(word) : e.lowerCase(word); | ||
} | ||
if (!significant && index > 0) { | ||
if (word.length < 3 || ~e.insignificantWords.indexOf(word)) { | ||
return e.lowerCase(word); | ||
} | ||
} | ||
// Returns the word with the first character uppercased. | ||
return e.upperCaseFirst(e.lowerCase(word)); | ||
}, function (separator) { | ||
// Remove extra spaces. | ||
separator = separator.replace(/ +/, ' '); | ||
// Alias the previous separator for use in the next match. | ||
prevSeparator = separator; | ||
return separator; | ||
}); | ||
return e.upperCaseFirst(e.lowerCase(word)); | ||
}) | ||
// Fix broken sentence formatting. | ||
.replace(/(\.\,\!\?\;)([a-zA-Z0-9]+) /g, '$1 $2'); | ||
}); | ||
@@ -228,3 +235,3 @@ | ||
e.switchCase = e.switch = acceptString(function (string) { | ||
return string.replace(/\w/g, function (c) { | ||
return string.replace(/[a-zA-Z]/g, function (c) { | ||
var u = e.upperCase(c); | ||
@@ -231,0 +238,0 @@ return c === u ? e.lowerCase(c) : u; |
{ | ||
"name": "change-case", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Convert strings between camelCase, PascalCase, Title Case, snake_case, etc", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -53,5 +53,5 @@ # Change Case | ||
**titleCase** changeCase.titleCase(string) | ||
**titleCase** changeCase.titleCase(string, capitalizeAll) | ||
Title cases a string, useful for user inputted values being shown as titles. It does minimal sanitization of characters and will not handle non-sentence formats correctly (since it risks breaking formatting). If you need to parse a string in a different format, pass it through `sentenceCase` first. | ||
Title cases a string, usually user inputted strings that should be displayed as titles. It attempts to correct formatting of the sentence. If you need to parse a string in a different format, trying passing it through `sentenceCase` first. | ||
@@ -61,2 +61,8 @@ ```js | ||
--> "A Simple Test" | ||
changeCase.titleCase('i found a bug'); | ||
--> "I Found a Bug" | ||
changeCase.titleCase('i found a bug', true); | ||
--> "I Found A Bug" | ||
``` | ||
@@ -63,0 +69,0 @@ |
@@ -25,2 +25,8 @@ /* global describe, it */ | ||
assert.equal(changeCase.titleCase('s.p.e.c'), 'S.P.E.C'); | ||
assert.equal(changeCase.titleCase('another s.p.e.c'), 'Another S.P.E.C'); | ||
assert.equal(changeCase.titleCase('i found a bug'), 'I Found a Bug'); | ||
assert.equal(changeCase.titleCase('i found a bug', true), 'I Found A Bug'); | ||
assert.equal(changeCase.titleCase('some things\''), 'Some Things\''); | ||
assert.equal(changeCase.titleCase('"quotes"'), '"Quotes"'); | ||
assert.equal(changeCase.titleCase('hyphen-ness'), 'Hyphen-ness'); | ||
}); | ||
@@ -27,0 +33,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14994
297
143