Skip to content

Instantly share code, notes, and snippets.

@robjens
Last active July 31, 2016 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robjens/fd57bb81f1011fd3665ccc5396f3d467 to your computer and use it in GitHub Desktop.
Save robjens/fd57bb81f1011fd3665ccc5396f3d467 to your computer and use it in GitHub Desktop.
SImple JavaScript tokenize, cases, simple but nice to remember
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