Skip to content

Instantly share code, notes, and snippets.

View jasdeepkhalsa's full-sized avatar

Jasdeep Khalsa jasdeepkhalsa

View GitHub Profile
@jasdeepkhalsa
jasdeepkhalsa / waitForWindowCypress.js
Last active December 22, 2020 17:00
Cypress waitForWindow recursive promise function
export default function waitForWindow(property, timeout = 10000, interval = 500, attempts = 0) {
if (!property) {
return Promise.reject(new Error('No window property added to the function'))
}
return cy.window()
.then(
(win) => {
if (win[property] !== undefined) {
return true
}
@jasdeepkhalsa
jasdeepkhalsa / querystrings.js
Created January 12, 2020 21:06
Read URL Query Strings for Data Prepopulation
// Data pre-population
var queryStrings = decodeURIComponent(location.search).split(/[?&]/).reduce(function(total, qs) {
total[qs.split('=')[0]] = qs.split('=')[1];
return total;
}, {});
if (queryStrings) {
var inviteeEmailValue = queryStrings['Email'];
var inviteeFullNameValue = queryStrings['Name'];
var inviteeMobileValue = queryStrings['mobile'];
@jasdeepkhalsa
jasdeepkhalsa / RecordType.ts
Last active October 23, 2019 13:15
TypeScript Record Advanced Type for representing an object of key, value data
/**
* Construct a type with a set of properties K of type T
*/
type Record<K extends keyof any, T> = {
[P in K]: T;
};
// Usage
type Names = {
name: string
@jasdeepkhalsa
jasdeepkhalsa / react-hooks.md
Created October 13, 2019 21:28
React Hooks Transition Guide

setState -> const [state, setState] = useState()

componentDidMount -> useEffect(() => {}, [])

componentDidMount & componentDidUpdate -> useEffect(() => {}, [props.changedValue]

componentWillUnmount & componentDidUnmount -> useEffect(() => { return () => console.log('Cleaning up') }, [])

shouldComponentUpdate -> export default React.memo(myComponent, () => console.log('finer grain control here, but I work opposite to shouldComponentUpdate. Usually out of the box memoization is good enough')

import Html exposing (..)
-- MODEL
type alias Model = { ... }
-- UPDATE
@jasdeepkhalsa
jasdeepkhalsa / jenkinsfile-stage.groovy
Last active July 10, 2019 10:15
Adding a stage into a Jenkins file
stage('Lint') {
steps {
sh 'npm run lint'
}
}
@jasdeepkhalsa
jasdeepkhalsa / complex-git-rebase-strategies.md
Last active May 15, 2019 12:50
Complex git rebase strategies (using --rebase-merges)

Firstly make sure you're using a decent editor with your git e.g. Atom, VS Code etc.

git config --global core.editor "atom"

To modify a merge commit history, like the merges done by popular online version control systems such as GitLab or Bitbucket:

¢ git rebase --rebase-merges -i HEAD~1
@jasdeepkhalsa
jasdeepkhalsa / triggerEventIE8+.js
Created February 20, 2019 17:39
triggerEvent dispatch fire trigger events cross browser IE8 compatible
function triggerEvent(elem, eventType) {
const noop = () => {}
if ('createEvent' in document) {
// modern browsers, IE9+
const evt = document.createEvent('HTMLEvents') || new Event(eventType);
evt.initEvent ? evt.initEvent(eventType, true, false) : noop();
elem.dispatchEvent(evt);
return;
}
@jasdeepkhalsa
jasdeepkhalsa / video-to-gif.sh
Created January 9, 2019 16:44
Video to GIF with FFMPEG
ffmpeg -i video.mp4 -filter_complex "[0:v] fps=24,scale=1080:-1,split [a][b];[a] palettegen [p];[b][p] paletteuse" video.gif
## REFERENCES
# https://engineering.giphy.com/how-to-make-gifs-with-ffmpeg/
# https://ffmpeg.org/ffmpeg-filters.html#scale-1
@jasdeepkhalsa
jasdeepkhalsa / numberToWords.js
Last active December 28, 2018 11:18
Number to Words Convertor
import { toWords } from 'number-to-words';
export function numberToWords(number) {
if (!number || number === true) {
return number;
}
const numWithNoNonDigits = number.replace ? number.replace(/\D/g,'') : number;
const numToArray = numWithNoNonDigits.toString().split('');