Skip to content

Commit

Permalink
Add helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Jul 30, 2024
1 parent 7e02f58 commit 47e2e0c
Show file tree
Hide file tree
Showing 5 changed files with 327 additions and 1 deletion.
57 changes: 57 additions & 0 deletions resources/Js/Handlebars/date_status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Handlebars Array Helpers
*
* @source https://github.com/helpers/handlebars-helpers
*
* @package kzarshenas/crazyphp
* @author kekefreedog <[email protected]>
* @copyright 2022-2024 Kévin Zarshenas
*/

/**
* Multiply
*
* Multiply a with b
*
* @param a value
* @param b value
* @param Object options
*
* @return number
*/
module.exports = (date, options) => {

// Check date given
if (typeof date !== 'string' || isNaN(Date.parse(date)))

// Return the input if it is not a valid date string
return date;

// Date instance
const givenDate = new Date(date);

// Today instance
const today = new Date();

// Set time to 00:00:00 to only compare dates
today.setHours(0, 0, 0, 0);

// In past
if(givenDate < today)

//Date is in the past
return -1;

else
// Future
if(givenDate > today)

// Date is in the future
return 1;

else

// Date is today
return 0;

}
35 changes: 35 additions & 0 deletions resources/Js/Handlebars/divide.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Handlebars Array Helpers
*
* @source https://github.com/helpers/handlebars-helpers
*
* @package kzarshenas/crazyphp
* @author kekefreedog <[email protected]>
* @copyright 2022-2024 Kévin Zarshenas
*/

/**
* Divide
*
* Divide a by b
*
* @param a value
* @param b value
* @param Object options
*
* @return number
*/
module.exports = (a, b, options) => {

// Check if array or string
if((!isNaN(parseFloat(a)) && isFinite(a)) && (!isNaN(parseFloat(b)) && isFinite(b)) && Number(b) != 0)

// Return length
return Number($a) / Number($b);

else

// Return string
return `${a}/${b}`;

}
35 changes: 35 additions & 0 deletions resources/Js/Handlebars/multiply.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Handlebars Array Helpers
*
* @source https://github.com/helpers/handlebars-helpers
*
* @package kzarshenas/crazyphp
* @author kekefreedog <[email protected]>
* @copyright 2022-2024 Kévin Zarshenas
*/

/**
* Multiply
*
* Multiply a with b
*
* @param a value
* @param b value
* @param Object options
*
* @return number
*/
module.exports = (a, b, options) => {

// Check if array or string
if((!isNaN(parseFloat(a)) && isFinite(a)) && (!isNaN(parseFloat(b)) && isFinite(b)))

// Return length
return Number($a) * Number($b);

else

// Return string
return `${a}*${b}`;

}
99 changes: 99 additions & 0 deletions src/Front/Library/Utility/DateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,104 @@ export default class DateTime {
return `${year}${separator}${month}${separator}${day}`;

}

/**
* Get the first date of a week based on an offset from the current week.
*
* @param weekOffset - The week offset from the current week (0 for this week, -1 for previous week, 1 for next week).
* @param format - Optional format for the returned date. Default is 'YYYY-MM-DD'.
* @returns {string} The first date of the specified week in the given format.
*/
public static getFirstDateOfWeek = (weekOffset: number, format: string = 'YYYY-MM-DD'):string => {

// Get current date
const today = new Date();

// Get day number 0 (Sunday) to 6 (Saturday)
const currentDay = today.getDay();

// Days since the last Monday
const daysSinceMonday = (currentDay + 6) % 7;

// Calculate the date of the last Monday
const lastMonday = new Date(today);
lastMonday.setDate(today.getDate() - daysSinceMonday);

// Calculate the first date of the target week
const targetMonday = new Date(lastMonday);
targetMonday.setDate(lastMonday.getDate() + weekOffset * 7);

// Format the date
return DateTime.formatDate(targetMonday, format);

}

