Skip to content

Instantly share code, notes, and snippets.

View sagiavinash's full-sized avatar

Sagi Avinash Varma sagiavinash

View GitHub Profile
@sagiavinash
sagiavinash / ReactRouterV4FlowTypes.js
Created September 10, 2018 12:52
Flow Types for React Router V4
/*
* Types for React Router V4
* Example Usage:
* type PropsT = {
* myProp: string,
* } & ContextRouterT<{
* routeParam: string,
* anotherRouteParam: ?string,
* }>
*/
@sagiavinash
sagiavinash / gitaliases
Last active December 22, 2021 19:14
My Git Aliases
git config --global alias.ci 'commit';
git config --global alias.st 'status';
git config --global alias.br 'branch';
git config --global alias.unstage 'reset HEAD --';
git config --global alias.hist 'log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short';
git config --global alias.type 'cat-file -t';
git config --global alias.dump 'cat-file -p';
git config --global alias.tree 'log --all --graph --decorate --oneline --simplify-by-decoration';
git config --global alias.als 'config --get-regexp alias';
git config --global alias.clog 'log --pretty=oneline --abbrev-commit';
@sagiavinash
sagiavinash / throttle.js
Created April 28, 2015 17:15
Throttle Snippet - Plain JS
function throttle(fn, threshhold, scope) {
threshhold || (threshhold = 250);
var last,
deferTimer;
return function () {
var context = scope || this;
var now = +new Date,
args = arguments;
if (last && now < last + threshhold) {
@sagiavinash
sagiavinash / debounce-snippet.js
Created April 28, 2015 17:16
Debounce Function Snippet : Plain JS
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
@sagiavinash
sagiavinash / efficientTypeConversion.js
Created April 28, 2015 17:11
Efficient Type Conversion
/* General Statement: To convert values to a particular datatype use type coercsion instead of explicit conversion. */
/* To Number */
var val = "123";
// 1. using Exclusing Type conversion.
console.log(Number(val));
// 2. using Type Coercion (unary plus operator)
console.log(+val);
// among these methods the Type Coercion is fast. (http://jsperf.com/number-vs-unary-plus)
@sagiavinash
sagiavinash / createCustomErrorClass.js
Last active March 19, 2020 17:17
Factory function for creating custom Error classes
/* defintion */
const createErrorClass = (name, customConstructor = () => {}) => {
const ctx = {
[name]: class extends Error {
constructor(...args) {
super(name);
customConstructor(this, ...args);
Error.captureStackTrace(this, ctx[name]);
}
}
@sagiavinash
sagiavinash / machine.js
Created March 11, 2020 20:52
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@sagiavinash
sagiavinash / SimpleTextInput_CWRP.js
Created September 12, 2018 13:31
SimpleTextInput with CWRP
class SimpleTextInput extend Component {
state = {
value: this.props.value,
};
componentWillReceiveProps(nextProps) {
if (this.props.value !== nextProps.value) {
this.setState({value: nextProps.value});
}
}
render() {
@sagiavinash
sagiavinash / makeEnzymeCollectionsLodashCompatible.js
Last active September 4, 2018 13:20
make enzyme collections lodash compatible
_.mixin({
enzyme: reactWrapper => (
new Proxy(reactWrapper, {
get: (wrapper, prop) => {
const isSymbol = typeof prop === 'symbol';
const isNumber = !isSymbol && !isNaN(Number(prop));
return isNumber ? wrapper.at(prop) : wrapper[prop];
},
})
// AppContainer.js
import {withRefreshedStore} from 'react-store-manager';
const HomeRoute = Loadable({
loader: withRefreshedStore(import('./HomePageContainer')),
loading: () => <div>Loading...</div>
});
const ProductListRoute = Loadable({
loader: withRefreshedStore(import('./ProductListPageContainer')),