New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add function for converting strings in snake_case to CamelCase #23
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks good to me, but I'm curious - what is the use case for this, and is it a common enough one to include in this library?
'snake', | ||
'snake', | ||
false, | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed you had some special handling for extra underscores - it might be good to see some tests of that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call.
(@shadowhand presumably has a fix in the works for the Travis failures, by the way.) |
*/ | ||
function snakeToCamelCase($snake_string, $first = false) | ||
{ | ||
$camel = implode('', array_map(function($piece) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be a static function
as a micro optimization.
I remember seeing those errors with Travis before, but I am not sure what they are caused by. |
@bvisness I need the function for use in
|
I say go for it. (Once the Travis issues are resolved, anyway... @shadowhand, perhaps it is an old version of PHPUnit again?) |
@bvisness this build had no issues: https://travis-ci.org/equip/assist/jobs/155386468 |
function snakeToCamelCase($snake_string, $first = false) | ||
{ | ||
$camel = implode('', array_map(static function($piece) { | ||
return empty($piece) ? '' : ucfirst(strtolower($piece)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using array_filter
would remove the need for this ternary.
{ | ||
$camel = implode('', array_map(static function($piece) { | ||
return empty($piece) ? '' : ucfirst(strtolower($piece)); | ||
}, explode('_', $snake_string))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replacing this explode with preg_split('/_++/', $snake_string)
would also remove the need for the ternary, as empty values would be impossible.
The most recent version of lstrojny/functional-php (1.4) is incompatible with HHVM so we much lock the version's upper bound in order for equip/assist to continue to work on HHVM.
f4a41a6
to
b8651d8
Compare
@shadowhand @bvisness Looks like |
No description provided.