Skip to content

Instantly share code, notes, and snippets.

@abinavseelan
abinavseelan / json-type.ts
Last active October 31, 2020 20:15
Typescript type for JSON data
type PrimitiveValue = string | null | number | boolean;
interface JSONValue {
[k: string]: PrimitiveValue | JSONValue | Array<PrimitiveValue> | Array<JSONValue>;
}
type JSONData = PrimitiveValue | JSONValue | Array<PrimitiveValue> | Array<JSONValue>;
interface Schema {
posts: Array<{
id: number;
title: string;
views: number;
}>;
user: {
name: string;
};
}
@abinavseelan
abinavseelan / ErrorBoundary.tsx
Last active October 30, 2020 20:44
ErrorBoundary
import React from 'react';
class ErrorBoundary extends React.Component {
constructor() {
super();
this.state = {
error: null,
errorInfo: null,
};
@abinavseelan
abinavseelan / machine.js
Created February 9, 2020 03:27
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@abinavseelan
abinavseelan / machine.js
Last active January 28, 2020 04:35
Generated by XState Viz: https://xstate.js.org/viz
const hasAARequest = (context) => {
return context.AARequest;
}
const dropAARequest = assign({
AARequest: () => null,
});
const REQUEST_SUBMISSION = {
id: 'RequestSubmission',
@abinavseelan
abinavseelan / gist:706865be42a9d907f918a142f929ed24
Last active October 17, 2018 13:29
Different gitconfigs for different folders
/*
If you have seperate github accounts for personal use and work, but use the same laptop.
Taken from this StackOverflow answer: https://stackoverflow.com/a/43884702
*/
/*
Have two folders, ~/company is going to house all your company repos and
~/personal is going to house all your personal projects
*/
@abinavseelan
abinavseelan / stack.js
Created May 2, 2018 15:58
Build an implementation of a stack in JS. The smallest value in the stack should be retrieveable in O(1)
function Stack() {
this.min = Infinity;
this.values = [];
}
Stack.prototype.push = function(value) {
this.values[this.values.length] = value; // Using this logic since .push() was banned from use.
if (value < this.min) {
this.min = value;
@abinavseelan
abinavseelan / index.js
Created May 2, 2018 15:40
Class with chaining methods example
function Menu() {
this.items = [];
}
Menu.prototype.addItem = function(item) {
this.items.push(item);
return this; // This allows for chaining
}
Menu.prototype.showMenu = function() {
@abinavseelan
abinavseelan / index.js
Created March 26, 2018 12:48
Implementing .bind with .apply
/*
Question: Assume .bind does not exist.
Implement the .bind functionality using .apply()
*/
Function.prototype.bind = function(thisArg) {
// Get a reference to the function that you want to bind
let functionToBind = this;
@abinavseelan
abinavseelan / index.html
Created March 16, 2018 06:25
(3) Page Visibility API
...
<body>
<script type="text/javascript">
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
document.title = '😴';
}
if (document.visibilityState === 'visible') {
document.title = '😁';