Skip to content

Instantly share code, notes, and snippets.

Avatar
👨‍💻
Working from home

Mitesh Patel miteshmap

👨‍💻
Working from home
View GitHub Profile
@miteshmap
miteshmap / test.php
Last active Feb 23, 2022
Check the plugin is exist or not in layout builder
View test.php
<?php
$display = \Drupal::entityTypeManager()
->getStorage('entity_view_display')
->load($param->getEntityTypeId() . '.' . $param->bundle() . '.default');
if ($display && $display->isLayoutBuilderEnabled()) {
foreach($display->getSections() as $section) {
$result = (bool) array_filter(
$section->getComponents(),
function (SectionComponent $component) use ($plugin_id) {
@miteshmap
miteshmap / blob-download.js
Last active Jan 8, 2021
Download blob data with axios
View blob-download.js
import axios from "axios";
import { saveAs } from "file-saver";
axios(`${apiUrlPrefix}/file/5`, {
method: 'GET',
responseType: 'blob',
headers: {
'Authorization': `Bearer ${token}`,
}
}).then(response => {
View FrogBuilder.js
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 / gist:7666a0ddebf40f9fff773a61bf9ba92b
Created Mar 23, 2020
Measuring used JS heap size in chrome
View gist:7666a0ddebf40f9fff773a61bf9ba92b
console.log(performance.memory.usedJSHeapSize)
@miteshmap
miteshmap / CacheSearch.js
Last active Jun 10, 2020
React Fetcher with cache for axios request, based on a talk of Dan Abramov: https://youtu.be/nLF0n9SACd4?t=877
View CacheSearch.js
/**
* Cache class for search.
*
* Avoid unnecessary api request by caching the results.
*/
class CacheSearch {
constructor() {
this.query = '';
this.queryCount = 0;
this.cache = {};
@miteshmap
miteshmap / Promise-fetcher.js
Last active Feb 28, 2020
helper method for promoise fetch. which expects promise as an argument.
View Promise-fetcher.js
// 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!'};
}
View savings_func.js
/**
* 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
View change-month-dropdown.jquery.js
// 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 / memory-usage.php
Created Feb 6, 2019
Check memory used by a script.
View memory-usage.php
<?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;
@miteshmap
miteshmap / time-calculate.php
Last active Jan 30, 2020
calculate time to execute a script.
View time-calculate.php
<?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;