From b46e3d5bf4b83bc48ba940f5122fc4e0bdd6a7b4 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sun, 29 Mar 2020 18:05:48 +0000 Subject: [PATCH] [COMMON] Added toCamelCase and toSnakeCase functions --- src/Util/Common.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Util/Common.php b/src/Util/Common.php index 49aeef9272..cb0408020e 100644 --- a/src/Util/Common.php +++ b/src/Util/Common.php @@ -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))); + } }