Skip to content

Instantly share code, notes, and snippets.

View gsrai's full-sized avatar

Gagan Srai gsrai

View GitHub Profile
@gsrai
gsrai / timesImpl.js
Created September 10, 2017 14:42
Javascript implementation of times
function times(str, n) {
if (n == 0) {
return '';
}
if (n == 1) {
return str;
} else {
return str + times(str, n - 1);
}
@gsrai
gsrai / System Design.md
Created June 13, 2018 18:49 — forked from vasanthk/System Design.md
System Design Cheatsheet

System Design Cheatsheet

Picking the right architecture = Picking the right battles + Managing trade-offs

Basic Steps

  1. Clarify and agree on the scope of the system
  • User cases (description of sequences of events that, taken together, lead to a system doing something useful)
    • Who is going to use it?
    • How are they going to use it?
@gsrai
gsrai / AsyncAwait.js
Created June 21, 2018 06:13
Example of Async/Await vs Promises vs Promise.all
// Async Await is syntactic sugar over promises
// They are not a complete replacement as you still need to construct Promise objects
// and methods like Promise.all([]) can only be done with promises
const myFetch1 = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (Math.random() > 0.05) { // change this to trigger the catch blocks
resolve('foo')
} else {
@gsrai
gsrai / Exceptional.js
Created June 21, 2018 06:16
Creating, throwing and handling exceptions in JS
function InvalidRequestError(statusCode, message) {
const defaultMessage = 'invalid request, status code ' + statusCode;
this.name = 'InvalidRequestError';
this.message = message || defaultMessage;
this.stack = (new Error()).stack;
}
InvalidRequestError.prototype = Object.create(Error.prototype);
InvalidRequestError.prototype.constructor = InvalidRequestError;
@gsrai
gsrai / iPromise.js
Created June 21, 2018 06:17
Simple Promise Impl to help understand promises
// simplified promise implementation for learning purposes
// https://levelup.gitconnected.com/understand-javascript-promises-by-building-a-promise-from-scratch-84c0fd855720
class MyPromise {
constructor(promiseCallback) {
this.promiseChain = []
this.handleError = () => {}
// bind this as onResolve and onReject callbacks are passed into the promise callback
this.onResolve = this.onResolve.bind(this)
@gsrai
gsrai / lazy.js
Created June 21, 2018 07:27
Lazy evaluation in javascript, with a simple stream implementation because we can
// Lazy evaluation in javascript
// call by name: lazily evaluate the arguments passed into the function
// call by value: eagerly evaluate the arguments passed into the function
// call by need: memoized version of call by name
// memoization: avoid recomputation for the same inputs by caching the results
//
// Javascript by default uses call by value, however by using Object.defineProperty(),
// lazy evaluation in javascript object properties can be performed
//
// e.g. an infinite data structure - the stream, can only exist with lazy evaluation (else infinite recursion)
@gsrai
gsrai / react.html
Created July 20, 2018 16:52
Simplest React App all in one html file, using: React & Babel
<!DOCTYPE html>
<html lang="en">
<head>
<title>React::Inline</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://unpkg.com/purecss@1.0.0/build/pure-min.css" integrity="sha384-nn4HPE8lTHyVtfCBi5yW9d20FjT8BJwUXyWZT9InLYax14RDjBj46LmSztkmNP9w" crossorigin="anonymous">
</head>
<body>
<div id="app"></div>
@gsrai
gsrai / RequestNode.js
Created July 20, 2018 16:54
Using Raw Node JS to send data to a http server
const http = require('http')
const HOSTNAME = 'localhost'
const PORT = 3000
const client = () => {
const bodyString = JSON.stringify({
username: 'foobar',
password: 'Password1'
})
// HigherOrderComponentExample
import React, { Component } from 'react'
// https://gist.github.com/sebmarkbage/ef0bf1f338a7182b6775
// will augment component to include a loading wheel
// curried Higher order function
const LoaderHOC = (propName) => (WrappedComponent) => {
return class LoaderHOC extends Component {
isEmpty(prop) {
return (
@gsrai
gsrai / index.js
Created January 9, 2019 12:34
Migrating to Container and Presentational Components in react using redux
// example inspired by https://gist.github.com/chantastic/fc9e3853464dffdb1e3c
/* ------------- V0 ------------
* Here you have complex component that is hard to test
* and difficult to understand, extend and maintain
*/
import React from 'react'
class CommentList extends React.Component {
constructor() {