/**
* Is Date Current Week
*
* Check if a given date is in the current week.
*
* @param date - The date to check.
* @returns True if the date is in the current week, false otherwise.
*/
public static isDateInCurrentWeek = (date:Date|string):boolean => {

// check date
if(typeof date == "string")

// Set date
date = new Date(date);

const today = new Date();
const dayOfWeek = today.getDay(); // 0 (Sunday) to 6 (Saturday)
const startOfWeek = new Date(today);
const endOfWeek = new Date(today);

// Calculate the start of the current week (Monday)
startOfWeek.setDate(today.getDate() - (dayOfWeek === 0 ? 6 : dayOfWeek - 1));

// Calculate the end of the current week (Sunday)
endOfWeek.setDate(startOfWeek.getDate() + 6);

// Set hours to 0 to compare only date parts
startOfWeek.setHours(0, 0, 0, 0);
endOfWeek.setHours(23, 59, 59, 999);

// Compare the given date with the start and end of the current week
return date >= startOfWeek && date <= endOfWeek;

}

/**
* Format a Date object into a string based on the given format.
*
* @param date - The Date object to format.
* @param format - The format string.
* @returns {string} The formatted date string.
*/
public static formatDate = (date: Date, format: string):string => {

// Get full year
const year = date.getFullYear();

// Get month
const month = ('0' + (date.getMonth() + 1)).slice(-2);

// Get day
const day = ('0' + date.getDate()).slice(-2);

// Swtich
switch (format) {
case 'YYYY-MM-DD':
return `${year}-${month}-${day}`;
case 'MM/DD/YY':
return `${month}/${day}/${year.toString().slice(-2)}`;
case 'DD/MM/YYYY':
return `${day}/${month}/${year}`;
default:
throw new Error('Unsupported date format');
}

}

}
102 changes: 101 additions & 1 deletion src/Library/Template/Handlebars/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

use CrazyPHP\Exception\CrazyException;
use CrazyPHP\Library\Time\DateTime;
use CrazyPHP\Library\Form\Process;
use ReflectionMethod;
use ReflectionClass;
Expand Down Expand Up @@ -463,7 +464,13 @@ public static function colorThemeSuffix($a, $v, $theme) {
public static function is($a, $b, $option) {

# Check arguments are equivalent
return $a == $b ? $option["fn"]() : $option["inverse"]();
return (
(!$a && !$b) ||
($a == $b)
)
? $option["fn"]()
: $option["inverse"]()
;

}

Expand Down Expand Up @@ -920,4 +927,97 @@ public static function isLast($index, $list, $option) {

}

/**
* Divide
*
* Divide a by b
*
* @param mixed $a
* @param mixed $b
* @param mixed options
*/
public static function divide($a, $b, $option) {

# Check if array
if(is_numeric($a) && is_numeric($b) && intval($b) != 0)

# Return length
return intval($a) / intval($b);

else

# Return string
return "$a/$b";

}

/**
* Multiply
*
* Multiply a with b
*
* @param mixed $a
* @param mixed $b
* @param mixed options
*/
public static function multiply($a, $b, $option) {

# Check if array
if(is_numeric($a) && is_numeric($b))

# Return length
return intval($a) * intval($b);

else

# Return string
return "$a*$b";

}

/**
* Date Status
*
* Multiply a with b
*
* @param mixed $a
* @param mixed $b
* @param mixed options
*/
public static function date_status($date, $option) {

# Check input
if(!is_string($date) || !strtotime($date))

# Return the input if it is not a valid date string
return $date;

# Date instance
$givenDate = new DateTime($date);

# Today instance
$today = new DateTime();

# Set time to 00:00:00 to only compare dates
$today->setTime(0, 0, 0);

# Check past
if($givenDate < $today)

# Date is in the past
return -1;

# Furure
elseif($givenDate > $today)

# Date is in the future
return 1;

else

# Date is today
return 0;

}

}

0 comments on commit 47e2e0c

Please sign in to comment.