Skip to content

Instantly share code, notes, and snippets.

View hoangtranson's full-sized avatar
🎯
Focusing

Hoang Tran Son hoangtranson

🎯
Focusing
View GitHub Profile
@hoangtranson
hoangtranson / ArrayUtil.js
Created April 7, 2018 08:16
Util to work with array
export const arrayofRandom = randomCeil => length => Array.from({ length: length }, (v, i) => Math.floor(Math.random() * randomCeil));
@hoangtranson
hoangtranson / enum.js
Created May 9, 2019 03:57
Number Enums and Strings
var Tristate;
(function (Tristate) {
Tristate[Tristate["False"] = 0] = "False";
Tristate[Tristate["True"] = 1] = "True";
Tristate[Tristate["Unknown"] = 2] = "Unknown";
})(Tristate || (Tristate = {}));
@hoangtranson
hoangtranson / weekday.ts
Created May 9, 2019 06:12
Enum with static functions
enum Weekday {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
namespace Weekday {
@hoangtranson
hoangtranson / LettersOnlyValidator.ts
Last active May 9, 2019 06:14
Namespaced Validators
/// <reference path="Validation.ts" />
namespace Validation {
const lettersRegexp = /^[A-Za-z]+$/;
export class LettersOnlyValidator implements StringValidator {
isAcceptable(s: string) {
return lettersRegexp.test(s);
}
}
}
@hoangtranson
hoangtranson / switch.js
Created May 10, 2019 10:15
re-thinking switch code
function counter(state = 0, action) {
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
@hoangtranson
hoangtranson / route.js
Last active July 1, 2019 09:32
Accessing URL parameter in Angular
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { DashboardComponent } from './dashboard/dashboard.component';
import { HeroesComponent } from './heroes/heroes.component';
import { HeroDetailComponent } from './hero-detail/hero-detail.component';
const routes: Routes = [
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: 'dashboard', component: DashboardComponent },
@hoangtranson
hoangtranson / index.html
Last active July 30, 2019 07:44
Test performance with prototype
<!DOCTYPE html>
<html>
<head>
<title>Prototype Performance</title>
<meta charset="UTF-8" />
</head>
<body>
<div id="app" style="margin:100px"></div>
@hoangtranson
hoangtranson / functionConstructor.js
Last active August 4, 2019 13:01
create function by Function constructor
const sum = new Function('a', 'b', 'return a + b');
console.log(sum(2, 6));
// expected output: 8
@hoangtranson
hoangtranson / functionExpression.js
Created August 4, 2019 13:14
Create function by function expression
function calcRectArea(width, height) {
return width * height;
}
console.log(calcRectArea(5, 6));
@hoangtranson
hoangtranson / hoisting.js
Created August 4, 2019 13:20
test hoisting on browser
var hoisted = "foo" in this;
console.log(`'foo' name ${hoisted ? "is" : "is not"} hoisted. typeof foo is ${typeof foo}`);
if (false) {
function foo(){ return 1; }
}
// In Chrome:
// 'foo' name is hoisted. typeof foo is undefined
//
// In Firefox: