Skip to content

Commit

Permalink
Add isLast helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Apr 14, 2024
1 parent db6844e commit 284885d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
38 changes: 38 additions & 0 deletions resources/Js/Handlebars/isLast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Handlebars Comparaison Helpers
*
* @source https://github.com/helpers/handlebars-helpers
*
* @package kzarshenas/crazyphp
* @author kekefreedog <[email protected]>
* @copyright 2022-2024 Kévin Zarshenas
*/

/**
* Is
*
* Block helper that renders a block if a is equal to b.
* If an inverse block is specified it will be rendered when falsy.
* Similar to eq but does not do strict equality.
*
* @param a Value to compare
* @param v Value to compare with
*
* @return boolean
*/
module.exports = (index, list, options) => (
typeof index === "number" &&
(
(
Array.isArray(list) &&
index === (list.length - 1)
) ||
(
typeof list === "object" &&
index === (Object.keys(list).length - 1)
)
)
)
? options.fn(this)
: options.inverse(this)
;
27 changes: 27 additions & 0 deletions src/Library/Template/Handlebars/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -856,4 +856,31 @@ public static function add($a, $b, $option) {

}

/**
* Is Last
*
* Block helper that renders a block if index gien is the last of the list given
*
* @param $index Value to compare
* @param $list Value to compare with
*
* @return boolean
*/
public static function isLast($index, $list, $option) {

# Set result
$result = (
is_numeric($index) &&
is_array($list) &&
$index === (count($list) - 1)
)
? $option['fn']()
: $option['inverse']()
;

# Return result
return $result;

}

}

0 comments on commit 284885d

Please sign in to comment.