Skip to content

Instantly share code, notes, and snippets.

View ernestlv's full-sized avatar
Hello World!

Ernest Leyva ernestlv

Hello World!
View GitHub Profile
@ernestlv
ernestlv / intersection.js
Last active June 6, 2023 15:51
The intersection of two sets. It merges objects in common.
/* it is in both A & B */
/* merge common objects using expression: { ...map[b.key], ...b } */
function intersection(arrA, arrB) {
const mapA = arrA.reduce((res, a) => ({ ...res, [a.key]:a }), {});
return arrB.reduce((res, b) => (b.key in mapA) ? [ ...res, { ...mapA[b.key], ...b } ] : res, []);
}
@ernestlv
ernestlv / union.js
Last active June 6, 2023 15:54
Calculate the union of two sets. It merges common elements using expression: { ...mapA[b.key], ...b }
/* it is either in A or B */
function union(arrA, arrB) {
const mapA = arrA.reduce((res, a) => ({ ...res, [a.key]:a }), {});
const inter = arrB.reduce((res, b) => (b.key in mapA) ? [ ...res, { ...mapA[b.key], ...b } ] : res, []);
const mapInter = inter.reduce((res, i) => ({ ...res, [i.key]:i }), {});
const diffA = arrA.reduce((res, a) => !(a.key in mapInter) ? [ ...res, a ] : res, []);
const diffB = arrB.reduce((res, b) => !(b.key in mapInter) ? [ ...res, b ] : res, []);
return [ ...diffA, ...inter, ...diffB ];
}
@ernestlv
ernestlv / const.js
Last active June 7, 2023 15:59
Defines a constant property in an object.
const obj = {};
Object.defineProperty(obj, "x", { value: 42, writable: false });
// or
const obj2 = {
get x() {
return 42;
},
};
@ernestlv
ernestlv / this.js
Created June 7, 2023 17:34
Provides different values of "this" depending if running in strict mode or not.
/* sloppy mode */
/* It always return an object. Boxes "this" in a primitive object if a primitive value is provided or the global object otherwise */
function fun() {
return this;
}
fun.call(2) // Number(2)
fun.bind(true)() //Boolean(true)
fun.call(null) //window object
func.apply(undefined) //window object
@ernestlv
ernestlv / arguments.js
Last active June 7, 2023 18:51
arguments and strict mode
/* sloppy mode */
function test(a) {
arguments[0] = 'a'; //local arguments synch up with argument variable
console.log(a, arguments[0], test.arguments[0]);
}
test(1,2); // prints a, a, 1
/*strict mode */
function test2(a) {
"use strict";
@ernestlv
ernestlv / eval.js
Last active June 7, 2023 18:51
eval behavior in strict mode
/* sloppy mode */
function test(arg) {
alert(eval("var arg = 1; arg")+" "+arg); //arg in eval changes arg in func context. if you declare arg suing let it will lock in eval context.
}
test(3); //prints 1 1
/* strict mode */
function test(arg) {
"use strict"
alert(eval("var arg = 1; arg")+" "+arg); //arg in eval does not change arg in func context
@ernestlv
ernestlv / test-component.js
Last active June 20, 2023 17:29
#Angular component test with change detection, click event and input validation
import { DebugElement } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { By } from "@angular/platform-browser"; // predicates for browser environment
import { ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(() => TestBed.configureTestingModule({
imports: [ReactiveFormsModule],
declarations: [AppComponent]
@ernestlv
ernestlv / async.js
Created June 14, 2023 02:37
Async call in React Hooks
const Main = () => {
useEffect(() => { // runs after Main component is rendered
async function init() {
console.log('fetching suggestions ...');
let response;
try {
response = await fetch('https://pokeapi.co/api/v2/pokemon/');
response = await response.json();
@ernestlv
ernestlv / sectional.tsx
Created June 15, 2023 20:43
Simple React functional component with TypeScript annotations
import React from 'react';
interface SectionalProps {
title:string;
list:Array<{key:string, value:string}>;
onClick:( value: string ) => void;
}
export const Sectional:React.FC<SectionalProps> = ( { tile, list, onClick } ) => { //props is passed by react as an object
@ernestlv
ernestlv / state.js
Last active June 16, 2023 20:10
Creates a simple local state in react with hooks
import { useState } from 'react'; // hook
const Root = () => { // functional component
const [ state, setState ] = useState(0); // local state for Root component
const click = () => {
setState( state + 1 ); // re-render Main component
}
console.log("render Main:", state);
return ( // jsx