This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const obj = {}; | |
Object.defineProperty(obj, "x", { value: 42, writable: false }); | |
// or | |
const obj2 = { | |
get x() { | |
return 42; | |
}, | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 ]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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, []); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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, []); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 }), {}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); |