[COMMON] Added toCamelCase and toSnakeCase functions

This commit is contained in:
Hugo Sales 2020-03-29 18:05:48 +00:00 committed by Hugo Sales
parent 98a5b89e42
commit c38b9a1503
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
1 changed files with 24 additions and 0 deletions

View File

@ -107,4 +107,28 @@ abstract class Common
return self::endsWith($haystack, $needle);
});
}
/**
* Call $func with only abs($count) arguments, taken either from the
* left or right depending on the sign
*/
public static function arity(callable $func, int $count): callable
{
return function (...$args) use ($func, $count) {
if ($count > 0) {
return call_user_func_array($func, F\take_left($args, $count));
}
return call_user_func_array($func, F\take_right($args, -$count));
};
}
public static function toSnakeCase(string $str): string
{
return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $str));
}
public static function toCamelCase(string $str): string
{
return implode('', F\map(preg_split('/[\b_]/', $str), self::arity('ucfirst', 1)));
}
}