2015-01-12

Nerdy joy using Humanizer to mutate variables in print()

I've added a super cool resource to the project, Humanizer. So far I've integrated a good slice of it's functionality directly into the print() function. Here are some code samples along with the output created.
:: Humanizer Tests
WORD TRANSFORMATION

set($txtKey = "text sample of some words")
$txtKey = 'print($txtKey)'
$txtKey -> uppercase = 'print($txtKey, "uppercase")'
$txtKey -> lowercase = 'print($txtKey, "lowercase")'
$txtKey -> titlecase = 'print($txtKey, "titlecase")'
$txtKey -> sentencecase = 'print($txtKey, "sentencecase")'
$txtKey -> underscore -> pascal = 'print($txtKey, "underscore", "pascal")'
$txtKey -> underscore -> camelcase = 'print($txtKey, "underscore", "camelcase")'
$txtKey -> underscore = 'print($txtKey, "underscore")'
$txtKey -> underscore -> hyphenate = 'print($txtKey, "underscore", "hyphenate")'

line(1)

SINGULAR/PLURAL & QUANTITIES

set($goatKey = "goat")
$goatKey = 'print($goatKey)'
$goatKey -> plural = 'print($goatKey, "plural")'
$goatKey -> toquantitynone, 0 = 'print($goatKey, "toquantitynone", 0)'
$goatKey -> toquantitynone, 1 = 'print($goatKey, "toquantitynone", 1)'
$goatKey -> toquantitynone, 11 = 'print($goatKey, "toquantitynone", 11)'
$goatKey -> toquantitynumeric, 0 = 'print($goatKey, "toquantitynumeric", 0)'
$goatKey -> toquantitynumeric, 1 = 'print($goatKey, "toquantitynumeric", 1)'
$goatKey -> toquantitynumeric, 11 = 'print($goatKey, "toquantitynumeric", 11)'
$goatKey -> toquantitywords, 0 = 'print($goatKey, "toquantitywords", 0)'
$goatKey -> toquantitywords, 1 = 'print($goatKey, "toquantitywords", 1)'
$goatKey -> toquantitywords, 11 = 'print($goatKey, "toquantitywords", 11)'

set($womenKey = "women")
$womenKey = 'print($womenKey)'
$womenKey -> singular = 'print($womenKey, "singular")'
$womenKey -> toquantitynone, 0 = 'print($womenKey, "toquantitynone", 0)'
$womenKey -> toquantitynone, 1 = 'print($womenKey, "toquantitynone", 1)'
$womenKey -> toquantitynone, 11 = 'print($womenKey, "toquantitynone", 11)'
$womenKey -> toquantitynumeric, 0 = 'print($womenKey, "toquantitynumeric", 0)'
$womenKey -> toquantitynumeric, 1 = 'print($womenKey, "toquantitynumeric", 1)'
$womenKey -> toquantitynumeric, 11 = 'print($womenKey, "toquantitynumeric", 11)'
$womenKey -> toquantitywords, 0 = 'print($womenKey, "toquantitywords", 0)'
$womenKey -> toquantitywords, 1 = 'print($womenKey, "toquantitywords", 1)'
$womenKey -> toquantitywords, 11 = 'print($womenKey, "toquantitywords", 11)'

set($disappearanceKey = "the disappearance of 'Razor'")
$disappearanceKey = 'print($disappearanceKey)'
$disappearanceKey -> truncate, 7 = 'print($disappearanceKey, "truncate", 7)'
$disappearanceKey -> truncatecharacter, 7 = 'print($disappearanceKey, "truncatecharacter", 7)'
$disappearanceKey -> truncateword, 2 = 'print($disappearanceKey, "truncateword", 2)'
$disappearanceKey -> truncateleft, 7 = 'print($disappearanceKey, "truncateleft", 7)'
$disappearanceKey -> truncatecharacterleft, 7 = 'print($disappearanceKey, "truncatecharacterleft", 7)'
$disappearanceKey -> truncatewordleft, 2 = 'print($disappearanceKey, "truncatewordleft", 2)'

line(1)

NUMBERS

set($numberKey = 0)
$numberKey = 'print($numberKey)'
$numberKey -> numbertowords = 'print($numberKey, "numbertowords")'
$numberKey -> numbertowordsordinal = 'print($numberKey, "numbertowordsordinal")'
$numberKey -> numbertoroman = 'print($numberKey, "numbertoroman")'

set($number1Key = 1)
$number1Key = 'print($number1Key)'
$number1Key -> numbertowords = 'print($number1Key, "numbertowords")'
$number1Key -> numbertowordsordinal = 'print($number1Key, "numbertowordsordinal")'
$number1Key -> numbertoroman = 'print($number1Key, "numbertoroman")'

set($number55Key = 55)
$number55Key = 'print($number55Key)'
$number55Key -> numbertowords = 'print($number55Key, "numbertowords")'
$number55Key -> numbertowordsordinal = 'print($number55Key, "numbertowordsordinal")'
$number55Key -> numbertoroman = 'print($number55Key, "numbertoroman")'

set($number1111Key = 1111)
$number1111Key = 'print($number1111Key)'
$number1111Key -> numbertowords = 'print($number1111Key, "numbertowords")'
$number1111Key -> numbertowordsordinal = 'print($number1111Key, "numbertowordsordinal")'
$number1111Key -> numbertoroman = 'print($number1111Key, "numbertoroman")'


The really nice thing is that these commands can be chained until the input variable is printed in the correct format. For example in order to make the $txtKey variable into camelcase, I had to underscore it first. There were a few issues with some of the localization stuff, but I think this is only a problem on my particular system. Even so I might wrap all the calls to Humanizer into a try catch block and print out the default variable values if things fail. Still, I can see all kinds of potential uses for these functions. I'm not entirely happy with the verbose arguments in the print call, but that is easy enough to change later. I will probably expose all of this stuff in a transformation function so that modifications are saved into the variables permanently if desired.

No comments:

Post a Comment