From f601d64211e89f8d9dab558fae48143fabf9d24d Mon Sep 17 00:00:00 2001 From: kekefreedog <70959083+kekefreedog@users.noreply.github.com> Date: Tue, 12 Mar 2024 01:19:13 +0100 Subject: [PATCH] Add helpers --- resources/Js/Handlebars/gt.js | 22 +++++ resources/Js/Handlebars/gte.js | 25 +++++ resources/Js/Handlebars/lt.js | 24 +++++ resources/Js/Handlebars/lte.js | 25 +++++ resources/Js/Handlebars/replace.js | 20 ++++ src/Library/Array/Arrays.php | 2 +- src/Library/Template/Handlebars/Helpers.php | 100 ++++++++++++++++++++ 7 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 resources/Js/Handlebars/gt.js create mode 100644 resources/Js/Handlebars/gte.js create mode 100644 resources/Js/Handlebars/lt.js create mode 100644 resources/Js/Handlebars/lte.js create mode 100644 resources/Js/Handlebars/replace.js diff --git a/resources/Js/Handlebars/gt.js b/resources/Js/Handlebars/gt.js new file mode 100644 index 0000000..e389e89 --- /dev/null +++ b/resources/Js/Handlebars/gt.js @@ -0,0 +1,22 @@ +/** + * Handlebars Comparaison Helpers + * + * @source https://github.com/helpers/handlebars-helpers + * + * @package kzarshenas/crazyphp + * @author kekefreedog + * @copyright 2022-2023 Kévin Zarshenas + */ + +/** + * Gt + * + * Block helper that renders a block if a is greater than b. + * If an inverse block is specified it will be rendered when falsy. You may optionally use the compare="" hash argument for the second value. + * + * @param a Value to compare + * @param b Value to compare with + * + * @return boolean + */ +module.exports = (a, b, options) => (a > b) ? options.fn(this) : options.inverse(this); \ No newline at end of file diff --git a/resources/Js/Handlebars/gte.js b/resources/Js/Handlebars/gte.js new file mode 100644 index 0000000..89ebe4d --- /dev/null +++ b/resources/Js/Handlebars/gte.js @@ -0,0 +1,25 @@ +/** + * Handlebars Comparaison Helpers + * + * @source https://github.com/helpers/handlebars-helpers + * + * @package kzarshenas/crazyphp + * @author kekefreedog + * @copyright 2022-2023 Kévin Zarshenas + */ + +/** + * Gte + * + * Block helper that renders a block if `a` is **greater than or + * equal to** `b`. + * If an inverse block is specified it will be rendered when falsy. + * You may optionally use the `compare=""` hash argument for the + * second value. + * + * @param a Value to compare + * @param b Value to compare with + * + * @return boolean + */ +module.exports = (a, b, options) => (a >= b) ? options.fn(this) : options.inverse(this); \ No newline at end of file diff --git a/resources/Js/Handlebars/lt.js b/resources/Js/Handlebars/lt.js new file mode 100644 index 0000000..0a0a7c5 --- /dev/null +++ b/resources/Js/Handlebars/lt.js @@ -0,0 +1,24 @@ +/** + * Handlebars Comparaison Helpers + * + * @source https://github.com/helpers/handlebars-helpers + * + * @package kzarshenas/crazyphp + * @author kekefreedog + * @copyright 2022-2023 Kévin Zarshenas + */ + +/** + * Lt + * + * Block helper that renders a block if `a` is **less than** `b`. + * If an inverse block is specified it will be rendered when falsy. + * You may optionally use the `compare=""` hash argument for the + * second value. + * + * @param a Value to compare + * @param b Value to compare with + * + * @return boolean + */ +module.exports = (a, b, options) => (a < b) ? options.fn(this) : options.inverse(this); \ No newline at end of file diff --git a/resources/Js/Handlebars/lte.js b/resources/Js/Handlebars/lte.js new file mode 100644 index 0000000..b9e246e --- /dev/null +++ b/resources/Js/Handlebars/lte.js @@ -0,0 +1,25 @@ +/** + * Handlebars Comparaison Helpers + * + * @source https://github.com/helpers/handlebars-helpers + * + * @package kzarshenas/crazyphp + * @author kekefreedog + * @copyright 2022-2023 Kévin Zarshenas + */ + +/** + * Lte + * + * Block helper that renders a block if `a` is **less than or + * equal to** `b`. + * If an inverse block is specified it will be rendered when falsy. + * You may optionally use the `compare=""` hash argument for the + * second value. + * + * @param a Value to compare + * @param b Value to compare with + * + * @return boolean + */ +module.exports = (a, b, options) => (a <= b) ? options.fn(this) : options.inverse(this); \ No newline at end of file diff --git a/resources/Js/Handlebars/replace.js b/resources/Js/Handlebars/replace.js new file mode 100644 index 0000000..a98c380 --- /dev/null +++ b/resources/Js/Handlebars/replace.js @@ -0,0 +1,20 @@ +/** + * Handlebars String Helpers + * + * @source https://github.com/helpers/handlebars-helpers + * + * @package kzarshenas/crazyphp + * @author kekefreedog + * @copyright 2022-2023 Kévin Zarshenas + */ + +/** + * Replace + * + * Replace all occurrences of substring `a` with substring `b`. + * + * @param a Object to stringify + * + * @return string + */ +module.exports = (str, a, b, options) => (typeof str === "string" && typeof a === "string" && typeof b === "string") ? str.split(a).join(b) : str; \ No newline at end of file diff --git a/src/Library/Array/Arrays.php b/src/Library/Array/Arrays.php index 2b841c8..25c2d50 100644 --- a/src/Library/Array/Arrays.php +++ b/src/Library/Array/Arrays.php @@ -694,7 +694,7 @@ public static function convertStringsToIntegers(array $array = []):array { else # Check if string - if (is_string($value) && ctype_digit($value)) + if ((is_string($value) && ctype_digit($value)) || (is_string($value) && preg_match('/^-\d+/', $value))) $array[$key] = (int) $value; # Return array diff --git a/src/Library/Template/Handlebars/Helpers.php b/src/Library/Template/Handlebars/Helpers.php index 68bfaad..d8e6867 100644 --- a/src/Library/Template/Handlebars/Helpers.php +++ b/src/Library/Template/Handlebars/Helpers.php @@ -690,4 +690,104 @@ public static function join(mixed $array, mixed $separator = ", "):string { } + /** + * Replace + * + * Replace all occurrences of substring `a` with substring `b`. + * + * @param mixed $str + * @param mixed $a + * @param mixed $b + * @return string + */ + public static function replace(mixed $str, mixed $a, mixed $b):string { + + # Check is is string + return is_string($str) && is_string($a) && is_string($b) + ? str_replace($a, $b, $str) + : $str + ; + + } + + /** + * Gt + * + * Block helper that renders a block if a is greater than b. + * If an inverse block is specified it will be rendered when falsy. You may optionally use the compare="" hash argument for the second value. + * + * @param a Value to compare + * @param b Value to compare with + * + * @return boolean + */ + public static function gt($a, $b, $option) { + + # Check arguments are equivalent + return $a > $b ? $option["fn"]() : $option["inverse"](); + + } + + /** + * Gte + * + * Block helper that renders a block if `a` is **greater than or + * equal to** `b`. + * If an inverse block is specified it will be rendered when falsy. + * You may optionally use the `compare=""` hash argument for the + * second value. + * + * @param a Value to compare + * @param b Value to compare with + * + * @return boolean + */ + public static function gte($a, $b, $option) { + + # Check arguments are equivalent + return $a >= $b ? $option["fn"]() : $option["inverse"](); + + } + + /** + * Lt + * + * Block helper that renders a block if `a` is **less than** `b`. + * If an inverse block is specified it will be rendered when falsy. + * You may optionally use the `compare=""` hash argument for the + * second value. + * + * @param a Value to compare + * @param b Value to compare with + * + * @return boolean + */ + public static function lt($a, $b, $option) { + + # Check arguments are equivalent + return $a < $b ? $option["fn"]() : $option["inverse"](); + + } + + /** + * Lte + * + * Block helper that renders a block if `a` is **less than or + * equal to** `b`. + * If an inverse block is specified it will be rendered when falsy. + * You may optionally use the `compare=""` hash argument for the + * second value. + * + * @param a Value to compare + * @param b Value to compare with + * + * @return boolean + */ + public static function lte($a, $b, $option) { + + # Check arguments are equivalent + return $a <= $b ? $option["fn"]() : $option["inverse"](); + + } + } \ No newline at end of file