Skip to content

Instantly share code, notes, and snippets.

View neharkarvishal's full-sized avatar

Vishal Neharkar neharkarvishal

View GitHub Profile
/**
* fibonacciGenerator.js
* tags: { JavaScript, Generator, Iterator }
*/
function* fibonacciGenerator() {
var [prev, curr] = [0, 1]
while(true) {
[prev, curr] = [curr, prev + curr]
yield curr
/**
* Workaround to set up a handshake, lazy-loading by resolving a promise
*/
let settleReadyState = {}
let subprocess = {}
// following code outputs: Promise {<pending>} on console and sets
// settleReadyState = { resolve: ƒ, reject: ƒ}
// subprocess = {ready: Promise<pending>}
function fizzBuzz(n: number) {
return (
{ true: '', false: 'Fizz' }[`${n % 3 === 0}`] +
{ true: '', false: 'Buzz' }[`${n % 5 === 0}`] ||
n.toString()
)
}
function fizzBuzzFunctional(n: number) {
let test =
@neharkarvishal
neharkarvishal / sha-256.js
Created August 11, 2022 06:22
Calculate SHA-256 hash (Node.js)
/**
* Creates a hash for a value using the SHA-256 algorithm. Returns a promise.
*
* Use crypto.createHash() to create a Hash object with the appropriate
* algorithm.
*
* Use hash.update() to add the data from val to the Hash, hash.digest() to
* calculate the digest of the data.
*
* Use setTimeout() to prevent blocking on a long operation. Return a Promise to
@neharkarvishal
neharkarvishal / media.js
Created August 27, 2022 11:37
Media Queries in CSS, JavaScript and with RxJS
/**
* The vanilla javascript way of implementing such a functionality would be to use
* he window's matchMedia function. The function takes a string query and
* returns a MediaQueryList that can be used to get the current result of the
* query and listen to changes of the media query.
*/
const mediaQueryList = window.matchMedia(`(min-width: 767px)`);
console.log(mediaQueryList.matches); // true or false
/**
* Generate html string suitable for rendering in PDF
*/
export const generateReportPdfHTML = async (options: GenerateReportPdfOptions): Promise<string> => {
const pages = CustomReportTemplate({ ...options, ssr: true });
const pagesContent = _.map(pages, ({ name, content }) => (
<Flex key={`box-${name}`} flexDir="column" h="100vh" style={{ pageBreakBefore: 'always' }}>
{content}
</Flex>
import { Extension } from '@tiptap/core';
import { ComponentType } from 'react';
import { ContextMenuPlugin, ContextMenuProps } from './ContextMenuPlugin';
interface ContextMenuOptions {
component: ComponentType<ContextMenuProps>;
}
export const ContextMenu = Extension.create<ContextMenuOptions>({
name: 'contextMenu',
/*------------------------------------------
Responsive Grid Media Queries - 1280, 1024, 768, 480
1280-1024 - desktop (default grid)
1024-768 - tablet landscape
768-480 - tablet
480-less - phone landscape & smaller
--------------------------------------------*/
@media all and (min-width: 1024px) and (max-width: 1280px) { }
@media all and (min-width: 768px) and (max-width: 1024px) { }
@neharkarvishal
neharkarvishal / withTimeout.js
Created January 7, 2023 09:05
Reject promise after timeout
/**
* Reject promise after timeout
*
* @todo this is slow, we should find a way to do this in a faster way
* @param {Promise<any>} callback
* @param {number} ms
* @return {Promise<never>}
*/
function withTimeout(callback, ms) {
let timeout;
@neharkarvishal
neharkarvishal / generateCacheKey.js
Created January 7, 2023 09:08
Generate Cache Key
'use strict';
const { generateHeadersKey } = require('./generateHeadersKey');
const { generateQueryParamsKey } = require('./generateQueryParamsKey');
function generateCacheKey(
ctx,
keys = {
useQueryParams: false, // @todo: array or boolean => can be optimized
useHeaders: [],