Last active
July 31, 2016 17:44
-
-
Save robjens/fd57bb81f1011fd3665ccc5396f3d467 to your computer and use it in GitHub Desktop.
SImple JavaScript tokenize, cases, simple but nice to remember
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var | |
word = 'isAWordNotWrittenNormallyLikeThis', | |
// total number of characters in a word | |
numChars = word.length, | |
// number of latin capital letters in the word | |
numCapitals = word.match(/[A-Z]+/g).length, | |
// capital character coverage | |
pctCapitals = Math.floor((numCapitals * 100) / word.length), | |
// abbreviations must be all capitals | |
isAbbr = word.length === numCapitals, | |
// is the word in camel-case? it should start with lower-case and have at least 1 upper-case | |
isCamelCase = /^[a-z]/g.test(word) && numCapitals >= 1, | |
// title-case must have 1st character as capital and no more | |
isTitle = /^[A-Z]/g.test(word) ? (word.length - numCapitals) + 1 === word.length : false, | |
// array from word split on upper-case characters or first lower, forced to all lower | |
arrWords = (isCamelCase ? word.match(/(^[a-z]+|[A-Z][a-z]+|[A-Z])/g) : word.match(/[A-Z][a-z]+/g)) | |
.map(function(word) { return word.toLowerCase(); }) | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment