Skip to content

Commit

Permalink
Add helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Mar 12, 2024
1 parent eb433f6 commit f601d64
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 1 deletion.
22 changes: 22 additions & 0 deletions resources/Js/Handlebars/gt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Handlebars Comparaison Helpers
*
* @source https://github.com/helpers/handlebars-helpers
*
* @package kzarshenas/crazyphp
* @author kekefreedog <[email protected]>
* @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);
25 changes: 25 additions & 0 deletions resources/Js/Handlebars/gte.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Handlebars Comparaison Helpers
*
* @source https://github.com/helpers/handlebars-helpers
*
* @package kzarshenas/crazyphp
* @author kekefreedog <[email protected]>
* @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);
24 changes: 24 additions & 0 deletions resources/Js/Handlebars/lt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Handlebars Comparaison Helpers
*
* @source https://github.com/helpers/handlebars-helpers
*
* @package kzarshenas/crazyphp
* @author kekefreedog <[email protected]>
* @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);
25 changes: 25 additions & 0 deletions resources/Js/Handlebars/lte.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Handlebars Comparaison Helpers
*
* @source https://github.com/helpers/handlebars-helpers
*
* @package kzarshenas/crazyphp
* @author kekefreedog <[email protected]>
* @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);
20 changes: 20 additions & 0 deletions resources/Js/Handlebars/replace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Handlebars String Helpers
*
* @source https://github.com/helpers/handlebars-helpers
*
* @package kzarshenas/crazyphp
* @author kekefreedog <[email protected]>
* @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;
2 changes: 1 addition & 1 deletion src/Library/Array/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
100 changes: 100 additions & 0 deletions src/Library/Template/Handlebars/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"]();

}

}

0 comments on commit f601d64

Please sign in to comment.