Skip to content

Instantly share code, notes, and snippets.

View rabelloo's full-sized avatar

Andre Rabello rabelloo

  • Primer
  • Lisbon, Portugal
View GitHub Profile
export {};
/*
Taken from https://gist.github.com/sechel/e6aff22d9e56df02c5bd09c4afc516e6
which is the basis for https://github.com/JSmith01/broadcastchannel-polyfill
but rewritten into TypeScript and class rather than JS with .prototype extension
*/
const channels: Record<string, Set<BroadcastChannel>> = {};
import { useEffect } from 'react';
/**
* @example
* useKeyDown('escape', cancel);
*/
export function useKeyDown(keys: string, onKeyDown: Listener): void;
/**
* @example
* useKeyDown({ 'cmd + z': undo, 'cmd + z': redo });
function getUserName(user: { name?: string; fullName: string }) {
return user.name || user.fullName.split(' ')[0];
}
describe('getUserName', () => {
it('should return name if it exists', () => {
const name = 'name';
const result = getUserName({ name } as any);
// TypeScript would error ^^^^^^^^
// if we didn't cast it
{
"include": ["src/**/*"],
"exclude": ["src/**/*.spec.*"],
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
"module": "esnext",
"moduleResolution": "node",
"target": "es2019",
import React from 'react';
import type { Story, Meta } from '../storybook';
import { Button, ButtonProps } from './button';
export default {
title: 'React/Button',
component: Button,
argTypes: {
children: { name: 'label', control: 'text' },
},
@rabelloo
rabelloo / hmr.ts
Created June 10, 2019 08:10
Angular HMR
import { ApplicationRef, NgModuleRef } from '@angular/core';
import { createNewHosts } from '@angularclass/hmr';
import { ActionReducer } from '@ngrx/store';
export function hmrMetaReducer(reducer: ActionReducer<any>) {
return (state: any, action: any) =>
action.type === 'SET_ROOT_STATE'
? action.payload
: reducer(state, action);
}
@rabelloo
rabelloo / heap.js
Last active May 19, 2019 20:40
Heap
function Heap(compare = (target, parent) => target > parent) {
this.array = [];
this.compare = compare;
}
Object.defineProperty(Heap.prototype, 'size', { get: function () { return this.array.length; } })
Heap.prototype.peek = function () { return this.array[0]; }
Heap.prototype.pop = function () {
@rabelloo
rabelloo / Expect.cs
Last active July 17, 2020 11:41
C# Expect inspired by Jasmine
public class Expect
{
private object expectation;
protected bool not;
public Expect(object expectation)
{
this.expectation = expectation;
}