Skip to content

Instantly share code, notes, and snippets.

View AnandPilania's full-sized avatar
🎖️
Working from home

Anand Pilania AnandPilania

🎖️
Working from home
View GitHub Profile
@AnandPilania
AnandPilania / blank.js
Last active April 2, 2024 11:16
Enhance Your Website Notifications with JavaScript Toasts
function blank(value) {
if(Array.isArray(value)) {
return value.length === 0;
}
if(value instanceof Date) {
return false;
}
if(typeof value === 'object' && value !== null) {
@AnandPilania
AnandPilania / README.md
Last active April 1, 2024 05:30
Enhanced jQuery Plugin for Observing Events

This gist provides a jQuery plugin for observing events, offering improved flexibility and functionality. The plugin supports various syntaxes, including object-based and chained function-based, and allows for observing both DOM and AJAX events. It also includes error handling and validation for input parameters.

<input id="input">
<select id="select">
  <option value="" selected></option>
  <option value="1">1</option>
@AnandPilania
AnandPilania / parseURL.js
Last active March 22, 2024 03:20
URL Parser - Easily extract protocol, host, path, query parameters, and more from any URL string (w/o URLSearchParams API).
/*
This JavaScript function parseURL takes a URL string as input and parses it into its various
components such as protocol, host, path, query parameters, and relative path. It utilizes
regular expressions to extract these components efficiently. The parsed components are then
encapsulated within an object for convenient access and manipulation in your code. This can
be useful in scenarios where you need to extract specific parts of a URL or analyze and modify
URLs dynamically within your web applications.
*/
function parseURL(url) {
@AnandPilania
AnandPilania / generateUUID.js
Last active March 21, 2024 13:43
UUID Generator
const generateUUID = () =>
"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
var r = Math.random() * 16 | 0;
return (c == "x" ? r : (r & 0x3 | 0x8)).toString(16);
});
$1 = generateUUID();
$2 = generateUUID();
@AnandPilania
AnandPilania / app\Listeners\LogActivity.php
Created February 21, 2024 13:18
Laravel Auth logging
<?php
namespace App\Listeners;
use App\Events;
use Illuminate\Auth\Events as LaravelEvents;
use Illuminate\Support\Facades\Log;
class LogActivity
{
public function login(LaravelEvents\Login $event)
@AnandPilania
AnandPilania / memoize.js
Last active February 21, 2024 01:31
Memoize a function
const memoize = (fn: any) =>
(
(cache = Object.create(null)) =>
(arg: any) =>
cache[arg] || (cache[arg] = fn(arg))
)();
// Calculate Fibonacci numbers
// const fibo = memoize((n: number) => (n <= 2 ? 1 : fibo(n - 1) + fibo(n - 2)));
// fibo(1); // 1
@AnandPilania
AnandPilania / README.md
Created February 8, 2024 09:38
Dynamic Conditional Checks in Laravel Eloquent Base Model

This Laravel Eloquent base model enhancement provides a dynamic method handling mechanism for conditional checks based on column values.

The __call magic method is utilized to interpret method calls ending with Is or IsNot. The associated column is extracted, and the method dynamically checks if the column's value matches or does not match specified values.

This allow to handle multiple parameters, enhancing its flexibility. This concise and expressive approach simplifies conditional checks, making the code more readable and adaptable in various scenarios.

// Assuming there's a 'status' column in the model

// Check if status is one of [1, 2, 3]
@AnandPilania
AnandPilania / README.md
Created December 8, 2023 11:07
Laravel: Pulse sample card - Inspire

Add the card to dashboard

<livewire:pulse-cards.inspire-card cols="3" />
@AnandPilania
AnandPilania / app\Concerns\ModelConcern.php
Created December 1, 2023 05:14
Laravel: Model common utility trait
<?php
namespace App\Concerns;
use DB;
use Illuminate\Database\Eloquent\Builder;
use Schema;
/**
* Trait ModelConcern
@AnandPilania
AnandPilania / app\Concerns\BelongsToTeam.php
Created December 1, 2023 05:04
Laravel: Simplify Access Controller with BelongsToTeam
<?php
namespace App\Concerns;
use App\Models\Team;
use Illuminate\Database\Eloquent\Builder;
trait BelongsToTeam
{
public static function boot()