Skip to content

Instantly share code, notes, and snippets.

View miteshmap's full-sized avatar
👨‍💻
Working from home

Mitesh Patel miteshmap

👨‍💻
Working from home
View GitHub Profile
@miteshmap
miteshmap / OriginalServiceOverride.php
Last active February 19, 2020 03:15
Drupal 8: Service Decorators: OriginalServiceOverride.php
<?php
namespace Drupal\custom_decorator_override;
use Drupal\custom_decorator_base\OriginalService;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Class OriginalServiceOverride
*/
class OriginalServiceOverride extends OriginalService {
/**
* Original service object.
@miteshmap
miteshmap / time-calculate.php
Last active January 30, 2020 05:56
calculate time to execute a script.
<?php
//Create a variable for start time
$time_start = microtime(true);
//Create a variable for end time
$time_end = microtime(true);
// total execution time.
$execution_time = ($time_end - $time_start) / 60;
@miteshmap
miteshmap / memory-usage.php
Created February 6, 2019 17:38
Check memory used by a script.
<?php
// Add these lines at the end of your code.
echo PHP_EOL;
echo "Memory Usage :" . memory_get_usage();
echo PHP_EOL;
echo "Real Memory Usage :" . memory_get_usage(true);
echo PHP_EOL;
echo "Real Peak Memory Usage :" . memory_get_peak_usage(true);
echo PHP_EOL;
// With Jquery.
$('.year-select').once('current-year').on('change', function () {
var month = $('.month-select');
var currentYear = (new Date()).getFullYear().toString();
var selectedYear = jQuery('.year-select option:selected').val();
if (currentYear === selectedYear) {
var currentMonth = (new Date()).getMonth();
@miteshmap
miteshmap / savings_func.js
Last active January 2, 2020 16:37
small savings
/**
* The script is created purely based the video I saw on youtube about
* Personal finance on how to save money with small savings.
*
* If you invest 2 INR a day,In a year you can save around 133590 INR.
*
* The calculation goes like if on the 1st of january you save 2 rupees
* 2nd january you have to save 2*2 = 4 and 3 day 2*3 = 6 and likewise.
*
* You can not ideally transac this small amount to the bank, so I created
@miteshmap
miteshmap / Promise-fetcher.js
Last active February 28, 2020 07:38
helper method for promoise fetch. which expects promise as an argument.
// Helper method to handle error and expact only error or result.
export const createFetcher = promiseFunc => {
return {
read: arg => {
try {
return promiseFunc(arg)
.then(response => {
if (!response) {
return {error: 'error!'};
}
@miteshmap
miteshmap / CacheSearch.js
Last active June 10, 2020 15:32
React Fetcher with cache for axios request, based on a talk of Dan Abramov: https://youtu.be/nLF0n9SACd4?t=877
/**
* Cache class for search.
*
* Avoid unnecessary api request by caching the results.
*/
class CacheSearch {
constructor() {
this.query = '';
this.queryCount = 0;
this.cache = {};
@miteshmap
miteshmap / gist:7666a0ddebf40f9fff773a61bf9ba92b
Created March 23, 2020 03:55
Measuring used JS heap size in chrome
console.log(performance.memory.usedJSHeapSize)
class FrogBuilder {
constructor(name, gender) {
// Ensure that the first character is always capitalized
this.name = name.charAt(0).toUpperCase() + name.slice(1)
this.gender = gender
}
formatEyesCorrectly(eyes) {
return Array.isArray(eyes) ? { left: eye[0], right: eye[1] } : eyes
}
setEyes(eyes) {
@miteshmap
miteshmap / blob-download.js
Last active January 8, 2021 08:33
Download blob data with axios
import axios from "axios";
import { saveAs } from "file-saver";
axios(`${apiUrlPrefix}/file/5`, {
method: 'GET',
responseType: 'blob',
headers: {
'Authorization': `Bearer ${token}`,
}
}).then(response => {