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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / diff.js
Last active June 6, 2023 15:49
Calculate the difference between two sets
/* it is in A but not in B */
function diff(arrA, arrB) {
const mapB = arrB.reduce((res, b) => ({ ...res, [b.key]:b }), {});
return arrA.reduce((res, a) => !(a.key in mapB) ? [ ...res, a ] : res, []);
}
@ernestlv
ernestlv / array2map.js
Created June 6, 2023 15:24
convert arr to map
/* converts an array of the form [ { key, value } ] to a map of the form { key:value } */
const arr = [{key:"123", value:"abc"}, {key:"456", value:"def"}, {key:"789", value:"ghi"}];
const map = arr.reduce((res, o) => ({ ...res, [o.key]:o }), {});
@ernestlv
ernestlv / proxy-json.js
Created March 23, 2018 16:48
Proxy a REST/json request to an external API in Node.js
const http = require('http')
const httpS = require('https')
const Router = require('router')
const port = process.env.PORT || 3000
const router = Router()
router.get('/', (req, res) => {
res.write('REST API is up!');