Skip to content

Instantly share code, notes, and snippets.

View themgoncalves's full-sized avatar
⚜️
Software and cathedrals are much the same – first we build them, then we pray.

Marcos Gonçalves themgoncalves

⚜️
Software and cathedrals are much the same – first we build them, then we pray.
View GitHub Profile
@themgoncalves
themgoncalves / es6-merge-refs.js
Created August 17, 2020 12:44
Working with multiple references on React - ES6
import React from 'react';
const mergeRefs = (...refs) => (value) => {
for (let i = 0; i < refs.length; i += 1) {
const ref = refs[i];
if (typeof ref === 'function') {
ref(value);
} else if (ref) {
ref.current = value;
@themgoncalves
themgoncalves / type-of.ts
Created August 17, 2020 15:41
Making JavaScript typeof() work - typescript
const typeOf = (value: unknown | unknown[]): string =>
({}.toString
.call(value)
.match(/\s([a-zA-Z]+)/)[1]
.toLowerCase());
export default typeOf;
@themgoncalves
themgoncalves / type-of.js
Created August 17, 2020 15:41
Making JavaScript typeof() work - Es6
const typeOf = (value) =>
({}.toString
.call(value)
.match(/\s([a-zA-Z]+)/)[1]
.toLowerCase());
export default typeOf;
@themgoncalves
themgoncalves / type-of-demo.ts
Last active September 3, 2021 02:16
Making JavaScript typeof() work - typescript demo
import typeOf from './type-of';
const shallowCompare: (source: Record<string, unknown | unknown[]>, target: Record<string, unknown | unknown[]>): boolean => {
if (typeOf(source) !== typeOf(target)) {
return false;
}
return Object.keys(source).every((key) => {
if (typeOf(source[key]) !== typeOf(target[key])) {
return false;
@themgoncalves
themgoncalves / adventure-pattern-interactions.md
Last active August 20, 2020 10:47
Adventure Pattern interactions table
can use components enhancers screen state styles utils
components 🚫 🚫 🚫
enhancers 🚫 🚫 🚫 🚫
screen 🚫 🚫 🚫 🚫 🚫
state 🚫 🚫 🚫
styles 🚫 🚫
utils
@themgoncalves
themgoncalves / async-operation.js
Created May 26, 2021 15:28
JavaScript and Asynchronous Operations: Do you really do it right?
/** BAD, BLOCKS THE EXECUTION */
async function getOptions() {
// request "toppings" and wait for the response
// only then goes to the next line
const toppings = await getToppings();
const condiments = await getCondiments();
const buns = await getBuns();
return { toppings, condiments, buns };
const species = ['octopus', 'squid', 'shark', 'seahorse', 'starfish', 'whale',];
// TRADITIONAL WAY
const firstItem = species[0];
const lastItem = species[species.length - 1];
// NEW WAY WITH SMART ACCESS
const { 0: firstItem, [species.length / 2]: middleItem, [species.length - 1]: lastItem } = species;
console.log(firstItem, middleItem, lastItem);
@themgoncalves
themgoncalves / shallow-compare.ts
Last active May 29, 2021 10:41
Shallow comparison - validating if two elements are shallow equal
import typeOf from './type-of';
const shallowCompare = (source: unknown | unknown[], target: unknown | unknown[]): boolean => {
if (typeOf(source) !== typeOf(target)) {
return false;
}
if (typeOf(source) === 'array') {
return (source as unknown[]).length === (target as unknown[]).length;
} else if (typeOf(source) === 'object') {
import typeOf from './type-of';
const shallowCompare = (source, target) => {
if (typeOf(source) !== typeOf(target)) {
return false;
}
if (typeOf(source) === 'array') {
return source.length === target.length;
} else if (typeOf(source) === 'object') {
const deepCompare = (source: unknown | unknown[], target: unknown | unknown[]): boolean => {
if (typeOf(source) !== typeOf(target)) {
return false;
}
if (typeOf(source) === 'array') {
if ((source as unknown[]).length !== (target as unknown[]).length) {
return false;
}